AbstractXPathRuleQuery.java
01 package net.sourceforge.pmd.lang.rule.xpath;
02 
03 import java.util.ArrayList;
04 import java.util.List;
05 import java.util.Map;
06 
07 import net.sourceforge.pmd.PropertyDescriptor;
08 import net.sourceforge.pmd.RuleContext;
09 import net.sourceforge.pmd.lang.ast.Node;
10 
11 /**
12  * This implementation of XPathRuleQuery provides support for RuleChain visits.
13  */
14 public abstract class AbstractXPathRuleQuery implements XPathRuleQuery {
15 
16     /**
17      * The XPath query string.
18      */
19     protected String xpath;
20 
21     /**
22      * The XPath version;
23      */
24     protected String version;
25 
26     /**
27      * The properties.
28      */
29     protected Map<PropertyDescriptor<?>, Object> properties;
30 
31     /**
32      * Subclasses can manage RuleChain visits via this list.
33      */
34     protected final List<String> ruleChainVisits = new ArrayList<String>();
35 
36     /**
37      * {@inheritDoc}
38      */
39     public void setXPath(String xpath) {
40   this.xpath = xpath;
41     }
42 
43     /**
44      * {@inheritDoc}
45      */
46     public void setVersion(String versionthrows UnsupportedOperationException {
47   if (!isSupportedVersion(version)) {
48       throw new UnsupportedOperationException(this.getClass().getSimpleName()
49         " does not support XPath version: " + version);
50   }
51   this.version = version;
52     }
53 
54     /**
55      * Subclasses should implement to indicate whether an XPath version is supported.
56      @param version The XPath version.
57      @return <code>true</code> if the XPath version is supported, <code>false</code> otherwise.
58      */
59     protected abstract boolean isSupportedVersion(String version);
60 
61     /**
62      * {@inheritDoc}
63      */
64     public void setProperties(Map<PropertyDescriptor<?>, Object> properties) {
65   this.properties = properties;
66     }
67 
68     /**
69      * {@inheritDoc}
70      */
71     public List<String> getRuleChainVisits() {
72   return ruleChainVisits;
73     }
74 
75     /**
76      * {@inheritDoc}
77      */
78     public abstract List<Node> evaluate(Node node, RuleContext data);
79 }