@Override public void characters(char[] ch, int start, int length) throws SAXException { LOGGER.trace("characters()"); if (top() == Action.KEEP) { if (activeText == null) { activeText = new Text(); currentParent.addChild(activeText); } activeText.appendContent(ch, start, length); } }
/** * Adds a text as last child. * * @param content The text content. * @param merge If {@code true}, then if last child exists and is a text, * {@code content} is added to this last child.<br> * Otherwise, a text child is created and added to last position. * @return The modified or created text. */ public Text addText(String content, boolean merge) { if (merge) { final Child last = getLastChild(); if (last != null && last.getType() == NodeType.TEXT) { ((Text) last).appendContent(content); return (Text) last; } } final Text result = new Text(content); addChild(result); return result; }
final Text text = child.getChild(Text.class); text.clearContent(); text.appendContent(newContent); } else { child.removeChildren();
/** * Merges all consecutive texts locally. */ public default void mergeTexts() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.TEXT && next.getType() == NodeType.TEXT) { // ref and next are both texts: merge them ((Text) ref).appendContent(((Text) next).getContent()); // Remove next next.detach(); // Do not change index and ref } else { ref = next; index++; } } } }
@Test public void testConstructors2() { final Document doc = new Document(); final Text x = new Text(doc); assertEquals(NodeType.TEXT, x.getType()); assertEquals(doc, x.getParent()); assertEquals("", x.getContent()); x.setContent(HELLO); assertEquals(HELLO, x.getContent()); x.clearContent(); assertEquals("", x.getContent()); x.appendContent(HELLO); assertEquals(HELLO, x.getContent()); x.appendContent(null); assertEquals(HELLO, x.getContent()); assertEquals(null, x.getRootElement()); assertEquals(doc, x.getDocument()); assertEquals(x, x.getRootChild()); }
@Test public void testConstructors1() { final Text x = new Text(); assertEquals(NodeType.TEXT, x.getType()); assertEquals(null, x.getParent()); assertEquals("", x.getContent()); x.setContent(HELLO); assertEquals(HELLO, x.getContent()); x.clearContent(); assertEquals("", x.getContent()); x.appendContent(HELLO); assertEquals(HELLO, x.getContent()); x.appendContent(null); assertEquals(HELLO, x.getContent()); assertEquals(null, x.getRootElement()); assertEquals(null, x.getDocument()); assertEquals(x, x.getRootChild()); assertTrue(x.deepEquals(x)); assertFalse(x.deepEquals(null)); }