use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractDomXmlRuleTest method xsdIsNotLookedUp.
@Test
public void xsdIsNotLookedUp() {
String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + "<web-app xmlns=\"http://java.sun.com/xml/ns/javaee\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.inexisting.com/xml/ns/javaee/web-app_2_5.xsd\" " + "version=\"2.5\">" + "</web-app>";
XmlParserOptions parserOptions = new XmlParserOptions();
Parser parser = LanguageRegistry.getLanguage(XmlLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getParser(parserOptions);
XmlNode xmlNode = (XmlNode) parser.parse(null, new StringReader(source));
// no exception should be thrown
// first element is still parsed
MyRule rule = new MyRule();
List<XmlNode> nodes = new ArrayList<>();
nodes.add(xmlNode);
rule.apply(nodes, null);
assertNotNull(rule.visitedNodes.get("Element"));
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractDomXmlRuleTest method dtdIsNotLookedUp.
@Test
public void dtdIsNotLookedUp() {
String source = "<!DOCTYPE struts-config PUBLIC " + " \"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN \" " + " \"http://jakarta.inexistinghost.org/struts/dtds/struts-config_1_1.dtd\" >" + "<struts-config/>";
XmlParserOptions parserOptions = new XmlParserOptions();
parserOptions.setLookupDescriptorDoc(false);
Parser parser = LanguageRegistry.getLanguage(XmlLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getParser(parserOptions);
XmlNode xmlNode = (XmlNode) parser.parse(null, new StringReader(source));
// no exception should be thrown
MyRule rule = new MyRule();
List<XmlNode> nodes = new ArrayList<>();
nodes.add(xmlNode);
rule.apply(nodes, null);
// first element is still parsed
assertNotNull(rule.visitedNodes.get("Element"));
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractDomXmlRuleTest method testVisit.
@Test
public void testVisit() throws Exception {
String source = "<?xml version=\"1.0\"?><?mypi?><!DOCTYPE testDoc [<!ENTITY entity \"e\">]><!--Comment--><foo abc=\"abc\"><bar>TEXT</bar><![CDATA[cdata!]]>>&entity;<</foo>";
XmlParserOptions parserOptions = new XmlParserOptions();
parserOptions.setExpandEntityReferences(false);
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);
List<org.w3c.dom.Node> visited = rule.visitedNodes.get("Attr");
assertEquals(1, visited.size());
assertEquals("abc", visited.get(0).getLocalName());
visited = rule.visitedNodes.get("CharacterData");
assertEquals(1, visited.size());
assertEquals("cdata!", ((CharacterData) visited.get(0)).getData());
visited = rule.visitedNodes.get("Comment");
assertEquals("Comment", ((Comment) visited.get(0)).getData());
visited = rule.visitedNodes.get("Document");
assertEquals(1, visited.size());
visited = rule.visitedNodes.get("DocumentType");
assertEquals("testDoc", ((DocumentType) visited.get(0)).getName());
visited = rule.visitedNodes.get("Element");
assertEquals(2, visited.size());
assertEquals("foo", visited.get(0).getLocalName());
assertEquals("bar", visited.get(1).getLocalName());
// TODO Figure out how to trigger this.
// visited = rule.visitedNodes.get("Entity");
// assertEquals(0, visited.size());
visited = rule.visitedNodes.get("EntityReference");
assertEquals(1, visited.size());
assertEquals("entity", ((EntityReference) visited.get(0)).getNodeName());
// TODO Figure out how to trigger this.
// visited = rule.visitedNodes.get("Notation");
// assertEquals(0, visited.size());
visited = rule.visitedNodes.get("ProcessingInstruction");
assertEquals(1, visited.size());
assertEquals("mypi", ((ProcessingInstruction) visited.get(0)).getTarget());
visited = rule.visitedNodes.get("Text");
assertEquals(3, visited.size());
assertEquals("TEXT", ((Text) visited.get(0)).getData());
assertEquals(">", ((Text) visited.get(1)).getData());
assertEquals("e<", ((Text) visited.get(2)).getData());
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractDomXmlRule method visit.
@Override
protected void visit(XmlNode node, RuleContext ctx) {
final Node domNode = node.getNode();
// Visit the node
visitDomNode(node, domNode, ctx);
// Visit attributes
visitAttributeNodes(node, domNode, ctx);
}
use of net.sourceforge.pmd.lang.xml.ast.XmlNode in project pmd by pmd.
the class AbstractWsdlRuleTest 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(WsdlLanguageModule.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