public Comment(Comment other) { super(null, other.getContent()); }
/** * Changes all comments locally. * * @param modifier A function that take content and returns new content. */ public default void changeComments(Function<String, String> modifier) { for (final Comment child : getComments()) { final String content = child.getContent(); final String newContent = modifier.apply(content); if (Operators.notEquals(content, newContent)) { child.setContent(newContent); } } }
case COMMENT: final Comment comment = (Comment) node; out.print(" " + comment.getContent().length()); break; case DOCUMENT:
/** * Merges all consecutive comments locally. */ public default void mergeComments() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.COMMENT && next.getType() == NodeType.COMMENT) { // ref and next are both texts: merge them ((Comment) ref).appendContent(((Comment) 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 Comment x = new Comment(doc); assertEquals(NodeType.COMMENT, 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 Comment x = new Comment(); assertEquals(NodeType.COMMENT, 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)); }
@Test public void testChangeComments() { final Element root = new Element(NAME); root.addComment("hello", false); root.addComment("HELLO", false); root.changeComments(String::toUpperCase); assertEquals(2, root.getChildrenCount()); assertEquals("HELLO", root.getChildAt(Comment.class, 0).getContent()); assertEquals("HELLO", root.getChildAt(Comment.class, 1).getContent()); root.removeChildren(); final Element child1 = root.addElement(NAME1); final Element child2 = root.addElement(NAME2); final Element child3 = root.addElement(NAME1); child1.addComment("hello"); child2.addComment("hello"); child3.addComment("hel"); child3.addComment("lo", false); assertEquals(2, child3.getChildrenCount()); root.changeComments(String::toUpperCase, true); assertEquals(2, child3.getChildrenCount()); assertEquals("HELLO", child1.getChild(Comment.class).getContent()); assertEquals("HEL", child3.getChildAt(Comment.class, 0).getContent()); }
case COMMENT: final Comment comment = (Comment) node; writer.addComment(comment.getContent()); break; case DOCUMENT: