Search in sources :

Example 6 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class XmlParserOptionsTest method testEqualsHashcode.

@Test
public void testEqualsHashcode() throws Exception {
    BooleanProperty[] properties = new BooleanProperty[] { XmlParserOptions.COALESCING_DESCRIPTOR, XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR, XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR, XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR, XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR, XmlParserOptions.VALIDATING_DESCRIPTOR, XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR };
    for (int i = 0; i < properties.length; i++) {
        BooleanProperty property = properties[i];
        MyRule rule = new MyRule();
        rule.setProperty(property, true);
        ParserOptions options1 = rule.getParserOptions();
        rule.setProperty(property, false);
        ParserOptions options2 = rule.getParserOptions();
        rule.setProperty(property, true);
        ParserOptions options3 = rule.getParserOptions();
        rule.setProperty(property, false);
        ParserOptions options4 = rule.getParserOptions();
        verifyOptionsEqualsHashcode(options1, options2, options3, options4);
    }
    XmlParserOptions options1 = new XmlParserOptions();
    options1.setSuppressMarker("foo");
    XmlParserOptions options2 = new XmlParserOptions();
    options2.setSuppressMarker("bar");
    XmlParserOptions options3 = new XmlParserOptions();
    options3.setSuppressMarker("foo");
    XmlParserOptions options4 = new XmlParserOptions();
    options4.setSuppressMarker("bar");
    verifyOptionsEqualsHashcode(options1, options2, options3, options4);
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) BooleanProperty(net.sourceforge.pmd.properties.BooleanProperty) Test(org.junit.Test)

Example 7 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class XPathRuleTest method testFollowingSibling.

/**
 * Following sibling check: See https://sourceforge.net/p/pmd/bugs/1209/
 *
 * @throws Exception
 *             any error
 */
@Test
public void testFollowingSibling() throws Exception {
    final String SOURCE = "public class dummy {\n" + "  public String toString() {\n" + "    String test = \"bad example\";\n" + "    test = \"a\";\n" + "    return test;\n" + "  }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SOURCE));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//Block/BlockStatement/following-sibling::BlockStatement";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Example 8 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class XPathRuleTest method testImageOfPrimarySuffix.

/**
 * Test for problem reported in bug #1219 PrimarySuffix/@Image does not work
 * in some cases in xpath 2.0
 *
 * @throws Exception
 *             any error
 */
@Test
public void testImageOfPrimarySuffix() throws Exception {
    final String SUFFIX = "import java.io.File;\n" + "\n" + "public class TestSuffix {\n" + "    public static void main(String args[]) {\n" + "        new File(\"subdirectory\").list();\n" + "    }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SUFFIX));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//PrimarySuffix[@Image='list']";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 1.0 Compatibility
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0_COMPATIBILITY);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Example 9 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class PMD method parserFor.

/**
 * Helper method to get a configured parser for the requested language. The
 * parser is configured based on the given {@link PMDConfiguration}.
 *
 * @param languageVersion
 *            the requested language
 * @param configuration
 *            the given configuration
 * @return the pre-configured parser
 */
public static Parser parserFor(LanguageVersion languageVersion, PMDConfiguration configuration) {
    // TODO Handle Rules having different parser options.
    LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
    ParserOptions options = languageVersionHandler.getDefaultParserOptions();
    if (configuration != null) {
        options.setSuppressMarker(configuration.getSuppressMarker());
    }
    return languageVersionHandler.getParser(options);
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

Aggregations

ParserOptions (net.sourceforge.pmd.lang.ParserOptions)9 Test (org.junit.Test)6 StringReader (java.io.StringReader)5 Parser (net.sourceforge.pmd.lang.Parser)5 RuleContext (net.sourceforge.pmd.RuleContext)4 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)4 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)3 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)2 JaxenXPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery)2 SaxonXPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery)2 XPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery)2 BooleanProperty (net.sourceforge.pmd.properties.BooleanProperty)2 PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)2 FileReader (java.io.FileReader)1 Java17Parser (net.sourceforge.pmd.lang.java.Java17Parser)1 ScopeAndDeclarationFinder (net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder)1