Java Code Examples for org.w3c.dom.ls.LSParser#parse()

The following examples show how to use org.w3c.dom.ls.LSParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelTestUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Normalize and pretty-print XML so that it can be compared using string
 * compare. The following code does the following: - Removes comments -
 * Makes sure attributes are ordered consistently - Trims every element -
 * Pretty print the document
 *
 * @param xml The XML to be normalized
 * @return The equivalent XML, but now normalized
 */
public static String normalizeXML(String xml) throws Exception {
    // Remove all white space adjoining tags ("trim all elements")
    xml = xml.replaceAll("\\s*<", "<");
    xml = xml.replaceAll(">\\s*", ">");

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    LSInput input = domLS.createLSInput();
    input.setStringData(xml);
    Document document = lsParser.parse(input);

    LSSerializer lsSerializer = domLS.createLSSerializer();
    lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
    lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    return lsSerializer.writeToString(document);
}
 
Example 2
Source File: XMLUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Normalize and pretty-print XML so that it can be compared using string
 * compare. The following code does the following: - Removes comments -
 * Makes sure attributes are ordered consistently - Trims every element -
 * Pretty print the document
 *
 * @param xml The XML to be normalized
 * @return The equivalent XML, but now normalized
 */
public static String normalizeXML(String xml) throws Exception {
    // Remove all white space adjoining tags ("trim all elements")
    xml = xml.replaceAll("\\s*<", "<");
    xml = xml.replaceAll(">\\s*", ">");

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    LSInput input = domLS.createLSInput();
    input.setStringData(xml);
    Document document = lsParser.parse(input);

    LSSerializer lsSerializer = domLS.createLSSerializer();
    lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
    lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    return lsSerializer.writeToString(document);
}
 
Example 3
Source File: DocumentLSTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLSInputParsingByteStream() throws Exception {
    DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
    LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
    LSInput src = impl.createLSInput();

    try (InputStream is = new FileInputStream(ASTROCAT)) {
        src.setByteStream(is);
        assertNotNull(src.getByteStream());
        // set certified accessor methods
        boolean origCertified = src.getCertifiedText();
        src.setCertifiedText(true);
        assertTrue(src.getCertifiedText());
        src.setCertifiedText(origCertified); // set back to orig

        src.setSystemId(filenameToURL(ASTROCAT));

        Document doc = domParser.parse(src);
        Element result = doc.getDocumentElement();
        assertEquals(result.getTagName(), "stardb");
    }
}
 
Example 4
Source File: Bug6355326.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testExternalEncoding() {

    try {
        LSInput src = null;
        LSParser dp = null;

        src = createLSInputEncoding();
        dp = createLSParser();

        src.setEncoding("UTF-16");
        Document doc = dp.parse(src);
        Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly");

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example 5
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs all start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0010() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_REJECT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 6
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0011() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_SKIP;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 1) {
        Assert.fail("Not all Element nodes skipped");
    }
    System.out.println("OKAY");
}
 
Example 7
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning
 * with state, input and output values orientation
 * for public Document parse(LSInput is),
 * <br><b>pre-conditions</b>: set filter that REJECTs any CHILD* node,
 * <br><b>is</b>: xml1
 * <br><b>output</b>: XML document with ELEMNENT1 and ELEMENT2 only.
 */
@Test
public void testfilter0001() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("CHILD")) {
                return FILTER_REJECT;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }

    System.out.println("OKAY");
}
 
Example 8
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 only.
 */
