KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > InspectorDescriptorStack


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.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import com.pavelvlasov.config.ConfigurationException;
33 import com.pavelvlasov.config.Parameterizable;
34
35 /**
36  * Contains information from multiple descriptors. Returns first not-null
37  * value from the stack.
38  * @author Pavel Vlasov
39  * @version $Revision: 1.6 $
40  */

41 public class InspectorDescriptorStack implements InspectorDescriptor {
42     private LinkedList JavaDoc stack=new LinkedList JavaDoc();
43     
44     public String JavaDoc getDescription() {
45         Iterator JavaDoc it=stack.iterator();
46         while (it.hasNext()) {
47             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
48             if (descriptor.getDescription()!=null) {
49                 return descriptor.getDescription();
50             }
51         }
52         return null;
53     }
54
55     public String JavaDoc getCategory() {
56         Iterator JavaDoc it=stack.iterator();
57         while (it.hasNext()) {
58             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
59             if (descriptor.getCategory()!=null) {
60                 return descriptor.getCategory();
61             }
62         }
63         return "Miscellaneous";
64     }
65
66     public Boolean JavaDoc isEnabled() {
67         Iterator JavaDoc it=stack.iterator();
68         while (it.hasNext()) {
69             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
70             if (descriptor.isEnabled()!=null) {
71                 return descriptor.isEnabled();
72             }
73         }
74         return null;
75     }
76
77     public String JavaDoc getName() {
78         Iterator JavaDoc it=stack.iterator();
79         while (it.hasNext()) {
80             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
81             if (descriptor.getName()!=null) {
82                 return descriptor.getName();
83             }
84         }
85         return null;
86     }
87
88     Integer JavaDoc defaultSeverity=new Integer JavaDoc(1);
89     private Set JavaDoc parameterized=new HashSet JavaDoc();
90     
91     public Integer JavaDoc getSeverity() {
92         Iterator JavaDoc it=stack.iterator();
93         while (it.hasNext()) {
94             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
95             if (descriptor.getSeverity()!=null) {
96                 return descriptor.getSeverity();
97             }
98         }
99         return defaultSeverity;
100     }
101
102     public Integer JavaDoc getOrder() {
103         Iterator JavaDoc it=stack.iterator();
104         while (it.hasNext()) {
105             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
106             if (descriptor.getOrder()!=null) {
107                 return descriptor.getOrder();
108             }
109         }
110         return null;
111     }
112
113     public String JavaDoc getRationale() {
114         Iterator JavaDoc it=stack.iterator();
115         while (it.hasNext()) {
116             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
117             if (descriptor.getRationale()!=null) {
118                 return descriptor.getRationale();
119             }
120         }
121         return null;
122     }
123
124     public String JavaDoc getViolationSample() {
125         Iterator JavaDoc it=stack.iterator();
126         while (it.hasNext()) {
127             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
128             if (descriptor.getViolationSample()!=null) {
129                 return descriptor.getViolationSample();
130             }
131         }
132         return null;
133     }
134
135     public String JavaDoc getFixSample() {
136         Iterator JavaDoc it=stack.iterator();
137         while (it.hasNext()) {
138             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
139             if (descriptor.getFixSample()!=null) {
140                 return descriptor.getFixSample();
141             }
142         }
143         return null;
144     }
145
146     public String JavaDoc getResources() {
147         Iterator JavaDoc it=stack.iterator();
148         while (it.hasNext()) {
149             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
150             if (descriptor.getResources()!=null) {
151                 return descriptor.getResources();
152             }
153         }
154         return null;
155     }
156
157     public String JavaDoc getMessage() {
158         Iterator JavaDoc it=stack.iterator();
159         while (it.hasNext()) {
160             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
161             if (descriptor.getMessage()!=null) {
162                 return descriptor.getMessage();
163             }
164         }
165         return getDescription();
166     }
167     
168     /**
169      * Adds descriptor at the beginning of the stack (will be used first)
170      * @param descriptor
171      */

172     public void addFirst(InspectorDescriptor descriptor) {
173         stack.addFirst(descriptor);
174     }
175     
176     public void addLast(InspectorDescriptor descriptor) {
177         stack.addLast(descriptor);
178     }
179
180     public Inspector getInspector() throws ConfigurationException {
181         Iterator JavaDoc it=stack.iterator();
182         while (it.hasNext()) {
183             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
184             Inspector inspector=descriptor.getInspector();
185             if (inspector!=null) {
186                 if (!getParameters().isEmpty()) {
187                     if (inspector instanceof Parameterizable) {
188                         if (!parameterized.contains(inspector)) {
189                             parameterized.add(inspector);
190                             Iterator JavaDoc pit=getParameters().iterator();
191                             while (pit.hasNext()) {
192                                 ParameterEntry pe=(ParameterEntry) pit.next();
193                                 if (!((Parameterizable) inspector).setParameter(pe.getName(), pe.getValue())) {
194                                     throw new ConfigurationException(inspector.getClass().getName()+" does not support parameter "+pe.getName());
195                                 }
196                             }
197                         }
198                     } else {
199                         throw new ConfigurationException(inspector.getClass().getName()+" does not implement "+Parameterizable.class.getName());
200                     }
201                 }
202                 return inspector;
203             }
204         }
205         return null;
206     }
207
208     public Collection JavaDoc getParameters() {
209         LinkedList JavaDoc ret=new LinkedList JavaDoc();
210         LinkedList JavaDoc rstack=new LinkedList JavaDoc(stack);
211         Collections.reverse(rstack);
212         Iterator JavaDoc it=rstack.iterator();
213         while (it.hasNext()) {
214             Collection JavaDoc params=((InspectorDescriptor) it.next()).getParameters();
215             if (params!=null) {
216                 ret.addAll(params);
217             }
218         }
219         return ret;
220     }
221
222     public String JavaDoc getMessage(String JavaDoc key) {
223         Iterator JavaDoc it=stack.iterator();
224         while (it.hasNext()) {
225             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
226             if (descriptor.getMessage(key)!=null) {
227                 return descriptor.getMessage(key);
228             }
229         }
230         return null;
231     }
232
233     public Boolean JavaDoc isWaivable() {
234         Iterator JavaDoc it=stack.iterator();
235         while (it.hasNext()) {
236             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
237             if (descriptor.isWaivable()!=null) {
238                 return descriptor.isWaivable();
239             }
240         }
241         return null;
242     }
243
244     public Collection JavaDoc getWaiveCases() {
245         LinkedList JavaDoc ret=new LinkedList JavaDoc();
246         Iterator JavaDoc it=ret.iterator();
247         while (it.hasNext()) {
248             Collection JavaDoc waiveCases=((InspectorDescriptor) it.next()).getWaiveCases();
249             if (waiveCases!=null) {
250                 ret.addAll(waiveCases);
251             }
252         }
253         return ret;
254     }
255
256     public String JavaDoc getWaivedInspectorName(String JavaDoc inspectorKey) {
257         Iterator JavaDoc it=stack.iterator();
258         while (it.hasNext()) {
259             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
260             String JavaDoc ret=descriptor.getWaivedInspectorName(inspectorKey);
261             if (ret!=null) {
262                 return ret;
263             }
264         }
265         return null;
266     }
267
268     public String JavaDoc getWaiveReason(String JavaDoc inspectorKey) {
269         Iterator JavaDoc it=stack.iterator();
270         while (it.hasNext()) {
271             InspectorDescriptor descriptor=(InspectorDescriptor) it.next();
272             String JavaDoc ret=descriptor.getWaiveReason(inspectorKey);
273             if (ret!=null) {
274                 return ret;
275             }
276         }
277         return null;
278     }
279
280     public Collection JavaDoc getWaivedInspectorNames() {
281         LinkedList JavaDoc ret=new LinkedList JavaDoc();
282         Iterator JavaDoc it=ret.iterator();
283         while (it.hasNext()) {
284             Collection JavaDoc win=((InspectorDescriptor) it.next()).getWaivedInspectorNames();
285             if (win!=null) {
286                 ret.addAll(win);
287             }
288         }
289         return ret;
290     }
291
292     public Collection JavaDoc getFilteredInspectorDesriptors(InspectorSet inspectorSet, Collection JavaDoc chain) {
293         Iterator JavaDoc it=stack.iterator();
294         while (it.hasNext()) {
295             chain=((InspectorDescriptor) it.next()).getFilteredInspectorDesriptors(inspectorSet, chain);
296         }
297         return chain;
298     }
299
300     public Collection JavaDoc getAfterInspectorNames() {
301         LinkedList JavaDoc ret=new LinkedList JavaDoc();
302         Iterator JavaDoc it=ret.iterator();
303         while (it.hasNext()) {
304             Collection JavaDoc ain=((InspectorDescriptor) it.next()).getAfterInspectorNames();
305             if (ain!=null) {
306                 ret.addAll(ain);
307             }
308         }
309         return ret;
310     }
311 }
312
Popular Tags