Search in sources :

Example 1 with XmlParserOptions

use of net.sourceforge.pmd.lang.xml.XmlParserOptions 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)

Example 2 with XmlParserOptions

use of net.sourceforge.pmd.lang.xml.XmlParserOptions 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"));
}
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)

Example 3 with XmlParserOptions

use of net.sourceforge.pmd.lang.xml.XmlParserOptions 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"));
}
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)

Example 4 with XmlParserOptions

use of net.sourceforge.pmd.lang.xml.XmlParserOptions 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!]]>&gt;&entity;&lt;</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());
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) XmlParserOptions(net.sourceforge.pmd.lang.xml.XmlParserOptions) Node(net.sourceforge.pmd.lang.ast.Node) XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Parser(net.sourceforge.pmd.lang.Parser) Test(org.junit.Test)

Example 5 with XmlParserOptions

use of net.sourceforge.pmd.lang.xml.XmlParserOptions 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());
}
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

StringReader (java.io.StringReader)5 ArrayList (java.util.ArrayList)5 Parser (net.sourceforge.pmd.lang.Parser)5 XmlParserOptions (net.sourceforge.pmd.lang.xml.XmlParserOptions)5 XmlNode (net.sourceforge.pmd.lang.xml.ast.XmlNode)5 Test (org.junit.Test)5 Node (net.sourceforge.pmd.lang.ast.Node)1