1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd; 5 6 import java.util.HashSet; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Set; 10 11 public class RuleSet { 12 private Set rules = new HashSet(); 13 private String name; 14 private String description; 15 16 /*** 17 * Indicates whether or not the rule set should be included in PMD's analysis. 18 * True to include the rule set; otherwise, false to exclude the rule set. 19 */ 20 private boolean m_include; 21 22 /*** 23 * The name of the file the rule set is stored in, e.g., "basic_rules.xml". The user may 24 * change the rule set name; therefore, the rule set name cannot be used for a file name. 25 * This variable is set when the rule set is read. 26 */ 27 private String m_fileName; 28 29 public int size() { 30 return rules.size(); 31 } 32 33 public void addRule(Rule rule) { 34 rules.add(rule); 35 } 36 37 public Set getRules() { 38 return rules; 39 } 40 41 public Rule getRuleByName(String ruleName) { 42 for (Iterator i = rules.iterator(); i.hasNext();) { 43 Rule r = (Rule) i.next(); 44 if (r.getName().equals(ruleName)) { 45 return r; 46 } 47 } 48 throw new RuntimeException("Couldn't find rule named " + ruleName + " in the ruleset " + name); 49 } 50 51 public void addRuleSet(RuleSet ruleSet) { 52 rules.addAll(ruleSet.getRules()); 53 } 54 55 public void apply(List acuList, RuleContext ctx) { 56 Iterator rs = rules.iterator(); 57 while (rs.hasNext()) { 58 Rule rule = (Rule) rs.next(); 59 60 rule.apply(acuList, ctx); 61 } 62 } 63 64 public String getName() { 65 return name; 66 } 67 68 public void setName(String name) { 69 this.name = name; 70 } 71 72 public String getDescription() { 73 return description; 74 } 75 76 public void setDescription(String description) { 77 this.description = description; 78 } 79 80 /*** 81 * Returns true when the rule set is included in PMD's analysis; otherwise, false when 82 * it is excluded. 83 * 84 * @return True to include during analysis. 85 */ 86 public boolean include() { 87 return m_include; 88 } 89 90 /*** 91 * Set to true when the rule set is included in PMD's analysis; otherwise, set to false 92 * when it is excluded. 93 * 94 * @param include True to include during analysis. 95 */ 96 public void setInclude(boolean include) { 97 m_include = include; 98 } 99 100 /*** 101 * Get the name of the file the rule set is to be stored in, e.g., "basic_rules.xml". 102 * 103 * @return The name of the rule set file. 104 */ 105 public String getFileName() { 106 if (m_fileName == null) { 107 m_fileName = name.toLowerCase().replace(' ', '_') + ".xml"; 108 } 109 110 return m_fileName; 111 } 112 113 /*** 114 * Set the name of the file the rule set is to be stored in, e.g., "basic_rules.xml". 115 * 116 * @param fileName The name of the rule set file. 117 */ 118 public void setFileName(String fileName) { 119 if (fileName != null) { 120 fileName = fileName.trim(); 121 122 if (fileName.length() == 0) { 123 fileName = null; 124 } 125 } 126 127 m_fileName = fileName; 128 } 129 }