View Javadoc

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