001: package org.acm.seguin.pmd;
002:
003: import java.util.HashSet;
004: import java.util.Iterator;
005: import java.util.List;
006: import java.util.Set;
007:
008: public class RuleSet {
009: private Set rules = new HashSet();
010: private String name;
011: private String description;
012:
013: /**
014: * Indicates whether or not the rule set should be included in PMD's analysis.
015: * True to include the rule set; otherwise, false to exclude the rule set.
016: */
017: private boolean m_include;
018:
019: /**
020: * The name of the file the rule set is stored in, e.g., "basic_rules.xml". The user may
021: * change the rule set name; therefore, the rule set name cannot be used for a file name.
022: * This variable is set when the rule set is read.
023: */
024: private String m_fileName;
025:
026: public int size() {
027: return rules.size();
028: }
029:
030: public void addRule(Rule rule) {
031: rules.add(rule);
032: }
033:
034: public Set getRules() {
035: return rules;
036: }
037:
038: public Rule getRuleByName(String ruleName) {
039: for (Iterator i = rules.iterator(); i.hasNext();) {
040: Rule r = (Rule) i.next();
041: if (r.getName().equals(ruleName)) {
042: return r;
043: }
044: }
045: throw new RuntimeException("Couldn't find rule named "
046: + ruleName + " in the ruleset " + name);
047: }
048:
049: public void addRuleSet(RuleSet ruleSet) {
050: rules.addAll(ruleSet.getRules());
051: }
052:
053: public void apply(List acuList, RuleContext ctx) {
054: Iterator rs = rules.iterator();
055: while (rs.hasNext()) {
056: Rule rule = (Rule) rs.next();
057:
058: rule.apply(acuList, ctx);
059: }
060: }
061:
062: public String getName() {
063: return name;
064: }
065:
066: public void setName(String name) {
067: this .name = name;
068: }
069:
070: public String getDescription() {
071: return description;
072: }
073:
074: public void setDescription(String description) {
075: this .description = description;
076: }
077:
078: /**
079: * Returns true when the rule set is included in PMD's analysis; otherwise, false when
080: * it is excluded.
081: *
082: * @return True to include during analysis.
083: */
084: public boolean include() {
085: return m_include;
086: }
087:
088: /**
089: * Set to true when the rule set is included in PMD's analysis; otherwise, set to false
090: * when it is excluded.
091: *
092: * @param include True to include during analysis.
093: */
094: public void setInclude(boolean include) {
095: m_include = include;
096: }
097:
098: /**
099: * Get the name of the file the rule set is to be stored in, e.g., "basic_rules.xml".
100: *
101: * @return The name of the rule set file.
102: */
103: public String getFileName() {
104: if (m_fileName == null) {
105: m_fileName = name.toLowerCase().replace(' ', '_') + ".xml";
106: }
107:
108: return m_fileName;
109: }
110:
111: /**
112: * Set the name of the file the rule set is to be stored in, e.g., "basic_rules.xml".
113: *
114: * @param fileName The name of the rule set file.
115: */
116: public void setFileName(String fileName) {
117: if (fileName != null) {
118: fileName = fileName.trim();
119:
120: if (fileName.length() == 0) {
121: fileName = null;
122: }
123: }
124:
125: m_fileName = fileName;
126: }
127: }
|