use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class XmlParserTest method assertTextNode.
/**
* Assert a single text node.
*
* @param node
* the node to check
* @param text
* the text to expect
* @param toString
* the to string representation
*/
private void assertTextNode(Node node, String text, String toString) {
Assert.assertEquals(toString, String.valueOf(node));
Assert.assertEquals(0, node.jjtGetNumChildren());
Assert.assertEquals(text, StringUtil.escapeWhitespace(node.getImage()));
Iterator<Attribute> attributeIterator = ((XmlNode) node).getAttributeIterator();
Assert.assertTrue(attributeIterator.hasNext());
Attribute attribute = attributeIterator.next();
Assert.assertEquals("Image", attribute.getName());
Assert.assertEquals(text, StringUtil.escapeWhitespace(attribute.getValue()));
Assert.assertFalse(attributeIterator.hasNext());
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class XmlParserTest method testDefaultParsingNamespaces.
/**
* Verifies the default parsing behavior of the XML parser with namespaces.
*/
@Test
public void testDefaultParsingNamespaces() {
LanguageVersionHandler xmlVersionHandler = LanguageRegistry.getLanguage(XmlLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
Parser parser = xmlVersionHandler.getParser(xmlVersionHandler.getDefaultParserOptions());
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.assertEquals("http://pmd.sf.net", ((XmlNode) rootElement).getNode().getNamespaceURI());
Assert.assertEquals("pmd", ((XmlNode) rootElement).getNode().getPrefix());
Assert.assertEquals("rootElement", ((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 AbstractXmlRule method visitAll.
protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
for (Object element : nodes) {
XmlNode node = (XmlNode) element;
visit(node, ctx);
}
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractXmlRule method visit.
protected void visit(XmlNode node, RuleContext ctx) {
final int numChildren = node.jjtGetNumChildren();
for (int i = 0; i < numChildren; i++) {
XmlNode child = (XmlNode) node.jjtGetChild(i);
visit(child, ctx);
}
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractXmlRuleTest method testVisit.
@Test
public void testVisit() throws Exception {
String source = "<?xml version=\"1.0\"?><foo abc=\"abc\"><bar/></foo>";
XmlParserOptions parserOptions = new XmlParserOptions();
Parser parser = LanguageRegistry.getLanguage(XmlLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getParser(parserOptions);
XmlNode xmlNode = (XmlNode) parser.parse(null, new StringReader(source));
List<XmlNode> nodes = new ArrayList<>();
nodes.add(xmlNode);
MyRule rule = new MyRule();
rule.apply(nodes, null);
assertEquals(3, rule.visitedNodes.size());
assertEquals("document", rule.visitedNodes.get(0).toString());
assertEquals("foo", rule.visitedNodes.get(1).toString());
assertEquals("bar", rule.visitedNodes.get(2).toString());
}
Aggregations