/** * Adds a comment as last child. * * @param content The comment content. * @param merge If {@code true}, then if last child exists and is a comment, * {@code content} is added to this last child.<br> * Otherwise, a comment child is created and added to last position. * @return The modified or created comment. */ public default Comment addComment(String content, boolean merge) { if (merge) { final Child last = getLastChild(); if (last != null && last.getType() == NodeType.COMMENT) { ((Comment) last).appendContent(content); return (Comment) last; } } final Comment result = new Comment(content); addChild(result); return result; }
/** * 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); } } }
@Override public String getQName() { return getParent().getQName() + "/comment()"; } }
@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 testClone() { final Comment x1 = new Comment(null, HELLO); final Comment x2 = x1.clone(false); final Comment x3 = x1.clone(true); assertNotEquals(x1, x2); assertNotEquals(x1, x3); assertTrue(x1.deepEquals(x2)); assertTrue(x2.deepEquals(x1)); }
/** * 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++; } } } }
public Comment(Comment other) { super(null, other.getContent()); }
@Test public void testGetRootElement() { final Comment x = new Comment(HELLO); assertEquals(null, x.getRootElement()); final Element root = new Element(NAME); root.addChild(x); assertEquals(root, x.getRootElement()); } }
@Override public Comment clone(boolean recurse) { return new Comment(this); }
@Test public void testChildren() { final Element e = new Element(NAME); assertEquals(null, e.getDocument()); assertEquals(e, e.getRootElement()); assertEquals(e, e.getRootChild()); final Element c0 = e.addElement(NAME1); final Element c1 = e.addElement(NAME1); final Text c2 = e.addText("Hello"); final Comment c3 = e.addComment("Hello"); assertEquals(c0, e.getChildAt(0)); assertEquals(c1, e.getChildAt(1)); assertEquals(c0, e.getElementNamed(NAME1)); assertEquals(null, c2.getDocument()); assertEquals(e, c3.getRootElement()); assertEquals(e, c3.getRootChild()); assertEquals(e, c2.getParent()); assertEquals(e, c2.getParent(Element.class)); assertEquals(4, e.getChildrenCount()); assertEquals(4, e.getChildrenCount(Node.class)); assertEquals(2, e.getChildrenCount(Element.class)); assertEquals(1, e.getChildrenCount(Comment.class)); assertEquals(1, e.getChildrenCount(Text.class)); assertEquals(2, e.getChildrenCount(Parent.class)); assertEquals(4, e.getChildrenCount(Child.class)); assertEquals(2, e.getChildrenCount(Leaf.class)); }
@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()); }
case COMMENT: final Comment comment = (Comment) node; out.print(" " + comment.getContent().length()); break; case DOCUMENT:
@Override public void comment(char[] ch, int start, int length) throws SAXException { LOGGER.trace("comment()"); if (top() == Action.KEEP) { // IGNORE_COMMENTS already handled in parser configuration checkActiveText(false); final Comment comment = new Comment(new String(ch, start, length)); currentParent.addChild(comment); resetActiveText(); } }
@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()); }
@Test public void testDeepEquals() { final Document doc1 = new Document(); final Document doc2 = new Document(); assertTrue(doc1.deepEquals(doc2)); doc1.addChild(new Comment()); assertFalse(doc1.deepEquals(doc2)); doc2.addChild(new Comment()); assertTrue(doc1.deepEquals(doc2)); doc1.addChild(new Element(NAME)); assertFalse(doc1.deepEquals(doc2)); doc2.addChild(new Element(NAME)); assertTrue(doc1.deepEquals(doc2)); ((Element) doc1.getLastChild()).addAttribute(NAME, VALUE); assertFalse(doc1.deepEquals(doc2)); ((Element) doc2.getLastChild()).addAttribute(NAME, VALUE); assertTrue(doc1.deepEquals(doc2)); final Document doc3 = doc1.clone(true); assertTrue(doc1.deepEquals(doc3)); doc3.removeChildren(); assertEquals(0, doc3.getChildrenCount()); }
case COMMENT: final Comment comment = (Comment) node; writer.addComment(comment.getContent()); break; case DOCUMENT: