Code example for DocumentType

Methods: getEntities, getNotations, isEqualNode

0
        domImplementation = builder.getDOMImplementation();
        document = builder.parse(new InputSource(new StringReader(xml)));
 
        // doctype nodes 
        doctype = document.getDoctype();
        if (doctype.getEntities() != null) {
            sp = (Entity) doctype.getEntities().item(0);
        } 
        if (doctype.getNotations() != null) {
            png = (Notation) doctype.getNotations().item(0);
        } 
 
        // document nodes 
        menu = document.getDocumentElement();
        item = (Element) menu.getChildNodes().item(1);
        itemXmlns = item.getAttributeNode("xmlns");
        itemXmlnsA = item.getAttributeNode("xmlns:a");
        name = (Element) item.getChildNodes().item(1);
        standard = name.getAttributeNode("a:standard");
        deluxe = name.getAttributeNode("deluxe");
        waffles = (Text) name.getChildNodes().item(0);
        description = (Element) item.getChildNodes().item(3);
        descriptionText1 = (Text) description.getChildNodes().item(0);
        descriptionText2 = (CDATASection) description.getChildNodes().item(1);
        descriptionText3 = (Text) description.getChildNodes().item(2);
        option1 = (Element) item.getChildNodes().item(5);
        option2 = (Element) item.getChildNodes().item(7);
        option2Reference = option2.getChildNodes().item(0);
        wafflemaker = (ProcessingInstruction) item.getChildNodes().item(9);
        nutrition = (Element) item.getChildNodes().item(11);
        vitamins = (Element) nutrition.getChildNodes().item(1);
        vitaminsXmlnsA = vitamins.getAttributeNode("xmlns:a");
        comment = (Comment) vitamins.getChildNodes().item(1);
        vitaminc = (Element) vitamins.getChildNodes().item(3);
        vitamincText = (Text) vitaminc.getChildNodes().item(0);
 
        allNodes = new ArrayList<Node>();
 
        if (sp != null) {
            allNodes.add(sp);
        } 
        if (png != null) {
            allNodes.add(png);
        } 
 
        allNodes.addAll(Arrays.asList(document, doctype, menu, item, itemXmlns,
                itemXmlnsA, name, standard, deluxe, waffles, description,
                descriptionText1, descriptionText2, descriptionText3, option1,
                option2, option2Reference, wafflemaker, nutrition, vitamins,
                vitaminsXmlnsA, comment, vitaminc, vitamincText));
    } 
 
    /** 
     * Android's parsed DOM doesn't include entity declarations. These nodes will 
     * only be tested for implementations that support them. 
     */ 
    @KnownFailure("Dalvik doesn't parse entity declarations") 
    public void testEntityDeclarations() { 
        assertNotNull("This implementation does not parse entity declarations", sp);
    } 
 
    /** 
     * Android's parsed DOM doesn't include notations. These nodes will only be 
     * tested for implementations that support them. 
     */ 
    @KnownFailure("Dalvik doesn't parse notations") 
    public void testNotations() { 
        assertNotNull("This implementation does not parse notations", png);
    } 
 
    public void testLookupNamespaceURIByPrefix() { 
        assertEquals(null, doctype.lookupNamespaceURI("a"));
        if (sp != null) {
            assertEquals(null, sp.lookupNamespaceURI("a"));
        } 
        if (png != null) {
            assertEquals(null, png.lookupNamespaceURI("a"));
        } 
        assertEquals(null, document.lookupNamespaceURI("a"));
        assertEquals(null, menu.lookupNamespaceURI("a"));
        assertEquals("http://addons", item.lookupNamespaceURI("a"));
        assertEquals("http://addons", itemXmlns.lookupNamespaceURI("a"));
        assertEquals("http://addons", itemXmlnsA.lookupNamespaceURI("a"));
        assertEquals("http://addons", name.lookupNamespaceURI("a"));
        assertEquals("http://addons", standard.lookupNamespaceURI("a"));
        assertEquals("http://addons", deluxe.lookupNamespaceURI("a"));
        assertEquals("http://addons", description.lookupNamespaceURI("a"));
        assertEquals("http://addons", descriptionText1.lookupNamespaceURI("a"));
        assertEquals("http://addons", descriptionText2.lookupNamespaceURI("a"));
        assertEquals("http://addons", descriptionText3.lookupNamespaceURI("a"));
        assertEquals("http://addons", option1.lookupNamespaceURI("a"));
        assertEquals("http://addons", option2.lookupNamespaceURI("a"));
        assertEquals("http://addons", option2Reference.lookupNamespaceURI("a"));
        assertEquals("http://addons", wafflemaker.lookupNamespaceURI("a"));
        assertEquals("http://addons", nutrition.lookupNamespaceURI("a"));
        assertEquals("http://usda", vitamins.lookupNamespaceURI("a"));
        assertEquals("http://usda", vitaminsXmlnsA.lookupNamespaceURI("a"));
        assertEquals("http://usda", comment.lookupNamespaceURI("a"));
        assertEquals("http://usda", vitaminc.lookupNamespaceURI("a"));
        assertEquals("http://usda", vitamincText.lookupNamespaceURI("a"));
    } 
 
    public void testLookupNamespaceURIWithNullPrefix() { 
        assertEquals(null, document.lookupNamespaceURI(null));
        assertEquals(null, doctype.lookupNamespaceURI(null));
        if (sp != null) {
            assertEquals(null, sp.lookupNamespaceURI(null));
        } 
        if (png != null) {
            assertEquals(null, png.lookupNamespaceURI(null));
        } 
        assertEquals(null, menu.lookupNamespaceURI(null));
        assertEquals("http://food", item.lookupNamespaceURI(null));
        assertEquals("http://food", itemXmlns.lookupNamespaceURI(null));
        assertEquals("http://food", itemXmlnsA.lookupNamespaceURI(null));
        assertEquals("http://food", name.lookupNamespaceURI(null));
        assertEquals("http://food", standard.lookupNamespaceURI(null));
        assertEquals("http://food", deluxe.lookupNamespaceURI(null));
        assertEquals("http://marketing", description.lookupNamespaceURI(null));
        assertEquals("http://marketing", descriptionText1.lookupNamespaceURI(null));
        assertEquals("http://marketing", descriptionText2.lookupNamespaceURI(null));
        assertEquals("http://marketing", descriptionText3.lookupNamespaceURI(null));
        assertEquals("http://food", option1.lookupNamespaceURI(null));
        assertEquals("http://food", option2.lookupNamespaceURI(null));
        assertEquals("http://food", option2Reference.lookupNamespaceURI(null));
        assertEquals("http://food", wafflemaker.lookupNamespaceURI(null));
        assertEquals("http://food", nutrition.lookupNamespaceURI(null));
        assertEquals("http://food", vitamins.lookupNamespaceURI(null));
        assertEquals("http://food", vitaminsXmlnsA.lookupNamespaceURI(null));
        assertEquals("http://food", comment.lookupNamespaceURI(null));
        assertEquals("http://food", vitaminc.lookupNamespaceURI(null));
        assertEquals("http://food", vitamincText.lookupNamespaceURI(null));
    } 
 
    public void testLookupNamespaceURIWithXmlnsPrefix() { 
        for (Node node : allNodes) {
            assertEquals(null, node.lookupNamespaceURI("xmlns"));
        } 
    } 
 
    public void testLookupPrefixWithShadowedUri() { 
        assertEquals(null, document.lookupPrefix("http://addons"));
        assertEquals(null, doctype.lookupPrefix("http://addons"));
        if (sp != null) {
            assertEquals(null, sp.lookupPrefix("http://addons"));
        } 
        if (png != null) {
            assertEquals(null, png.lookupPrefix("http://addons"));
        } 
        assertEquals(null, menu.lookupPrefix("http://addons"));
        assertEquals("a", item.lookupPrefix("http://addons"));
        assertEquals("a", itemXmlns.lookupPrefix("http://addons"));
        assertEquals("a", itemXmlnsA.lookupPrefix("http://addons"));
        assertEquals("a", name.lookupPrefix("http://addons"));
        assertEquals("a", standard.lookupPrefix("http://addons"));
        assertEquals("a", deluxe.lookupPrefix("http://addons"));
        assertEquals("a", description.lookupPrefix("http://addons"));
        assertEquals("a", descriptionText1.lookupPrefix("http://addons"));
        assertEquals("a", descriptionText2.lookupPrefix("http://addons"));
        assertEquals("a", descriptionText3.lookupPrefix("http://addons"));
        assertEquals("a", option1.lookupPrefix("http://addons"));
        assertEquals("a", option2.lookupPrefix("http://addons"));
        assertEquals("a", option2Reference.lookupPrefix("http://addons"));
        assertEquals("a", wafflemaker.lookupPrefix("http://addons"));
        assertEquals("a", nutrition.lookupPrefix("http://addons"));
        assertEquals(null, vitamins.lookupPrefix("http://addons"));
        assertEquals(null, vitaminsXmlnsA.lookupPrefix("http://addons"));
        assertEquals(null, comment.lookupPrefix("http://addons"));
        assertEquals(null, vitaminc.lookupPrefix("http://addons"));
        assertEquals(null, vitamincText.lookupPrefix("http://addons"));
    } 
 
    public void testLookupPrefixWithUnusedUri() { 
        for (Node node : allNodes) {
            assertEquals(null, node.lookupPrefix("http://unused"));
        } 
    } 
 
    public void testLookupPrefixWithNullUri() { 
        for (Node node : allNodes) {
            assertEquals(null, node.lookupPrefix(null));
        } 
    } 
 
    public void testLookupPrefixWithShadowingUri() { 
        assertEquals(null, document.lookupPrefix("http://usda"));
        assertEquals(null, doctype.lookupPrefix("http://usda"));
        if (sp != null) {
            assertEquals(null, sp.lookupPrefix("http://usda"));
        } 
        if (png != null) {
            assertEquals(null, png.lookupPrefix("http://usda"));
        } 
        assertEquals(null, menu.lookupPrefix("http://usda"));
        assertEquals(null, item.lookupPrefix("http://usda"));
        assertEquals(null, itemXmlns.lookupPrefix("http://usda"));
        assertEquals(null, itemXmlnsA.lookupPrefix("http://usda"));
        assertEquals(null, name.lookupPrefix("http://usda"));
        assertEquals(null, standard.lookupPrefix("http://usda"));
        assertEquals(null, deluxe.lookupPrefix("http://usda"));
        assertEquals(null, description.lookupPrefix("http://usda"));
        assertEquals(null, descriptionText1.lookupPrefix("http://usda"));
        assertEquals(null, descriptionText2.lookupPrefix("http://usda"));
        assertEquals(null, descriptionText3.lookupPrefix("http://usda"));
        assertEquals(null, option1.lookupPrefix("http://usda"));
        assertEquals(null, option2.lookupPrefix("http://usda"));
        assertEquals(null, option2Reference.lookupPrefix("http://usda"));
        assertEquals(null, wafflemaker.lookupPrefix("http://usda"));
        assertEquals(null, nutrition.lookupPrefix("http://usda"));
        assertEquals("a", vitamins.lookupPrefix("http://usda"));
        assertEquals("a", vitaminsXmlnsA.lookupPrefix("http://usda"));
        assertEquals("a", comment.lookupPrefix("http://usda"));
        assertEquals("a", vitaminc.lookupPrefix("http://usda"));
        assertEquals("a", vitamincText.lookupPrefix("http://usda"));
    } 
 
    public void testIsDefaultNamespace() { 
        assertFalse(document.isDefaultNamespace("http://food"));
        assertFalse(doctype.isDefaultNamespace("http://food"));
        if (sp != null) {
            assertFalse(sp.isDefaultNamespace("http://food"));
        } 
        if (png != null) {
            assertFalse(png.isDefaultNamespace("http://food"));
        } 
        assertFalse(menu.isDefaultNamespace("http://food"));
        assertTrue(item.isDefaultNamespace("http://food"));
        assertTrue(itemXmlns.isDefaultNamespace("http://food"));
        assertTrue(itemXmlnsA.isDefaultNamespace("http://food"));
        assertTrue(name.isDefaultNamespace("http://food"));
        assertTrue(standard.isDefaultNamespace("http://food"));
        assertTrue(deluxe.isDefaultNamespace("http://food"));
        assertFalse(description.isDefaultNamespace("http://food"));
        assertFalse(descriptionText1.isDefaultNamespace("http://food"));
        assertFalse(descriptionText2.isDefaultNamespace("http://food"));
        assertFalse(descriptionText3.isDefaultNamespace("http://food"));
        assertTrue(option1.isDefaultNamespace("http://food"));
        assertTrue(option2.isDefaultNamespace("http://food"));
        assertTrue(option2Reference.isDefaultNamespace("http://food"));
        assertTrue(wafflemaker.isDefaultNamespace("http://food"));
        assertTrue(nutrition.isDefaultNamespace("http://food"));
        assertTrue(vitamins.isDefaultNamespace("http://food"));
        assertTrue(vitaminsXmlnsA.isDefaultNamespace("http://food"));
        assertTrue(comment.isDefaultNamespace("http://food"));
        assertTrue(vitaminc.isDefaultNamespace("http://food"));
        assertTrue(vitamincText.isDefaultNamespace("http://food"));
    } 
 
    /** 
     * Xerces fails this test. It returns false always for entity, notation, 
     * document fragment and document type nodes. This contradicts its own 
     * behaviour on lookupNamespaceURI(null). 
     */ 
    public void testIsDefaultNamespaceNull_XercesBugs() { 
        String message = "isDefaultNamespace() should be consistent with lookupNamespaceURI(null)";
        assertTrue(message, doctype.isDefaultNamespace(null));
        if (sp != null) {
            assertTrue(message, sp.isDefaultNamespace(null));
        } 
        if (png != null) {
            assertTrue(message, png.isDefaultNamespace(null));
        } 
    } 
 
    public void testIsDefaultNamespaceNull() { 
        assertTrue(document.isDefaultNamespace(null));
        assertTrue(menu.isDefaultNamespace(null));
        assertFalse(item.isDefaultNamespace(null));
        assertFalse(itemXmlns.isDefaultNamespace(null));
        assertFalse(itemXmlnsA.isDefaultNamespace(null));
        assertFalse(name.isDefaultNamespace(null));
        assertFalse(standard.isDefaultNamespace(null));
        assertFalse(deluxe.isDefaultNamespace(null));
        assertFalse(description.isDefaultNamespace(null));
        assertFalse(descriptionText1.isDefaultNamespace(null));
        assertFalse(descriptionText2.isDefaultNamespace(null));
        assertFalse(descriptionText3.isDefaultNamespace(null));
        assertFalse(option1.isDefaultNamespace(null));
        assertFalse(option2.isDefaultNamespace(null));
        assertFalse(option2Reference.isDefaultNamespace(null));
        assertFalse(wafflemaker.isDefaultNamespace(null));
        assertFalse(nutrition.isDefaultNamespace(null));
        assertFalse(vitamins.isDefaultNamespace(null));
        assertFalse(vitaminsXmlnsA.isDefaultNamespace(null));
        assertFalse(comment.isDefaultNamespace(null));
        assertFalse(vitaminc.isDefaultNamespace(null));
        assertFalse(vitamincText.isDefaultNamespace(null));
    } 
 
    public void testDoctypeSetTextContent() throws TransformerException { 
        String original = domToString(document);
        doctype.setTextContent("foobar"); // strangely, this is specified to no-op
        assertEquals(original, domToString(document));
    } 
 
    public void testDocumentSetTextContent() throws TransformerException { 
        String original = domToString(document);
        document.setTextContent("foobar"); // strangely, this is specified to no-op
        assertEquals(original, domToString(document));
    } 
 
    public void testElementSetTextContent() throws TransformerException { 
        String original = domToString(document);
        nutrition.setTextContent("foobar");
        String expected = original.replaceFirst(
                "(?s)<nutrition>.*</nutrition>", "<nutrition>foobar</nutrition>"); 
        assertEquals(expected, domToString(document));
    } 
 
    public void testEntitySetTextContent() throws TransformerException { 
        if (sp == null) {
            return; 
        } 
        try { 
            sp.setTextContent("foobar");
            fail(); // is this implementation-specific behaviour?
        } catch (DOMException e) {
        } 
    } 
 
    public void testNotationSetTextContent() throws TransformerException { 
        if (png == null) {
            return; 
        } 
        String original = domToString(document);
        png.setTextContent("foobar");
        String expected = original.replace("image/png", "foobar");
        assertEquals(expected, domToString(document));
    } 
 
    /** 
     * Tests setTextContent on entity references. Although the other tests can 
     * act on a parsed DOM, this needs to use a programmatically constructed DOM 
     * because the parser may have replaced the entity reference with the 
     * corresponding text. 
     */ 
    public void testEntityReferenceSetTextContent() throws TransformerException { 
        document = builder.newDocument();
        Element root = document.createElement("menu");
        document.appendChild(root);
 
        EntityReference entityReference = document.createEntityReference("sp");
        root.appendChild(entityReference);
 
        try { 
            entityReference.setTextContent("Lite Syrup");
            fail();
        } catch (DOMException e) {
        } 
    } 
 
    public void testAttributeSetTextContent() throws TransformerException { 
        String original = domToString(document);
        standard.setTextContent("foobar");
        String expected = original.replace("standard=\"strawberry\"", "standard=\"foobar\"");
        assertEquals(expected, domToString(document));
    } 
 
    public void testTextSetTextContent() throws TransformerException { 
        String original = domToString(document);
        descriptionText1.setTextContent("foobar");
        String expected = original.replace(">Belgian<!", ">foobar<!");
        assertEquals(expected, domToString(document));
    } 
 
    public void testCdataSetTextContent() throws TransformerException { 
        String original = domToString(document);
        descriptionText2.setTextContent("foobar");
        String expected = original.replace(
                " waffles & strawberries (< 5g ", "foobar"); 
        assertEquals(expected, domToString(document));
    } 
 
    public void testProcessingInstructionSetTextContent() throws TransformerException { 
        String original = domToString(document);
        wafflemaker.setTextContent("foobar");
        String expected = original.replace(" square shape?>", " foobar?>");
        assertEquals(expected, domToString(document));
    } 
 
    public void testCommentSetTextContent() throws TransformerException { 
        String original = domToString(document);
        comment.setTextContent("foobar");
        String expected = original.replace("-- add other vitamins? --", "--foobar--");
        assertEquals(expected, domToString(document));
    } 
 
    public void testCoreFeature() { 
        assertFeature("Core", null); 
        assertFeature("Core", ""); 
        assertFeature("Core", "1.0"); 
        assertFeature("Core", "2.0"); 
        assertFeature("Core", "3.0"); 
        assertFeature("CORE", "3.0"); 
        assertFeature("+Core", "3.0"); 
        assertNoFeature("Core", "4.0"); 
    } 
 
    public void testXmlFeature() { 
        assertFeature("XML", null); 
        assertFeature("XML", ""); 
        assertFeature("XML", "1.0"); 
        assertFeature("XML", "2.0"); 
        assertFeature("XML", "3.0"); 
        assertFeature("Xml", "3.0"); 
        assertFeature("+XML", "3.0"); 
        assertNoFeature("XML", "4.0"); 
    } 
 
    /** 
     * The RI fails this test. 
     * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Document3-version 
     */ 
    public void testXmlVersionFeature() { 
        assertFeature("XMLVersion", null); 
        assertFeature("XMLVersion", ""); 
        assertFeature("XMLVersion", "1.0"); 
        assertFeature("XMLVersion", "1.1"); 
        assertFeature("XMLVERSION", "1.1"); 
        assertFeature("+XMLVersion", "1.1"); 
        assertNoFeature("XMLVersion", "1.2"); 
        assertNoFeature("XMLVersion", "2.0"); 
        assertNoFeature("XMLVersion", "2.0"); 
    } 
 
    @KnownFailure("Dalvik doesn't support load/save") 
    public void testLoadSaveFeature() { 
        assertFeature("LS", "3.0"); 
    } 
 
    @KnownFailure("Dalvik doesn't support the element traversal feature") 
    public void testElementTraversalFeature() { 
        assertFeature("ElementTraversal", "1.0"); 
    } 
 
    private void assertFeature(String feature, String version) {
        String message = "This implementation is expected to support "
                + feature + " v. " + version + " but does not.";
        assertTrue(message, domImplementation.hasFeature(feature, version));
        assertNotNull(message, domImplementation.getFeature(feature, version));
    } 
 
    private void assertNoFeature(String feature, String version) {
        assertFalse(domImplementation.hasFeature(feature, version));
        assertNull(domImplementation.getFeature(feature, version));
    } 
 
    public void testIsSupported() { 
        // we don't independently test the features; instead just assume the 
        // implementation calls through to hasFeature (as tested above) 
        for (Node node : allNodes) {
            assertTrue(node.isSupported("XML", null));
            assertTrue(node.isSupported("XML", "3.0"));
            assertFalse(node.isSupported("foo", null));
            assertFalse(node.isSupported("foo", "bar"));
        } 
    } 
 
    public void testGetFeature() { 
        // we don't independently test the features; instead just assume the 
        // implementation calls through to hasFeature (as tested above) 
        for (Node node : allNodes) {
            assertSame(node, node.getFeature("XML", null));
            assertSame(node, node.getFeature("XML", "3.0"));
            assertNull(node.getFeature("foo", null));
            assertNull(node.getFeature("foo", "bar"));
        } 
    } 
 
    public void testNodeEqualsPositive() throws Exception { 
        DomTest copy = new DomTest();
        copy.setUp();
 
        for (int i = 0; i < allNodes.size(); i++) {
            Node a = allNodes.get(i);
            Node b = copy.allNodes.get(i);
            assertTrue(a.isEqualNode(b));
        } 
    } 
 
    public void testNodeEqualsNegative() throws Exception { 
        for (Node a : allNodes) {
            for (Node b : allNodes) {
                assertEquals(a == b, a.isEqualNode(b));
            } 
        } 
    } 
 
    public void testNodeEqualsNegativeRecursive() throws Exception { 
        DomTest copy = new DomTest();
        copy.setUp();
        copy.vitaminc.setTextContent("55%");
 
        // changing anything about a node should break equality for all parents 
        assertFalse(document.isEqualNode(copy.document));
        assertFalse(menu.isEqualNode(copy.menu));
        assertFalse(item.isEqualNode(copy.item));
        assertFalse(nutrition.isEqualNode(copy.nutrition));
        assertFalse(vitamins.isEqualNode(copy.vitamins));
        assertFalse(vitaminc.isEqualNode(copy.vitaminc));
 
        // but not siblings 
        assertTrue(doctype.isEqualNode(copy.doctype));
        assertTrue(description.isEqualNode(copy.description));
        assertTrue(option1.isEqualNode(copy.option1));
    } 
 
    public void testNodeEqualsNull() {