Search in sources :

Example 1 with XmlNode

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());
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute)

Example 2 with XmlNode

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");
}
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 3 with XmlNode

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);
    }
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode)

Example 4 with XmlNode

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);
    }
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode)

Example 5 with XmlNode

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());
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) XmlParserOptions(net.sourceforge.pmd.lang.xml.XmlParserOptions) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Parser(net.sourceforge.pmd.lang.Parser) Test(org.junit.Test)

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