001: package org.acm.seguin.pmd;
002:
003: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
004: import net.sourceforge.jrefactory.parser.JavaParserVisitorAdapter;
005:
006: import java.util.Iterator;
007: import java.util.List;
008:
009: public abstract class AbstractRule extends JavaParserVisitorAdapter
010: implements Rule {
011:
012: private String name = getClass().getName();
013: private RuleProperties properties = new RuleProperties();
014: private String message;
015: private String description;
016: private String example;
017: private boolean m_include;
018: private int m_priority = LOWEST_PRIORITY;
019:
020: public String getDescription() {
021: return description;
022: }
023:
024: public void setDescription(String description) {
025: this .description = description;
026: }
027:
028: public String getExample() {
029: return example;
030: }
031:
032: public void setExample(String example) {
033: this .example = example;
034: }
035:
036: public boolean hasProperty(String name) {
037: return properties.containsKey(name);
038: }
039:
040: public void addProperty(String name, String value) {
041: properties.setValue(name, value);
042: }
043:
044: public double getDoubleProperty(String name) {
045: return properties.getDoubleValue(name);
046: }
047:
048: public int getIntProperty(String name) {
049: return properties.getIntegerValue(name);
050: }
051:
052: public boolean getBooleanProperty(String name) {
053: return properties.getBooleanValue(name);
054: }
055:
056: public String getStringProperty(String name) {
057: return properties.getValue(name);
058: }
059:
060: public String getName() {
061: return name;
062: }
063:
064: public void setName(String name) {
065: this .name = name;
066: }
067:
068: public String getMessage() {
069: return message;
070: }
071:
072: public void setMessage(String message) {
073: this .message = message;
074: }
075:
076: public boolean equals(Object o) {
077: if (!(o instanceof Rule)) {
078: return false;
079: }
080: return ((Rule) o).getName().equals(getName());
081: }
082:
083: public int hashCode() {
084: return getName().hashCode();
085: }
086:
087: protected void visitAll(List acus, RuleContext ctx) {
088: for (Iterator i = acus.iterator(); i.hasNext();) {
089: ASTCompilationUnit node = (ASTCompilationUnit) i.next();
090: visit(node, ctx);
091: }
092: }
093:
094: public void apply(List acus, RuleContext ctx) {
095: visitAll(acus, ctx);
096: }
097:
098: public RuleViolation createRuleViolation(RuleContext ctx,
099: int lineNumber) {
100: return new RuleViolation(this , lineNumber, ctx);
101: }
102:
103: public RuleViolation createRuleViolation(RuleContext ctx,
104: int lineNumber, String specificDescription) {
105: return new RuleViolation(this , lineNumber, specificDescription,
106: ctx);
107: }
108:
109: /**
110: ********************************************************************************
111: *
112: * Gets an enumeration to enumerate through this rule's property names.
113: *
114: * @return An enumeration of property names
115: */
116: public RuleProperties getProperties() {
117: return properties;
118: }
119:
120: /**
121: *********************************************************************************
122: *
123: * When the rule is to be included in the analysis, returns true; otherwise, returns false.
124: *
125: * @return True when the rule is included in analysis.
126: */
127: public boolean include() {
128: return m_include;
129: }
130:
131: /**
132: *********************************************************************************
133: *
134: * When the rule is to be included in the analysis, set to true; otherwise, set to false.
135: *
136: * @param include True when the rule is included in analysis.
137: */
138: public void setInclude(boolean include) {
139: m_include = include;
140: }
141:
142: /**
143: *********************************************************************************
144: *
145: * Returns the rule's priority that is used for including the rule in reports and analysis.
146: *
147: * @return A number between 1 and LOWEST_PRIORITY.
148: */
149: public int getPriority() {
150: if ((m_priority < 0) || (m_priority > LOWEST_PRIORITY)) {
151: m_priority = LOWEST_PRIORITY;
152: }
153:
154: return m_priority;
155: }
156:
157: /**
158: *********************************************************************************
159: *
160: * Returns the rule's priority name that is used for including the rule in reports and analysis.
161: *
162: * @return A member of PRIORITIES.
163: */
164: public String getPriorityName() {
165: return PRIORITIES[getPriority() - 1];
166: }
167:
168: /**
169: *********************************************************************************
170: *
171: * A rule will specify a priority for inclusion in reports and analysis. The default
172: * priority is "Low".
173: *
174: * @param The rule's priority of 1..LOWEST_PRIORITY.
175: */
176: public void setPriority(int priority) {
177: if ((priority < 1) || (priority > LOWEST_PRIORITY)) {
178: m_priority = LOWEST_PRIORITY;
179: } else {
180: m_priority = priority;
181: }
182: }
183:
184: public Object visit(ASTCompilationUnit expr, Object data) {
185: super.visit(expr, data);
186: return data;
187: }
188: }
|