KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > InspectorEntry


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.taskdefs.Property;
34
35 import com.pavelvlasov.ant.ObjectEntry;
36 import com.pavelvlasov.ant.Param;
37 import com.pavelvlasov.config.ConfigurationException;
38 import com.pavelvlasov.config.Parameterizable;
39
40 /**
41  * Defines inspector
42  * @ant.element name="inspector" display-name="In-line inspector definition"
43  * @author Pavel Vlasov
44  * @version $Revision: 1.8 $
45  */

46 public class InspectorEntry extends ObjectEntry implements InspectorDescriptor {
47     private String JavaDoc fixSample;
48     private String JavaDoc message;
49     private String JavaDoc name;
50     private Integer JavaDoc order;
51     private String JavaDoc rationale;
52     private String JavaDoc resources;
53     private Integer JavaDoc severity;
54     private String JavaDoc violationSample;
55     private Boolean JavaDoc isEnabled;
56     private Boolean JavaDoc isWaivable;
57     private String JavaDoc description;
58     private String JavaDoc category;
59     private List JavaDoc waiveCases=new LinkedList JavaDoc();
60     
61     public void addConfiguredWaiveCase(WaiveCaseEntry entry) {
62         waiveCases.add(entry.getText());
63     }
64     
65     public Boolean JavaDoc isEnabled() {
66         return isEnabled;
67     }
68
69     public Boolean JavaDoc isWaivable() {
70         return isWaivable;
71     }
72
73     public String JavaDoc getName() {
74         return name;
75     }
76
77     public Integer JavaDoc getSeverity() {
78         return severity;
79     }
80
81     public Integer JavaDoc getOrder() {
82         return order;
83     }
84
85     public String JavaDoc getRationale() {
86         return rationale;
87     }
88
89     public String JavaDoc getViolationSample() {
90         return violationSample;
91     }
92
93     public String JavaDoc getFixSample() {
94         return fixSample;
95     }
96
97     public String JavaDoc getResources() {
98         return resources;
99     }
100
101     public String JavaDoc getMessage() {
102         return message;
103     }
104     
105     private List JavaDoc filterEntries=new LinkedList JavaDoc();
106     
107     /**
108      * Inspector to include to filtering
109      * @ant.non-required
110      * @param entry
111      * @return
112      */

113     public FilterEntry createFilterInclude() {
114         FilterEntry fe=new FilterEntry();
115         filterEntries.add(fe);
116         return fe;
117     }
118
119     /**
120      * Inspector to exclude from filtering.
121      * @ant.non-required
122      * @param entry
123      * @return
124      */

125     public FilterEntry createFilterExclude() {
126         FilterEntry fe=new FilterEntry();
127         filterEntries.add(fe);
128         fe.exclude=true;
129         return fe;
130     }
131     
132     public Inspector getInspector() throws ConfigurationException {
133         if (getClassName()==null) {
134             return null;
135         } else {
136             Inspector ret = (Inspector) getObject(null);
137             if (!super.getParameters().isEmpty()) {
138                 if (ret instanceof Parameterizable) {
139                     Iterator JavaDoc it=getParameters().iterator();
140                     while (it.hasNext()) {
141                         ParameterEntry pe=(ParameterEntry) it.next();
142                         if (!((Parameterizable) ret).setParameter(pe.getName(), pe.getValue())) {
143                             throw new ConfigurationException(ret.getClass().getName()+" does not support parameter "+pe.getName());
144                         }
145                     }
146                 } else {
147                     throw new ConfigurationException(ret.getClass().getName()+" does not implement "+Parameterizable.class.getName());
148                 }
149             }
150             return ret;
151         }
152     }
153
154     protected void validateClass(Class JavaDoc clazz) throws BuildException {
155         super.validateClass(clazz);
156         if (!Inspector.class.isAssignableFrom(clazz)) {
157             throw new BuildException(clazz.getName()+" doesn't implement "+Inspector.class);
158         }
159     }
160     
161     /**
162      * @ant:non-required
163      * @param fixSample The fixSample to set.
164      */

165     public void setFixSample(String JavaDoc fixSample) {
166         this.fixSample = fixSample;
167     }
168
169     /**
170      * @ant:non-required
171      * @param message The message to set.
172      */

173     public void setMessage(String JavaDoc message) {
174         this.message = message;
175     }
176
177     /**
178      * @ant:non-required
179      * @param name The name to set.
180      */

181     public void setName(String JavaDoc name) {
182         this.name = name;
183     }
184
185     /**
186      * @ant:non-required
187      * @param order The order to set.
188      */

189     public void setOrder(int order) {
190         this.order = new Integer JavaDoc(order);
191     }
192
193     /**
194      * @ant:non-required
195      * @param rationale The rationale to set.
196      */

197     public void setRationale(String JavaDoc rationale) {
198         this.rationale = rationale;
199     }
200
201     /**
202      * @ant:non-required
203      * @param resources The resources to set.
204      */

205     public void setResources(String JavaDoc resources) {
206         this.resources = resources;
207     }
208
209     /**
210      * @ant:non-required
211      * @param severity The severity to set.
212      */

213     public void setSeverity(int severity) {
214         this.severity = new Integer JavaDoc(severity);
215     }
216
217     /**
218      * @ant:non-required
219      * @param violationSample The violationSample to set.
220      */

221     public void setViolationSample(String JavaDoc violationSample) {
222         this.violationSample = violationSample;
223     }
224
225     /**
226      * @ant:non-required
227      * @param isEnabled The isEnabled to set.
228      */

229     public void setEnabled(boolean isEnabled) {
230         this.isEnabled = isEnabled ? Boolean.TRUE : Boolean.FALSE;
231     }
232     
233     /**
234      * @ant:non-required
235      * @param isWaivable The isWaivable to set.
236      */

237     public void setWaivable(boolean isWaivable) {
238         this.isWaivable = isWaivable ? Boolean.TRUE : Boolean.FALSE;
239     }
240     
241     /**
242      * @return Returns the description.
243      */

244     public String JavaDoc getDescription() {
245         return description;
246     }
247
248     /**
249      * @return Returns the description.
250      */

251     public String JavaDoc getCategory() {
252         return category;
253     }
254
255     /**
256      * @ant:non-required
257      * @param description The description to set.
258      */

259     public void setDescription(String JavaDoc description) {
260         this.description = description;
261     }
262
263     /**
264      * @ant:non-required
265      * @param category The category to set.
266      */

267     public void setCategory(String JavaDoc category) {
268         this.category=category;
269     }
270     
271     /**
272      * Convert entries from Param to ParameterEntry
273      * @ant:ignore
274      */

275     public Collection JavaDoc getParameters() {
276         List JavaDoc ret=new LinkedList JavaDoc();
277         Iterator JavaDoc it=super.getParameters().iterator();
278         while (it.hasNext()) {
279             Param p=(Param) it.next();
280             ret.add(new ParameterEntry(p.getName(), p.getObject(null)));
281         }
282         return ret;
283     }
284     
285     private Map JavaDoc messages=new HashMap JavaDoc();
286     
287     /**
288      * Keyed message.
289      * @param message
290      * @ant.non-required
291      */

292     public void addConfiguredMessage(Property message) {
293         messages.put(message.getName(), message);
294     }
295
296     public String JavaDoc getMessage(String JavaDoc key) {
297         Property property=(Property) messages.get(key);
298         return property==null ? null : property.getValue();
299     }
300
301     public Collection JavaDoc getWaiveCases() {
302         return waiveCases;
303     }
304
305     public String JavaDoc getWaivedInspectorName(String JavaDoc inspectorKey) {
306         // TODO Auto-generated method stub
307
return null;
308     }
309
310     public String JavaDoc getWaiveReason(String JavaDoc inspectorKey) {
311         // TODO Auto-generated method stub
312
return null;
313     }
314
315     public Collection JavaDoc getWaivedInspectorNames() {
316         // TODO Auto-generated method stub
317
return null;
318     }
319
320     public Collection JavaDoc getFilteredInspectorDesriptors(InspectorSet inspectorSet, Collection JavaDoc chain) {
321         if (chain==null) {
322             chain=new LinkedList JavaDoc();
323         }
324         
325         Iterator JavaDoc it=filterEntries.iterator();
326         while (it.hasNext()) {
327             FilterEntry fe=(FilterEntry) it.next();
328             if (fe.exclude) {
329                 if (fe.name==null) {
330                     Iterator JavaDoc dit=chain.iterator();
331                     while (dit.hasNext()) {
332                         if (fe.category.equals(((InspectorDescriptor) dit.next()).getCategory())) {
333                             dit.remove();
334                         }
335                     }
336                 } else {
337                     if ("*".equals(fe.name)) {
338                         chain.clear();
339                     } else {
340                         Iterator JavaDoc dit=chain.iterator();
341                         while (dit.hasNext()) {
342                             if (fe.name.equals(((InspectorDescriptor) dit.next()).getName())) {
343                                 dit.remove();
344                             }
345                         }
346                     }
347                 }
348             } else {
349                 if (fe.name==null) {
350                     Iterator JavaDoc dit=inspectorSet.getDescriptors().iterator();
351                     while (dit.hasNext()) {
352                         InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next();
353                         if (fe.category.equals(inspectorDescriptor.getCategory())) {
354                             chain.add(inspectorDescriptor);
355                         }
356                     }
357                 } else {
358                     if ("*".equals(fe.name)) {
359                         chain.addAll(inspectorSet.getDescriptors());
360                     } else {
361                         Iterator JavaDoc dit=inspectorSet.getDescriptors().iterator();
362                         while (dit.hasNext()) {
363                             InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next();
364                             if (fe.name.equals(inspectorDescriptor.getName())) {
365                                 chain.add(inspectorDescriptor);
366                             }
367                         }
368                     }
369                 }
370             }
371         }
372         
373         return chain;
374     }
375
376     public Collection JavaDoc getAfterInspectorNames() {
377         return null;
378     }
379 }
380
Popular Tags