/** * 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(); }
@Override public boolean canAddChild(Child child) { if (child == null) { return false; } else if (child.getType() == NodeType.ELEMENT) { return getRootElement() == null; } else { return true; } }
/** * 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)); }
/** * Reads an URL. * * @param url The URL. * @return The root element of the corresponding Document. * @throws IOException When an IO error occurs. */ public Element readRoot(URL url) throws IOException { return Document.getRootElement(read(url)); }
/** * Reads a file. * * @param filename The file name. * @return The root element of the corresponding Document. * @throws IOException When an IO error occurs. */ public Element readRoot(String filename) throws IOException { return Document.getRootElement(read(filename)); }
/** * Reads a file. * * @param file The file. * @return The root element of the corresponding Document. * @throws IOException When an IO error occurs. */ public Element readRoot(File file) throws IOException { return Document.getRootElement(read(file)); }
/** * Reads a string. * * @param s The string. * @param charset The charset. Must be compliant with string content. * @return The root element of the corresponding Document. * @throws IOException When an IO error occurs. */ public Element readRoot(String s, Charset charset) throws IOException { return Document.getRootElement(read(s, charset)); }
@Override public final Element getRootElement() { final Node root = getRootChild(); if (root.getType() == NodeType.DOCUMENT) { // Root is a document return ((Document) root).getRootElement(); } else if (root.getType() == NodeType.ELEMENT) { // Root is an element return (Element) root; } else { // Root is either a comment or a text. return null; } }
@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)); }
@Test public void testFeaturesDefault4() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child> </child></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child> </child> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); assertEquals(1, doc.getRootElement().getChildAt(Element.class, 0).getChildrenCount()); }); }
@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 testFeaturesDefault5() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child></child> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); assertEquals(0, doc.getRootElement().getChildAt(Element.class, 0).getChildrenCount()); }); }
@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()); }); }
@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()); }); }
@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()); }); }
@Test public void testFeaturesDefault3() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>aaa</root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root><!-- comment -->aaa</root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault6() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@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 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 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()); }