Search in sources :

Example 11 with XmlNode

use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.

the class XmlParserTest method testParsingNotNamespaceAware.

/**
 * Verifies the default parsing behavior of the XML parser with namespaces
 * but not namespace aware.
 */
@Test
public void testParsingNotNamespaceAware() {
    LanguageVersionHandler xmlVersionHandler = LanguageRegistry.getLanguage(XmlLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
    XmlParserOptions parserOptions = new XmlParserOptions();
    parserOptions.setNamespaceAware(false);
    Parser parser = xmlVersionHandler.getParser(parserOptions);
    Node document = parser.parse(null, new StringReader(XML_NAMESPACE_TEST));
    assertNode(document, "document", 1);
    Node rootElement = document.jjtGetChild(0);
    assertNode(rootElement, "pmd:rootElement", 7, "xmlns:pmd", "http://pmd.sf.net");
    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.jjtGetChild(0), "\\n    ");
    assertNode(rootElement.jjtGetChild(1), "comment", 0);
    assertTextNode(rootElement.jjtGetChild(2), "\\n    ");
    Node child1 = rootElement.jjtGetChild(3);
    assertNode(child1, "pmd:child1", 1, "test", "1");
    assertTextNode(child1.jjtGetChild(0), "entity: &\\n    ");
    assertTextNode(rootElement.jjtGetChild(4), "\\n    ");
    Node child2 = rootElement.jjtGetChild(5);
    assertNode(child2, "pmd:child2", 3);
    assertTextNode(child2.jjtGetChild(0), "\\n      ");
    assertTextNode(child2.jjtGetChild(1), " cdata section ", "cdata-section");
    assertTextNode(child2.jjtGetChild(2), "\\n    ");
    assertTextNode(rootElement.jjtGetChild(6), "\\n");
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) Node(net.sourceforge.pmd.lang.ast.Node) XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) StringReader(java.io.StringReader) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) Parser(net.sourceforge.pmd.lang.Parser) XmlParser(net.sourceforge.pmd.lang.xml.ast.XmlParser) Test(org.junit.Test)

Example 12 with XmlNode

use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.

the class XmlParserTest method assertNode.

/**
 * Asserts a single node inclusive attributes.
 *
 * @param node
 *            the node
 * @param toString
 *            the to String representation to expect
 * @param childs
 *            number of childs
 * @param atts
 *            attributes - each object pair forms one attribute: first name,
 *            then value.
 */
private void assertNode(Node node, String toString, int childs, Object... atts) {
    Assert.assertEquals(toString, String.valueOf(node));
    Assert.assertEquals(childs, node.jjtGetNumChildren());
    Iterator<Attribute> attributeIterator = ((XmlNode) node).getAttributeIterator();
    if (atts != null) {
        for (int i = 0; i < atts.length; i += 2) {
            Assert.assertTrue(attributeIterator.hasNext());
            String name = String.valueOf(atts[i]);
            Object value = atts[i + 1];
            Attribute attribute = attributeIterator.next();
            Assert.assertEquals(name, attribute.getName());
            Assert.assertEquals(value, attribute.getValue());
        }
    }
    Assert.assertFalse(attributeIterator.hasNext());
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute)

Aggregations

XmlNode (net.sourceforge.pmd.lang.xml.ast.XmlNode)12 StringReader (java.io.StringReader)7 Parser (net.sourceforge.pmd.lang.Parser)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 XmlParserOptions (net.sourceforge.pmd.lang.xml.XmlParserOptions)5 Node (net.sourceforge.pmd.lang.ast.Node)3 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)2 Attribute (net.sourceforge.pmd.lang.ast.xpath.Attribute)2 XmlParser (net.sourceforge.pmd.lang.xml.ast.XmlParser)2 Node (org.w3c.dom.Node)1