XmlParser extends AbstractParser { @Override public Node parse(String fileName, Reader source) throws ParseException { return new net.sourceforge.pmd.lang.xml.ast.XmlParser((XmlParserOptions) parserOptions).parse(source); } XmlParser(ParserOptions parserOptions); @Override TokenManager createTokenManager(Reader source); @Override boolean canParse(); @Override Node parse(String fileName, Reader source); @Override Map<Integer, String> getSuppressMap();  }
@Test public void testLineNumbers() { Node document = XML.parse(XML_TEST); assertNode(document, "document", 2); assertLineNumbers(document, 1, 1, 19, 14); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); assertLineNumbers(dtdElement, 2, 1, 11, 1); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 7); assertLineNumbers(rootElement, 12, 1, 19, 14); assertTextNode(rootElement.getChild(0), "\\n "); assertLineNumbers(rootElement.getChild(0), 12, 14, 13, 4); assertNode(rootElement.getChild(1), "comment", 0); assertLineNumbers(rootElement.getChild(1), 13, 5, 13, 29); assertTextNode(rootElement.getChild(2), "\\n "); assertLineNumbers(rootElement.getChild(2), 13, 30, 14, 4); Node child1 = rootElement.getChild(3); assertNode(child1, "child1", 1, "test", "1"); assertLineNumbers(child1, 14, 5, 15, 13); assertTextNode(child1.getChild(0), "entity: Copyright: PMD\\n "); assertLineNumbers(child1.getChild(0), 14, 22, 15, 4); assertTextNode(rootElement.getChild(4), "\\n "); assertLineNumbers(rootElement.getChild(4), 15, 14, 16, 4); Node child2 = rootElement.getChild(5); assertNode(child2, "child2", 3); assertLineNumbers(child2, 16, 5, 18, 13); assertTextNode(child2.getChild(0), "\\n "); assertLineNumbers(child2.getChild(0), 16, 13, 17, 6); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertLineNumbers(child2.getChild(1), 17, 7, 17, 33); assertTextNode(child2.getChild(2), "\\n "); assertLineNumbers(child2.getChild(2), 17, 34, 18, 4); assertTextNode(rootElement.getChild(6), "\\n"); assertLineNumbers(rootElement.getChild(6), 18, 14, 18, 14); }
@Test public void testAutoclosingElementLength() { final String xml = "<elementName att1='foo' att2='bar' att3='other' />"; assertLineNumbers(XML.parse(xml), 1, 1, 1, xml.length()); }
@Test public void testDefaultParsing() { Node document = XML.parse(XML_TEST); assertNode(document, "document", 2); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 7); assertTextNode(rootElement.getChild(0), "\\n "); assertNode(rootElement.getChild(1), "comment", 0); assertTextNode(rootElement.getChild(2), "\\n "); Node child1 = rootElement.getChild(3); assertNode(child1, "child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: Copyright: PMD\\n "); assertTextNode(rootElement.getChild(4), "\\n "); Node child2 = rootElement.getChild(5); assertNode(child2, "child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); assertTextNode(rootElement.getChild(6), "\\n"); }
@Test public void testParsingCoalescingEnabled() { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setCoalescing(true); Node document = XML.withParserOptions(parserOptions).parse(XML_TEST); assertNode(document, "document", 2); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 7); assertTextNode(rootElement.getChild(0), "\\n "); assertNode(rootElement.getChild(1), "comment", 0); assertTextNode(rootElement.getChild(2), "\\n "); Node child1 = rootElement.getChild(3); assertNode(child1, "child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: Copyright: PMD\\n "); assertTextNode(rootElement.getChild(4), "\\n "); Node child2 = rootElement.getChild(5); assertNode(child2, "child2", 1); assertTextNode(child2.getChild(0), "\\n cdata section \\n "); assertTextNode(rootElement.getChild(6), "\\n"); }
@Test public void testParsingDoNotExpandEntities() { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setExpandEntityReferences(false); Node document = XML.withParserOptions(parserOptions).parse(XML_TEST); assertNode(document, "document", 2); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 7); assertTextNode(rootElement.getChild(0), "\\n "); assertNode(rootElement.getChild(1), "comment", 0); assertTextNode(rootElement.getChild(2), "\\n "); Node child1 = rootElement.getChild(3); assertNode(child1, "child1", 3, "test", "1"); assertTextNode(child1.getChild(0), "entity: "); assertNode(child1.getChild(1), "pmd", 0); String text = child1.getChild(2).getImage(); if ("\n ".equals(text)) { assertTextNode(child1.getChild(2), "\\n "); } else { assertTextNode(child1.getChild(2), "Copyright: PMD\\n "); } assertTextNode(rootElement.getChild(4), "\\n "); Node child2 = rootElement.getChild(5); assertNode(child2, "child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); assertTextNode(rootElement.getChild(6), "\\n"); }
@Test public void testParsingIgnoreComments() { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setIgnoringComments(true); Node document = XML.withParserOptions(parserOptions).parse(XML_TEST); assertNode(document, "document", 2); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 5); assertTextNode(rootElement.getChild(0), "\\n \\n "); Node child1 = rootElement.getChild(1); assertNode(child1, "child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: Copyright: PMD\\n "); assertTextNode(rootElement.getChild(2), "\\n "); Node child2 = rootElement.getChild(3); assertNode(child2, "child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); assertTextNode(rootElement.getChild(4), "\\n"); }
@Test public void testParsingIgnoreElementContentWhitespace() { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setIgnoringElementContentWhitespace(true); Node document = XML.withParserOptions(parserOptions).parse(XML_TEST); assertNode(document, "document", 2); Node dtdElement = document.getChild(0); assertNode(dtdElement, "rootElement", 0); Node rootElement = document.getChild(1); assertNode(rootElement, "rootElement", 3); assertNode(rootElement.getChild(0), "comment", 0); Node child1 = rootElement.getChild(1); assertNode(child1, "child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: Copyright: PMD\\n "); Node child2 = rootElement.getChild(2); assertNode(child2, "child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); }
@Test public void testDefaultParsingNamespaces() { Node document = XML.parse(XML_NAMESPACE_TEST); assertNode(document, "document", 1); Node rootElement = document.getChild(0); assertNode(rootElement, "pmd:rootElement", 7, "xmlns:pmd", "http: Assert.assertEquals("http: Assert.assertEquals("pmd", ((XmlNode) rootElement).getNode().getPrefix()); Assert.assertEquals("rootElement", ((XmlNode) rootElement).getNode().getLocalName()); Assert.assertEquals("pmd:rootElement", ((XmlNode) rootElement).getNode().getNodeName()); assertTextNode(rootElement.getChild(0), "\\n "); assertNode(rootElement.getChild(1), "comment", 0); assertTextNode(rootElement.getChild(2), "\\n "); Node child1 = rootElement.getChild(3); assertNode(child1, "pmd:child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: &\\n "); assertTextNode(rootElement.getChild(4), "\\n "); Node child2 = rootElement.getChild(5); assertNode(child2, "pmd:child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); assertTextNode(rootElement.getChild(6), "\\n"); }
@Test public void testParsingNotNamespaceAware() { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setNamespaceAware(false); Node document = XML.withParserOptions(parserOptions).parse(XML_NAMESPACE_TEST); assertNode(document, "document", 1); Node rootElement = document.getChild(0); assertNode(rootElement, "pmd:rootElement", 7, "xmlns:pmd", "http: Assert.assertNull(((XmlNode) rootElement).getNode().getNamespaceURI()); Assert.assertNull(((XmlNode) rootElement).getNode().getPrefix()); Assert.assertNull(((XmlNode) rootElement).getNode().getLocalName()); Assert.assertEquals("pmd:rootElement", ((XmlNode) rootElement).getNode().getNodeName()); assertTextNode(rootElement.getChild(0), "\\n "); assertNode(rootElement.getChild(1), "comment", 0); assertTextNode(rootElement.getChild(2), "\\n "); Node child1 = rootElement.getChild(3); assertNode(child1, "pmd:child1", 1, "test", "1"); assertTextNode(child1.getChild(0), "entity: &\\n "); assertTextNode(rootElement.getChild(4), "\\n "); Node child2 = rootElement.getChild(5); assertNode(child2, "pmd:child2", 3); assertTextNode(child2.getChild(0), "\\n "); assertTextNode(child2.getChild(1), " cdata section ", "cdata-section"); assertTextNode(child2.getChild(2), "\\n "); assertTextNode(rootElement.getChild(6), "\\n"); }
@Test public void testParsingWithValidation() throws UnsupportedEncodingException { XmlParserOptions parserOptions = new XmlParserOptions(); parserOptions.setValidating(true); PrintStream oldErr = System.err; Locale oldLocale = Locale.getDefault(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); System.setErr(new PrintStream(bos)); Locale.setDefault(Locale.ENGLISH); Node document = XML.withParserOptions(parserOptions).parse(XML_INVALID_WITH_DTD); Assert.assertNotNull(document); String output = bos.toString("UTF-8"); Assert.assertTrue(output.contains("Element type \"invalidChild\" must be declared.")); Assert.assertTrue(output.contains("The content of element type \"rootElement\" must match \"(child)\".")); Assert.assertEquals(2, document.getNumChildren()); Assert.assertEquals("invalidChild", String.valueOf(document.getChild(1).getChild(1))); } finally { System.setErr(oldErr); Locale.setDefault(oldLocale); } }
@Test public void testWithProcessingInstructions() { String xml = "<?xml version=\"1.0\"?><?mypi?><!DOCTYPE testDoc [<!ENTITY myentity \"e\">]><!--Comment--><foo abc=\"abc\"><bar>TEXT</bar><![CDATA[cdata!]]>&gt;&myentity;&lt;</foo>"; LanguageVersionHandler xmlVersionHandler = LanguageRegistry.getLanguage(XmlLanguageModule.NAME) .getDefaultVersion().getLanguageVersionHandler(); XmlParserOptions options = (XmlParserOptions) xmlVersionHandler.getDefaultParserOptions(); options.setExpandEntityReferences(false); Parser parser = xmlVersionHandler.getParser(options); Node document = parser.parse(null, new StringReader(xml)); Assert.assertNotNull(document); assertNode(document.getChild(0), "mypi", 0); assertLineNumbers(document.getChild(0), 1, 22, 1, 29); }