@Test
public void testFilter0009() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().startsWith("ELEMENT2")) {
                return FILTER_INTERRUPT;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 9
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
 */
@Test
public void testFilter0008() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().equals("ELEMENT1")) {
                return FILTER_SKIP;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 10
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs any CHILD* start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 and ELEMENT2 only.
 */
@Test
public void testFilter0007() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().startsWith("CHILD")) {
                return FILTER_REJECT;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 11
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0006() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_SKIP;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 12
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0005() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_REJECT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 13
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that accepts all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: full XML document.
 */
@Test
public void testFilter0004() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 14
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 only.
 */
@Test
public void testFilter0003() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("ELEMENT2")) {
                return FILTER_INTERRUPT;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 15
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
 */
@Test
public void testFilter0002() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("ELEMENT1")) {
                return FILTER_SKIP;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 16
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean match(String template, Node source) {
    LSParser dp = createLSParser();
    if (dp == null) {
        System.out.println("Can not create LSParser.");
        return false;
    }
    LSInput src = getXmlSource(template);
    Document doc = dp.parse(src);
    return checkXMLs(doc, source);
}
 
Example 17
Source File: LSSerializerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testEntityReference() throws Exception {
    final String XML_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" +
            "<!DOCTYPE author [\n" +
            " <!ENTITY name \"Jo Smith\">" +
            " <!ENTITY name1 \"&name;\">" +
            " <!ENTITY name2 \"&name1;\">" +
            "<!ENTITY ele \"<aa><bb>text</bb></aa>\">" +
            " <!ENTITY ele1 \"&ele;\">" +
            " <!ENTITY ele2 \"&ele1;\">" +
            " ]>" +
            " <author><a>&name1;</a>" +
            "<b>b &name2; &name1; b</b>" +
            "<c> &name; </c>" +
            "<d>&ele1;d</d>" +
            "<e> &ele2;eee </e>" +
            "<f>&lt;att&gt;</f>" +
            "<g> &ele; g</g>" +
            "<h>&ele2;</h></author>" ;


    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    DOMImplementation domImplementation = documentBuilder.getDOMImplementation();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation;

    LSParser domParser = domImplementationLS.createLSParser(MODE_SYNCHRONOUS, null);
    domParser.getDomConfig().setParameter("entities", Boolean.TRUE);

    LSInput src = domImplementationLS.createLSInput();
    src.setStringData(XML_DOCUMENT);
    Document document = domParser.parse(src);

    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

    lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
    System.out.println("test with default entities is " + lsSerializer.getDomConfig().getParameter("entities"));
    Assert.assertEquals(lsSerializer.writeToString(document),
            "<?xml version=\"1.1\" encoding=\"UTF-16\"?><!DOCTYPE author [ \n" +
            "<!ENTITY name 'Jo Smith'>\n" +
            "<!ENTITY name1 '&name;'>\n" +
            "<!ENTITY name2 '&name1;'>\n" +
            "<!ENTITY ele '<aa><bb>text</bb></aa>'>\n" +
            "<!ENTITY ele1 '&ele;'>\n" +
            "<!ENTITY ele2 '&ele1;'>\n" +
            "]>\n" +
            "<author>\n" +
            "    <a>&name1;Jo Smith</a>\n" +
            "    <b>b &name2;Jo Smith &name1;Jo Smith b</b>\n" +
            "    <c>&name;Jo Smith </c>\n" +
            "    <d>&ele1;d</d>\n" +
            "    <e>&ele2;eee </e>\n" +
            "    <f>&lt;att&gt;</f>\n" +
            "    <g>&ele; g</g>\n" +
            "    <h>&ele2;</h>\n" +
            "</author>\n");

    lsSerializer.getDomConfig().setParameter("entities", Boolean.FALSE);
    System.out.println("test with entities is false");
    Assert.assertEquals(lsSerializer.writeToString(document),
            "<?xml version=\"1.1\" encoding=\"UTF-16\"?><!DOCTYPE author [ \n" +
            "<!ENTITY name 'Jo Smith'>\n" +
            "<!ENTITY name1 '&name;'>\n" +
            "<!ENTITY name2 '&name1;'>\n" +
            "<!ENTITY ele '<aa><bb>text</bb></aa>'>\n" +
            "<!ENTITY ele1 '&ele;'>\n" +
            "<!ENTITY ele2 '&ele1;'>\n" +
            "]>\n" +
            "<author>\n" +
            "    <a>&name;Jo Smith</a>\n" +
            "    <b>b &name;Jo Smith &name;Jo Smith b</b>\n" +
            "    <c>&name;Jo Smith </c>\n" +
            "    <d>\n" +
            "        <aa>\n" +
            "            <bb>text</bb>\n" +
            "        </aa>\n" +
            "        d\n" +
            "    </d>\n" +
            "    <e>\n" +
            "        <aa>\n" +
            "            <bb>text</bb>\n" +
            "        </aa>\n" +
            "        eee \n" +
            "    </e>\n" +
            "    <f>&lt;att&gt;</f>\n" +
            "    <g>\n" +
            "        <aa>\n" +
            "            <bb>text</bb>\n" +
            "        </aa>\n" +
            "         g\n" +
            "    </g>\n" +
            "    <h>\n" +
            "        <aa>\n" +
            "            <bb>text</bb>\n" +
            "        </aa>\n" +
            "    </h>\n" +
            "</author>\n");

}
 
Example 18
Source File: DOMConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the root element has one Text node with not fully
 * normalized characters, the 'check-character-normalization' parameter set
 * to true, <br>
 * <b>name</b>: error-handler <br>
 * <b>value</b>: DOMErrorHandler. <br>
 * <b>Expected results</b>: LSParser calls the specified error handler
 */
@Test
public void testCheckCharNorm001() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");

    if (lsImpl == null) {
        System.out.println("OK, the DOM implementation does not support the LS 3.0");
        return;
    }

    LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    DOMConfiguration config = lsParser.getDomConfig();

    if (!config.canSetParameter("check-character-normalization", Boolean.TRUE)) {
        System.out.println("OK, setting 'check-character-normalization' to true is not supported");
        return;
    }

    config.setParameter("check-character-normalization", Boolean.TRUE);

    TestHandler testHandler = new TestHandler();
    config.setParameter("error-handler", testHandler);

    LSInput lsInput = lsImpl.createLSInput();
    lsInput.setStringData("<root>\u0073\u0075\u0063\u0327\u006F\u006E</root>");
    Document doc = lsParser.parse(lsInput);

    if (null == testHandler.getError()) {
        Assert.fail("no error is reported, expected 'check-character-normalization-failure'");

    }

    return; // Status.passed("OK");

}
 
Example 19
Source File: DOMConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the root element contains a fully-normalized text, <br>
 * <b>name</b>: check-character-normalization <br>
 * <b>value</b>: false. <br>
 * <b>Expected results</b>: LSParser reports no errors
 */
@Test
public void testCheckCharNorm002() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");

    if (lsImpl == null) {
        System.out.println("OK, the DOM implementation does not support the LS 3.0");
        return;
    }

    LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    DOMConfiguration config = lsParser.getDomConfig();

    if (!config.canSetParameter("check-character-normalization", Boolean.FALSE)) {
        Assert.fail("setting 'check-character-normalization' to false is not supported");
    }

    config.setParameter("check-character-normalization", Boolean.FALSE);

    TestHandler testHandler = new TestHandler();
    config.setParameter("error-handler", testHandler);

    LSInput lsInput = lsImpl.createLSInput();
    lsInput.setStringData("<root>fully-normalized</root>");
    Document doc = lsParser.parse(lsInput);

    if (null != testHandler.getError()) {
        Assert.fail("no error is expected, but reported: " + testHandler.getError());

    }

    return; // Status.passed("OK");

}
 
Example 20
Source File: DOML3InputSourceFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public InputSource newInputSource(String filename) throws Exception {
    // Create DOMImplementationLS, and DOM L3 LSParser
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder bldr = fact.newDocumentBuilder();
    DOMImplementationLS impl = (DOMImplementationLS) bldr.getDOMImplementation();
    LSParser domparser = impl.createLSParser(MODE_SYNCHRONOUS, null);
    domparser.setFilter(new MyDOMBuilderFilter());

    // Parse the xml document to create the DOM Document using
    // the DOM L3 LSParser and a LSInput (formerly LSInputSource)
    Document doc = null;
    LSInput src = impl.createLSInput();
    // register the input file with the input source...
    String systemId = filenameToURL(filename);
    src.setSystemId(systemId);
    try (Reader reader = new FileReader(filename)) {
        src.setCharacterStream(reader);
        src.setEncoding("UTF-8");
        doc = domparser.parse(src);
    }

    // Use DOM L3 LSSerializer (previously called a DOMWriter)
    // to serialize the xml doc DOM to a file stream.
    String tmpCatalog = Files.createTempFile(Paths.get(USER_DIR), "catalog.xml", null).toString();

    LSSerializer domserializer = impl.createLSSerializer();
    domserializer.setFilter(new MyDOMWriterFilter());
    domserializer.getNewLine();
    DOMConfiguration config = domserializer.getDomConfig();
    config.setParameter("xml-declaration", Boolean.TRUE);
    String result = domserializer.writeToString(doc);
    try (FileWriter os = new FileWriter(tmpCatalog, false)) {
        os.write(result);
        os.flush();
    }

    // Return the Input Source created from the Serialized DOM L3 Document.
    InputSource catsrc = new InputSource(new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmpCatalog)))));
    catsrc.setSystemId(systemId);
    return catsrc;
}