View Javadoc

1   package net.sourceforge.pmd;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.HashSet;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Set;
9   
10  /***
11   * Grouping of Rules per Language in a RuleSet.
12   *
13   * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
14   */
15  public class RuleSets {
16      /***
17       * Map of RuleLanguage on RuleSet.
18       */
19      private Collection ruleSets = new ArrayList();
20  
21      /***
22       * Public constructor.
23       */
24      public RuleSets() {
25      }
26  
27      /***
28       * Public constructor. Add the given rule set.
29       *
30       * @param ruleSet the RuleSet
31       */
32      public RuleSets(RuleSet ruleSet) {
33          this();
34          addRuleSet(ruleSet);
35      }
36  
37      /***
38       * Add a ruleset for a language. Only one ruleset can be added for a specific
39       * language. If ruleSet.getLanguage() is null, it is assumed to be a RuleSet of java
40       * rules.
41       *
42       * @param ruleSet the RuleSet
43       */
44      public void addRuleSet(RuleSet ruleSet) {
45          ruleSets.add(ruleSet);
46      }
47  
48      /***
49       * Get all the RuleSets.
50       *
51       * @return RuleSet[]
52       */
53      public RuleSet[] getAllRuleSets() {
54          return (RuleSet[]) ruleSets.toArray(new RuleSet[0]);
55      }
56  
57      /***
58       * Return all rules from all rulesets.
59       *
60       * @return Set
61       */
62      public Set getAllRules() {
63          HashSet result = new HashSet();
64          for (Iterator i = ruleSets.iterator(); i.hasNext();) {
65              result.addAll(((RuleSet) i.next()).getRules());
66          }
67          return result;
68      }
69  
70      /***
71       * Check if a source with given language should be checked by rules for a given
72       * language. This is the case if both languages are equal, or if the source is in
73       * java, and the language of the rules is unknown (for backward-compatibility
74       * reasons).
75       *
76       * @param languageOfSource language of a source; can not be null
77       * @param languageOfRule   language of a ruleset; can be null
78       * @return
79       */
80      public boolean applies(Language languageOfSource, Language languageOfRule) {
81          return (languageOfSource.equals(languageOfRule) || (languageOfSource
82                  .equals(Language.JAVA) && (null == languageOfRule)));
83      }
84  
85      /***
86       * Apply all applicable rules to the compilation units.
87       * Applicable means the language of the rules must match the language
88       * of the source (@see applies).
89       *
90       * @param acuList  the List of compilation units; the type these must have,
91       *                 depends on the source language
92       * @param ctx      the RuleContext
93       * @param language the Language of the source
94       */
95      public void apply(List acuList, RuleContext ctx, Language language) {
96          for (Iterator i = ruleSets.iterator(); i.hasNext();) {
97              RuleSet ruleSet = (RuleSet) i.next();
98              if (applies(language, ruleSet.getLanguage())) {
99                  ruleSet.apply(acuList, ctx);
100             }
101         }
102     }
103 
104     /***
105      * Check if the rules that apply to a source of the given language
106      * use DFA.
107      *
108      * @param language the language of a source
109      * @return true if any rule in the RuleSet needs the DFA layer
110      */
111     public boolean usesDFA(Language language) {
112         for (Iterator i = ruleSets.iterator(); i.hasNext();) {
113             RuleSet ruleSet = (RuleSet) i.next();
114             if (applies(language, ruleSet.getLanguage()) && ruleSet.usesDFA()) {
115                 return true;
116             }
117         }
118         return false;
119     }
120 
121     /***
122      * Returns the Rule with the given name
123      *
124      * @param ruleName the name of the rule to find
125      * @return the rule or null if not found
126      */
127     public Rule getRuleByName(String ruleName) {
128         Rule rule = null;
129         for (Iterator i = ruleSets.iterator(); i.hasNext() && (rule == null);) {
130             RuleSet ruleSet = (RuleSet) i.next();
131             rule = ruleSet.getRuleByName(ruleName);
132         }
133         return rule;
134     }
135 }