1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.jsp.rules;
5
6 import net.sourceforge.pmd.Rule;
7 import net.sourceforge.pmd.RuleContext;
8 import net.sourceforge.pmd.RuleViolation;
9 import net.sourceforge.pmd.ast.Node;
10 import net.sourceforge.pmd.jsp.ast.JspParserVisitorAdapter;
11 import net.sourceforge.pmd.jsp.ast.SimpleNode;
12
13 import java.text.MessageFormat;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Properties;
17
18 public abstract class AbstractJspRule extends JspParserVisitorAdapter implements Rule {
19
20 protected String name = getClass().getName();
21 protected Properties properties = new Properties();
22 protected String message;
23 protected String description;
24 protected String example;
25 protected String ruleSetName;
26 protected boolean include;
27 protected boolean usesDFA;
28 protected int priority = LOWEST_PRIORITY;
29 protected String externalInfoUrl;
30
31 public String getRuleSetName() {
32 return ruleSetName;
33 }
34
35 public void setRuleSetName(String ruleSetName) {
36 this.ruleSetName = ruleSetName;
37 }
38
39 public String getDescription() {
40 return description;
41 }
42
43 public void setDescription(String description) {
44 this.description = description;
45 }
46
47 public String getExample() {
48 return example;
49 }
50
51 public void setExample(String example) {
52 this.example = example;
53 }
54
55 public boolean hasProperty(String name) {
56 return properties.containsKey(name);
57 }
58
59 public void addProperty(String name, String value) {
60 properties.setProperty(name, value);
61 }
62
63 public void addProperties(Properties properties) {
64 this.properties.putAll(properties);
65 }
66
67 public double getDoubleProperty(String name) {
68 return Double.parseDouble(properties.getProperty(name));
69 }
70
71 public int getIntProperty(String name) {
72 return Integer.parseInt(properties.getProperty(name));
73 }
74
75 public boolean getBooleanProperty(String name) {
76 return Boolean.valueOf(properties.getProperty(name)).booleanValue();
77 }
78
79 public String getStringProperty(String name) {
80 return properties.getProperty(name);
81 }
82
83 public String getName() {
84 return name;
85 }
86
87 public void setName(String name) {
88 this.name = name;
89 }
90
91 public String getMessage() {
92 return message;
93 }
94
95 public void setMessage(String message) {
96 this.message = message;
97 }
98
99 public String getExternalInfoUrl() {
100 return externalInfoUrl;
101 }
102
103 public void setExternalInfoUrl(String url) {
104 this.externalInfoUrl = url;
105 }
106
107 /***
108 * Test if rules are equals. Rules are equals if
109 * 1. they have the same implementation class
110 * 2. they have the same name
111 * 3. they have the same priority
112 * 4. they share the same properties/values
113 */
114 public boolean equals(Object o) {
115 if (o == null) {
116 return false;
117 }
118
119 if (this == o) {
120 return true;
121 }
122
123 Rule rule = null;
124 boolean equality = this.getClass().getName().equals(o.getClass().getName());
125
126 if (equality) {
127 rule = (Rule) o;
128 equality = this.getName().equals(rule.getName())
129 && this.getPriority() == rule.getPriority()
130 && this.getProperties().equals(rule.getProperties());
131 }
132
133 return equality;
134 }
135
136 /***
137 * Return a hash code to conform to equality. Try with a string.
138 */
139 public int hashCode() {
140 String s = this.getClass().getName() + this.getName() + String.valueOf(this.getPriority()) + this.getProperties().toString();
141 return s.hashCode();
142 }
143
144 public void apply(List acus, RuleContext ctx) {
145 visitAll(acus, ctx);
146 }
147
148
149 public Properties getProperties() {
150 return properties;
151 }
152
153 public boolean include() {
154 return include;
155 }
156
157 public void setInclude(boolean include) {
158 this.include = include;
159 }
160
161 public int getPriority() {
162 return priority;
163 }
164
165 public String getPriorityName() {
166 return PRIORITIES[getPriority() - 1];
167 }
168
169 public void setPriority(int priority) {
170 this.priority = priority;
171 }
172
173 public void setUsesDFA() {
174 this.usesDFA = true;
175 }
176
177 public boolean usesDFA() {
178 return this.usesDFA;
179 }
180
181 protected void visitAll(List acus, RuleContext ctx) {
182 for (Iterator i = acus.iterator(); i.hasNext();) {
183 SimpleNode node = (SimpleNode) i.next();
184 visit(node, ctx);
185 }
186 }
187
188 /***
189 * Adds a violation to the report.
190 *
191 * @param ctx the RuleContext
192 * @param node the node that produces the violation
193 */
194 protected final void addViolation(Object data, SimpleNode node) {
195 RuleContext ctx = (RuleContext) data;
196 ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node));
197 }
198
199 /***
200 * Adds a violation to the report.
201 *
202 * @param ctx the RuleContext
203 * @param node the node that produces the violation
204 * @param msg specific message to put in the report
205 */
206 protected final void addViolationWithMessage(Object data, SimpleNode node, String msg) {
207 RuleContext ctx = (RuleContext) data;
208 ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, msg));
209 }
210
211 /***
212 * Adds a violation to the report.
213 *
214 * @param ctx the RuleContext
215 * @param node the node that produces the violation
216 * @param embed a variable to embed in the rule violation message
217 */
218 protected final void addViolation(Object data, SimpleNode node, String embed) {
219 RuleContext ctx = (RuleContext) data;
220 ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, MessageFormat.format(getMessage(), new Object[]{embed})));
221 }
222
223 /***
224 * Adds a violation to the report.
225 *
226 * @param ctx the RuleContext
227 * @param node the node that produces the violation, may be null, in which case all line and column info will be set to zero
228 * @param args objects to embed in the rule violation message
229 */
230 protected final void addViolation(Object data, Node node, Object[] args) {
231 RuleContext ctx = (RuleContext) data;
232 ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, (SimpleNode) node, MessageFormat.format(getMessage(), args)));
233 }
234 }