View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package net.sourceforge.pmd;
5   
6   import net.sourceforge.pmd.ast.ASTCompilationUnit;
7   import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
8   
9   import java.util.Iterator;
10  import java.util.List;
11  import java.util.Properties;
12  
13  public abstract class AbstractRule extends JavaParserVisitorAdapter implements Rule {
14  
15      private String name = getClass().getName();
16      private Properties properties = new Properties();
17      private String message;
18      private String description;
19      private String example;
20      private boolean m_include;
21      private int m_priority = LOWEST_PRIORITY;
22  
23      public String getDescription() {
24          return description;
25      }
26  
27      public void setDescription(String description) {
28          this.description = description;
29      }
30  
31      public String getExample() {
32          return example;
33      }
34  
35      public void setExample(String example) {
36          this.example = example;
37      }
38  
39      public boolean hasProperty(String name) {
40          return properties.containsKey(name);
41      }
42  
43      public void addProperty(String name, String value) {
44          properties.setProperty(name, value);
45      }
46  
47      public double getDoubleProperty(String name) {
48          return Double.parseDouble(properties.getProperty(name));
49      }
50  
51      public int getIntProperty(String name) {
52          return Integer.parseInt(properties.getProperty(name));
53      }
54  
55      public boolean getBooleanProperty(String name) {
56          return Boolean.valueOf(properties.getProperty(name)).booleanValue();
57      }
58  
59      public String getStringProperty(String name) {
60          return properties.getProperty(name);
61      }
62  
63      public String getName() {
64          return name;
65      }
66  
67      public void setName(String name) {
68          this.name = name;
69      }
70  
71      public String getMessage() {
72          return message;
73      }
74  
75      public void setMessage(String message) {
76          this.message = message;
77      }
78  
79      public boolean equals(Object o) {
80          if (!(o instanceof Rule)) {
81              return false;
82          }
83          return ((Rule)o).getName().equals(getName());
84      }
85  
86      public int hashCode() {
87          return getName().hashCode();
88      }
89  
90      protected void visitAll(List acus, RuleContext ctx) {
91          for (Iterator i = acus.iterator(); i.hasNext();) {
92              ASTCompilationUnit node = (ASTCompilationUnit) i.next();
93              visit(node, ctx);
94          }
95      }
96  
97      public void apply(List acus, RuleContext ctx) {
98          visitAll(acus, ctx);
99      }
100 
101     public RuleViolation createRuleViolation(RuleContext ctx, int lineNumber) {
102         return new RuleViolation(this, lineNumber, ctx);
103     }
104 
105     public RuleViolation createRuleViolation(RuleContext ctx, int lineNumber, String specificDescription) {
106         return new RuleViolation(this, lineNumber, specificDescription, 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 Properties 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 }