@Override public Comment clone(boolean recurse) { return new Comment(this); }
@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(); } }
/** * 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; }
@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()); } }
@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)); }
@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()); }
@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)); }