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");
}
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());
}
Aggregations