@Override public Document clone(boolean recurse) { final Document result = new Document(); if (recurse) { for (final Child child : getChildren()) { result.addChild(child.clone(true)); } } return result; }
@Override public boolean deepEquals(Node node) { if (this == node) { return true; } if (!(node instanceof Document)) { return false; } final Document other = (Document) node; if (getChildrenCount() == other.getChildrenCount()) { for (int index = 0; index < getChildrenCount(); index++) { if (!getChildAt(index).deepEquals(other.getChildAt(index))) { return false; } } } else { return false; } return true; }
/** * Returns the root element of a document. * <p> * If {@code document} is {@code null}, then {@code null} is returned. * * @param document The document (possibly {@code null}). * @return The root element of {@code document}. */ public static Element getRootElement(Document document) { return document == null ? null : document.getRootElement(); }
@Test public void testFeaturesCommentsMixed1() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!--comment--><root> aaa\n aaa<child/> aaa\n aaa</root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!--comment--><root> aaa\n aaa<child/> aaa\n aaa</root>", (doc) -> { assertEquals(2, doc.getChildrenCount()); assertEquals(1, doc.getChildrenCount(Comment.class)); assertEquals(3, doc.getRootElement().getChildrenCount()); assertEquals(2, doc.getRootElement().getChildrenCount(Text.class)); }, Feature.LOAD_COMMENTS, Feature.LOAD_SPACES, Feature.ALLOW_MIXED_CONTENT); }
@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 testContructors() { final Document doc = new Document(); assertEquals(doc, doc.getDocument()); assertEquals(null, doc.getRootElement()); assertEquals(0, doc.getChildrenCount()); assertTrue(doc.deepEquals(doc)); assertEquals(null, doc.getLastChild()); }
@Test public void testRoot() { final Document doc = new Document(); assertEquals(null, doc.getRootElement()); final Element root = new Element("root"); assertEquals(null, root.getParent()); assertTrue(doc.canAddChild(root)); doc.addChild(root); assertFalse(doc.canAddChild(root)); assertEquals(root, doc.getRootElement()); root.setParent(null); assertEquals(null, root.getParent()); assertEquals(null, doc.getRootElement()); } }
@Test public void testGetRootElement() { assertEquals(null, Document.getRootElement(null)); final Document doc = new Document(); assertEquals(null, doc.getRootElement()); assertEquals(null, Document.getRootElement(doc)); final Element root = new Element(NAME); doc.addChild(root); assertEquals(root, doc.getRootElement()); assertEquals(root, doc.getRootElement()); assertEquals(root, Document.getRootElement(doc)); }
public static void main(String[] args) throws IOException { LOGGER.info("Start " + XmlDataWriterDemo.class.getSimpleName()); Debug.printClassPath(); for (final URL url : Resources.getResources("log4j2.xml")) { LOGGER.info(" " + url); } final Document doc = new Document(); final Element root = new Element("root"); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); root.addAttribute("att1", "value1"); final Element child = new Element("child"); child.addAttribute("att1", "value1"); root.addChild(child); doc.removeChildren(); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); try (final XmlDataWriter writer = new XmlDataWriter(NonCloseableOutputStream.NON_CLOSABLE_SYSTEM_OUT)) { writer.getXmlWriter().setEnabled(XmlWriter.Feature.PRETTY_PRINT, true); writer.write(doc); writer.flush(); } } }
@Test public void testCanAddChild() { final Document doc = new Document(); assertFalse(doc.canAddChild(null)); final Element root = doc.addElement("root"); assertFalse(doc.canAddChild(root)); }
@Override public void startDocument() throws SAXException { LOGGER.trace("startDocument()"); document = new Document(); currentParent = document; resetActiveText(); }
@Test public void testFeaturesComments() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!--comment--><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <!--comment--> <root> \n <child/> \n </root>", (doc) -> { assertEquals(2, doc.getChildrenCount()); assertEquals(1, doc.getChildrenCount(Comment.class)); }, Feature.LOAD_COMMENTS); }
@Test public void testFeaturesDefault0() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root/><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(0, doc.getRootElement().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 testFeaturesDefault1() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root></root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(0, doc.getRootElement().getChildrenCount()); }); }
@Override public boolean canAddChild(Child child) { if (child == null) { return false; } else if (child.getType() == NodeType.ELEMENT) { return getRootElement() == null; } else { return true; } }
@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 testFeaturesDefault2() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root> </root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root> <!-- comment --> </root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
/** * Reads an InputStream. * * @param is The InputStream. * @param systemId The systemId which is needed for resolving relative URIs. * @return The root element of the corresponding Document. * @throws IOException When an IO error occurs. */ public Element readRoot(InputStream is, String systemId) throws IOException { return Document.getRootElement(read(is, systemId)); }
@Test public void testFeaturesDefault7() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child/> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }