Code example for Document

Methods: getDocumentElementgetDomConfig

0
 
    public void testCdataSectionsNotHonoredByNodeNormalize() throws Exception { 
        String xml = "<foo>ABC<![CDATA[DEF]]>GHI</foo>";
        parse(xml);
        domConfiguration.setParameter("cdata-sections", true);
        document.getDocumentElement().normalize();
        assertEquals(xml, domToString(document));
 
        parse(xml);
        domConfiguration.setParameter("cdata-sections", false);
        document.getDocumentElement().normalize();
        assertEquals(xml, domToString(document));
    } 
 
    public void testCdataSectionsHonoredByDocumentNormalize() throws Exception { 
        String xml = "<foo>ABC<![CDATA[DEF]]>GHI</foo>";
        parse(xml);
        domConfiguration.setParameter("cdata-sections", true);
        document.normalizeDocument();
        assertEquals(xml, domToString(document));
 
        parse(xml);
        domConfiguration.setParameter("cdata-sections", false);
        document.normalizeDocument();
        String expected = xml.replace("<![CDATA[DEF]]>", "DEF");
        assertEquals(expected, domToString(document));
    } 
 
    public void testMergeAdjacentTextNodes() throws Exception { 
        document = createDocumentWithAdjacentTexts("abc", "def");
        document.getDocumentElement().normalize();
        assertChildren(document.getDocumentElement(), "abcdef");
    } 
 
    public void testMergeAdjacentEmptyTextNodes() throws Exception { 
        document = createDocumentWithAdjacentTexts("", "", "");
        document.getDocumentElement().normalize();
        assertChildren(document.getDocumentElement());
    } 
 
    public void testMergeAdjacentNodesWithNonTextSiblings() throws Exception { 
        document = createDocumentWithAdjacentTexts("abc", "def", "<br>", "ghi", "jkl");
        document.getDocumentElement().normalize();
        assertChildren(document.getDocumentElement(), "abcdef", "<br>", "ghijkl");
    } 
 
    public void testMergeAdjacentNodesEliminatesEmptyTexts() throws Exception { 
        document = createDocumentWithAdjacentTexts("", "", "<br>", "", "", "<br>", "", "<br>", "");
        document.getDocumentElement().normalize();
        assertChildren(document.getDocumentElement(), "<br>", "<br>", "<br>");
    } 
 
    public void testRetainingComments() throws Exception { 
        String xml = "<foo>ABC<!-- bar -->DEF<!-- baz -->GHI</foo>";
        parse(xml);
        domConfiguration.setParameter("comments", true);
        document.normalizeDocument();
        assertEquals(xml, domToString(document));
    } 
 
    public void testCommentContainingDoubleDash() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        root.appendChild(document.createComment("ABC -- DEF"));
        document.normalizeDocument();
        errorRecorder.assertAllErrors(DOMError.SEVERITY_ERROR, "wf-invalid-character");
    } 
 
    public void testStrippingComments() throws Exception { 
        String xml = "<foo>ABC<!-- bar -->DEF<!-- baz -->GHI</foo>";
        parse(xml);
        domConfiguration.setParameter("comments", false);
        document.normalizeDocument();
        assertChildren(document.getDocumentElement(), "ABCDEFGHI");
    } 
 
    public void testSplittingCdataSectionsSplit() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("split-cdata-sections", true);
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        root.appendChild(document.createCDATASection("ABC]]>DEF]]>GHI"));
        document.normalizeDocument();
        errorRecorder.assertAllErrors(DOMError.SEVERITY_WARNING, "cdata-sections-splitted");
        assertChildren(root, "<![CDATA[ABC]]]]>", "<![CDATA[>DEF]]]]>", "<![CDATA[>GHI]]>");
    } 
 
    public void testSplittingCdataSectionsReportError() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("split-cdata-sections", false);
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        root.appendChild(document.createCDATASection("ABC]]>DEF"));
        document.normalizeDocument();
        errorRecorder.assertAllErrors(DOMError.SEVERITY_ERROR, "wf-invalid-character");
    } 
 
    public void testInvalidCharactersCdata() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("cdata-sections", true);
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        CDATASection cdata = document.createCDATASection("");
        root.appendChild(cdata);
 
        for (int c = 0; c <= Character.MAX_VALUE; c++) {
            cdata.setData(new String(new char[]{ 'A', 'B', (char) c }));
            document.normalizeDocument();
            if (isValid((char) c)) {
                assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
            } else { 
                errorRecorder.assertAllErrors("For character " + c,
                        DOMError.SEVERITY_ERROR, "wf-invalid-character");
            } 
        } 
    } 
 
    public void testInvalidCharactersText() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        Text text = document.createTextNode("");
        root.appendChild(text);
 
        for (int c = 0; c <= Character.MAX_VALUE; c++) {
            text.setData(new String(new char[]{ 'A', 'B', (char) c }));
            document.normalizeDocument();
            if (isValid((char) c)) {
                assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
            } else { 
                errorRecorder.assertAllErrors("For character " + c,
                        DOMError.SEVERITY_ERROR, "wf-invalid-character");
            } 
        } 
    } 
 
    public void testInvalidCharactersAttribute() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
 
        for (int c = 0; c <= Character.MAX_VALUE; c++) {
            root.setAttribute("bar", new String(new char[] { 'A', 'B', (char) c}));
            document.normalizeDocument();
            if (isValid((char) c)) {
                assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
            } else { 
                errorRecorder.assertAllErrors("For character " + c,
                        DOMError.SEVERITY_ERROR, "wf-invalid-character");
            } 
        } 
    } 
 
    public void testInvalidCharactersComment() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        Comment comment = document.createComment("");
        root.appendChild(comment);
 
        for (int c = 0; c <= Character.MAX_VALUE; c++) {
            comment.setData(new String(new char[] { 'A', 'B', (char) c}));
            document.normalizeDocument();
            if (isValid((char) c)) {
                assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
            } else { 
                errorRecorder.assertAllErrors("For character " + c,
                        DOMError.SEVERITY_ERROR, "wf-invalid-character");
            } 
        } 
    } 
 
    public void testInvalidCharactersProcessingInstructionData() throws Exception { 
        ErrorRecorder errorRecorder = new ErrorRecorder();
        domConfiguration.setParameter("error-handler", errorRecorder);
        domConfiguration.setParameter("namespaces", false);
        Element root = document.createElement("foo");
        document.appendChild(root);
        ProcessingInstruction pi = document.createProcessingInstruction("foo", "");
        root.appendChild(pi);
 
        for (int c = 0; c <= Character.MAX_VALUE; c++) {
            pi.setData(new String(new char[] { 'A', 'B', (char) c}));
            document.normalizeDocument();
            if (isValid((char) c)) {
                assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
            } else { 
                errorRecorder.assertAllErrors("For character " + c,
                        DOMError.SEVERITY_ERROR, "wf-invalid-character");
            } 
        } 
    } 
 
    // TODO: test for surrogates 
 
    private boolean isValid(char c) {
        // as defined by http://www.w3.org/TR/REC-xml/#charsets. 
        return c == 0x9 || c == 0xA || c == 0xD || (c >= 0x20 && c <= 0xd7ff)
                || (c >= 0xe000 && c <= 0xfffd);
    } 
 
    private Document createDocumentWithAdjacentTexts(String... texts) throws Exception {
        Document result = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().newDocument();
        Element root = result.createElement("foo");
        result.appendChild(root);
        for (String text : texts) {
            if (text.equals("<br>")) {
                root.appendChild(result.createElement("br"));
            } else { 
                root.appendChild(result.createTextNode(text));
            } 
        } 
        return result;
    } 
 
    private void assertChildren(Element element, String... texts) {
        List<String> actual = new ArrayList<String>();
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.TEXT_NODE) {
                actual.add(((Text) node).getData());
            } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
                actual.add("<![CDATA[" + ((CDATASection) node).getData() + "]]>");
            } else { 
                actual.add("<" + node.getNodeName() + ">");
            } 
        } 
        assertEquals(Arrays.asList(texts), actual);
    } 
 
    private void parse(String xml) throws Exception {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(xml)));
        domConfiguration = document.getDomConfig();
    } 
 
    private String domToString(Document document) throws TransformerException {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance() .newTransformer();
Connect your IDE to all the code out there  Get Codota for Java