public Text(Text other) { super(null, other.getContent()); }
public boolean isIgnorable() { return XmlUtil.isIgnorableSpace(getContent()); } }
/** * Returns the merged content of all children text nodes, if this node has no element children. * <p> * This will work when this node has only text and comment children.<br> * If it has element children, the default value is returned. * * @param def The default value. * @return The merged content of children texts (ignoring comments) or {@code def}. */ public String getText(String def) { if (children == null || children.isEmpty()) { return def; } else if (!hasChildren(Element.class)) { // Ignore comments final Iterable<Text> texts = getChildren(Text.class); if (Iterables.isEmpty(texts)) { return def; } else { final StringBuilder sb = new StringBuilder(); for (final Text text : texts) { sb.append(text.getContent()); } return sb.toString(); } } else { return def; } }
/** * Changes all texts locally. * * @param modifier A function that take content and returns new content. */ public default void changeTexts(Function<String, String> modifier) { for (final Text child : getTexts()) { final String content = child.getContent(); final String newContent = modifier.apply(content); if (Operators.notEquals(content, newContent)) { child.setContent(newContent); } } }
private void resetActiveText() { if (activeText != null) { activeText.setContent(textContentConverter.convertTextContent(activeText.getParent(), activeText.getContent())); activeText = null; } }
case TEXT: final Text text = (Text) node; out.print(" " + text.getContent().length()); break; default:
/** * Removes active text if possible and necessary. * * @param preserve If {@code true} should be preserved if necessary. * * @throws SAXException When mixed content is found and is not allowed. */ private void checkActiveText(boolean preserve) throws SAXException { if (activeText != null) { if (preserve && currentParent.getChildrenCount() == 1) { // Preserve the text that is the only child of its parent (element) resetActiveText(); } else if (activeText.getContent().isEmpty() || activeText.isIgnorable() && !isEnabled(Feature.LOAD_SPACES)) { currentParent.removeChildAt(currentParent.getChildrenCount() - 1); resetActiveText(); } else if (currentParent.getChildrenCount() > 1 && !isEnabled(Feature.ALLOW_MIXED_CONTENT)) { throw new SAXException("Mixed content not allowed"); } } }
/** * Sets the content of matching texts. * <p> * Uses a Depth First traversal for recursion. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param predicate The predicate of matching texts. <em>MUST NOT</em> be null. * @param converter The content converter. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void setContentOfMatchingTexts(Parent parent, TextPredicate predicate, TextContentConverter converter, Evaluator<? super Parent> evaluator) { for (final Child child : parent.getChildren()) { if (child instanceof Text) { final Text text = (Text) child; if (predicate.accepts(parent, text)) { final String content = converter.convertTextContent(parent, text.getContent()); text.setContent(content); } } else if (child instanceof Parent && evaluator.evaluate((Parent) child) == Evaluation.CONTINUE) { setContentOfMatchingTexts((Parent) child, predicate, converter, evaluator); } } }
/** * 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)); }
public static void setContentOfMatchingElements(Parent parent, ElementPredicate predicate, TextContentConverter converter, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final Element element = (Element) parent; if (predicate.accepts(element.getParent(), element)) { final int count = element.getChildrenCount(); if (count == 0) { // Add a text child final String content = converter.convertTextContent(element, null); if (content != null) { element.addText(content); } } else if (count == 1 && element.hasOnlyText()) { // Modify text child if possible final Text text = element.getChild(Text.class); final String content = converter.convertTextContent(element, text.getContent()); text.setContent(content); } } } for (final Child child : parent.getChildren()) { if (child instanceof Parent && evaluator.evaluate((Parent) child) == Evaluation.CONTINUE) { setContentOfMatchingElements((Parent) child, predicate, converter, evaluator); } } }
case TEXT: final Text text = (Text) node; writer.addElementContent(text.getContent()); break; default:
@Test public void testChangeTexts() { final Element root = new Element(NAME); root.addText("hello", false); root.addText("HELLO", false); root.changeTexts(String::toUpperCase); assertEquals(2, root.getChildrenCount()); assertEquals("HELLO", root.getChildAt(Text.class, 0).getContent()); assertEquals("HELLO", root.getChildAt(Text.class, 1).getContent()); root.removeChildren(); final Element child1 = root.addElement(NAME1); final Element child2 = root.addElement(NAME2); final Element child3 = root.addElement(NAME1); child1.addText("hello"); child2.addText("hello"); child3.addText("hel"); child3.addText("lo", false); assertEquals(2, child3.getChildrenCount()); root.changeTexts(String::toUpperCase); assertEquals(2, child3.getChildrenCount()); root.changeNamedTexts(NAME1, String::toUpperCase); assertEquals(1, child3.getChildrenCount()); assertEquals("HELLO", child1.getText()); assertEquals("HELLO", child3.getText()); }