KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > DomInspectorDescriptor


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.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.xml.transform.TransformerException JavaDoc;
34
35 import org.apache.xpath.CachedXPathAPI;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.traversal.NodeIterator;
40
41 import com.pavelvlasov.config.ConfigurationException;
42 import com.pavelvlasov.config.DomConfigFactory;
43 import com.pavelvlasov.config.Parameterizable;
44 import com.pavelvlasov.xml.dom.AbstractDomObject;
45
46 /**
47  * @author Pavel Vlasov
48  * @version $Revision: 1.9 $
49  */

50 public class DomInspectorDescriptor extends AbstractDomObject implements InspectorDescriptor {
51     private String JavaDoc fixSample;
52     private String JavaDoc message;
53     private String JavaDoc category;
54     private String JavaDoc name;
55     private Integer JavaDoc order;
56     private String JavaDoc rationale;
57     private String JavaDoc resources;
58     private Integer JavaDoc severity;
59     private String JavaDoc violationSample;
60     private Boolean JavaDoc isEnabled;
61     private Boolean JavaDoc isWaivable;
62     private String JavaDoc description;
63     private Element JavaDoc inspectorElement;
64     private Inspector inspector;
65     private List JavaDoc parameters=new LinkedList JavaDoc();
66     private List JavaDoc waiveCases=new LinkedList JavaDoc();
67     private Map JavaDoc messages=new HashMap JavaDoc();
68     
69     private class FilterNameEntry {
70         boolean exclude;
71         String JavaDoc name;
72         String JavaDoc category;
73     }
74     
75     private Collection JavaDoc filterEntries=new LinkedList JavaDoc();
76     
77     public DomInspectorDescriptor(Element JavaDoc holder) throws HammurapiException {
78         super();
79         try {
80             CachedXPathAPI cxpa=new CachedXPathAPI();
81             fixSample=getElementText(holder, "fix-sample", cxpa);
82             
83             NodeIterator it=cxpa.selectNodeIterator(holder, "message");
84             Node JavaDoc messageNode;
85             while ((messageNode=it.nextNode())!=null) {
86                 if (messageNode instanceof Element JavaDoc) {
87                     Element JavaDoc messageElement=(Element JavaDoc) messageNode;
88                     if (messageElement.hasAttribute("key")) {
89                         messages.put(messageElement.getAttribute("key"), getElementText(messageElement));
90                     } else {
91                         message=getElementText(messageElement);
92                     }
93                 }
94             }
95             name=getElementText(holder, "name", cxpa);
96             String JavaDoc orderVal=getElementText(holder, "order", cxpa);
97             if (orderVal!=null) {
98                 order=new Integer JavaDoc(orderVal);
99             }
100             rationale=getElementText(holder, "rationale", cxpa);
101             category=getElementText(holder, "category", cxpa);
102             resources=getElementText(holder, "resource", cxpa);
103             String JavaDoc severityVal=getElementText(holder, "severity", cxpa);
104             if (severityVal!=null) {
105                 severity=new Integer JavaDoc(severityVal);
106             }
107             violationSample=getElementText(holder, "violation-sample", cxpa);
108             
109             String JavaDoc enabledVal=getElementText(holder, "enabled", cxpa);
110             if (enabledVal!=null) {
111                 isEnabled="yes".equalsIgnoreCase(enabledVal) || "true".equalsIgnoreCase(enabledVal) ? Boolean.TRUE : Boolean.FALSE;
112             }
113             
114             String JavaDoc waivableVal=getElementText(holder, "waivable", cxpa);
115             if (waivableVal!=null) {
116                 isWaivable="yes".equalsIgnoreCase(waivableVal) || "true".equalsIgnoreCase(waivableVal) ? Boolean.TRUE : Boolean.FALSE;
117             }
118             
119             description=getElementText(holder, "description", cxpa);
120             
121             inspectorElement=(Element JavaDoc) cxpa.selectSingleNode(holder, "inspector");
122             
123             DomConfigFactory factory=new DomConfigFactory();
124             NodeIterator nit=cxpa.selectNodeIterator(holder, "parameter");
125             Node JavaDoc n;
126             while ((n=nit.nextNode())!=null) {
127                 parameters.add(new ParameterEntry(((Element JavaDoc) n).getAttribute("name"),factory.create(n)));
128             }
129             
130             nit=cxpa.selectNodeIterator(holder, "waive-case");
131             while ((n=nit.nextNode())!=null) {
132                 waiveCases.add(cxpa.eval(n, "text()").toString());
133             }
134             
135             nit=cxpa.selectNodeIterator(holder, "waives");
136             while ((n=nit.nextNode())!=null) {
137                 if (n instanceof Element JavaDoc) {
138                     Element JavaDoc waivesElement=(Element JavaDoc) n;
139                     if (waivesElement.hasAttribute("key")) {
140                         String JavaDoc wKey=waivesElement.getAttribute("key");
141                         String JavaDoc iName=cxpa.eval(n, "name/text()").toString();
142                         if (iName.trim().length()==0) {
143                             throw new HammurapiException("<waives> must have nested <name> element");
144                         }
145                         
146                         waivedInspectorNames.put(wKey, iName);
147                         waiveReasons.put(wKey, cxpa.eval(n, "reason/text()").toString());
148                     } else {
149                         throw new HammurapiException("<waives> must have 'key' attribute");
150                     }
151                 }
152             }
153             
154             nit=cxpa.selectNodeIterator(holder, "after");
155             while ((n=nit.nextNode())!=null) {
156                 afterInspectorNames.add(getElementText(n));
157             }
158             
159             NodeList JavaDoc nl=holder.getChildNodes();
160             for (int i=0; i<nl.getLength(); i++) {
161                 Node JavaDoc fn=nl.item(i);
162                 if (fn instanceof Element JavaDoc) {
163                     Element JavaDoc el=(Element JavaDoc) fn;
164                     if ("filter".equals(el.getNodeName())) {
165                         FilterNameEntry fne=new FilterNameEntry();
166                         if (el.hasAttribute("name")) {
167                             fne.name=el.getAttribute("name");
168                         } else if (el.hasAttribute("category")) {
169                             fne.category=el.getAttribute("category");
170                         } else {
171                             throw new ConfigurationException("<filter> element shall have either name or category attribute");
172                         }
173                         filterEntries.add(fne);
174                     } else if ("filter-exclude".equals(el.getNodeName())) {
175                         FilterNameEntry fne=new FilterNameEntry();
176                         fne.exclude=true;
177                         if (el.hasAttribute("name")) {
178                             fne.name=el.getAttribute("name");
179                         } else if (el.hasAttribute("category")) {
180                             fne.category=el.getAttribute("category");
181                         } else {
182                             throw new ConfigurationException("<filter-exclude> element shall have either name or category attribute");
183                         }
184                         filterEntries.add(fne);
185                     }
186                 }
187             }
188             //System.out.println(name);
189
} catch (TransformerException JavaDoc e) {
190             throw new HammurapiException(e);
191         } catch (ConfigurationException e) {
192             throw new HammurapiException(e);
193         }
194     }
195     
196     /**
197      * @return Returns the fixSample.
198      */

199     public String JavaDoc getFixSample() {
200         return fixSample;
201     }
202
203     /**
204      * @return Returns the isEnabled.
205      */

206     public Boolean JavaDoc isEnabled() {
207         return isEnabled;
208     }
209
210     /**
211      * @return Returns the message.
212      */

213     public String JavaDoc getMessage() {
214         return message;
215     }
216
217     /**
218      * @return Returns the message.
219      */

220     public String JavaDoc getCategory() {
221         return category;
222     }
223
224     /**
225      * @return Returns the name.
226      */

227     public String JavaDoc getName() {
228         return name;
229     }
230
231     /**
232      * @return Returns the order.
233      */

234     public Integer JavaDoc getOrder() {
235         return order;
236     }
237
238     /**
239      * @return Returns the rationale.
240      */

241     public String JavaDoc getRationale() {
242         return rationale;
243     }
244
245     /**
246      * @return Returns the resources.
247      */

248     public String JavaDoc getResources() {
249         return resources;
250     }
251
252     /**
253      * @return Returns the severity.
254      */

255     public Integer JavaDoc getSeverity() {
256         return severity;
257     }
258
259     /**
260      * @return Returns the violationSample.
261      */

262     public String JavaDoc getViolationSample() {
263         return violationSample;
264     }
265
266     public String JavaDoc getDescription() {
267         return description;
268     }
269
270     public Inspector getInspector() throws ConfigurationException {
271         if (inspector==null && inspectorElement!=null) {
272             DomConfigFactory factory=new DomConfigFactory();
273             Object JavaDoc o = factory.create(inspectorElement);
274             if (o instanceof Inspector) {
275                 inspector=(Inspector) o;
276                 if (!getParameters().isEmpty()) {
277                     if (inspector instanceof Parameterizable) {
278                         Iterator JavaDoc it=getParameters().iterator();
279                         while (it.hasNext()) {
280                             ParameterEntry pe=(ParameterEntry) it.next();
281                             if (!((Parameterizable) inspector).setParameter(pe.getName(), pe.getValue())) {
282                                 throw new ConfigurationException(o.getClass().getName()+" does not support parameter "+pe.getName());
283                             }
284                         }
285                     } else {
286                         throw new ConfigurationException(inspector.getClass().getName()+" does not implement "+Parameterizable.class.getName());
287                     }
288                 }
289             } else {
290                 throw new ConfigurationException(o.getClass().getName()+" doesn't implement "+Inspector.class.getName());
291             }
292         }
293         return inspector;
294     }
295
296     public Collection JavaDoc getParameters() {
297         return parameters;
298     }
299
300     public String JavaDoc getMessage(String JavaDoc key) {
301         return (String JavaDoc) messages.get(key);
302     }
303
304     public Boolean JavaDoc isWaivable() {
305         return isWaivable;
306     }
307
308     public Collection JavaDoc getWaiveCases() {
309         return waiveCases;
310     }
311
312     private Map JavaDoc waivedInspectorNames=new HashMap JavaDoc();
313     
314     public String JavaDoc getWaivedInspectorName(String JavaDoc inspectorKey) {
315         return (String JavaDoc) waivedInspectorNames.get(inspectorKey);
316     }
317
318     private Map JavaDoc waiveReasons=new HashMap JavaDoc();
319     private Collection JavaDoc afterInspectorNames = new HashSet JavaDoc();
320     
321     public String JavaDoc getWaiveReason(String JavaDoc inspectorKey) {
322         return (String JavaDoc) waiveReasons.get(inspectorKey);
323     }
324
325     public Collection JavaDoc getWaivedInspectorNames() {
326         return waivedInspectorNames.values();
327     }
328     
329     public Collection JavaDoc getAfterInspectorNames() {
330         return afterInspectorNames;
331     }
332
333     public Collection JavaDoc getFilteredInspectorDesriptors(InspectorSet inspectorSet, Collection JavaDoc chain) {
334         if (chain==null) {
335             chain=new LinkedList JavaDoc();
336         }
337         
338         Iterator JavaDoc it=filterEntries.iterator();
339         while (it.hasNext()) {
340             FilterNameEntry fne=(FilterNameEntry) it.next();
341             if (fne.exclude) {
342                 if (fne.name==null) {
343                     Iterator JavaDoc dit=chain.iterator();
344                     while (dit.hasNext()) {
345                         if (fne.category.equals(((InspectorDescriptor) dit.next()).getCategory())) {
346                             dit.remove();
347                         }
348                     }
349                 } else {
350                     if ("*".equals(fne.name)) {
351                         chain.clear();
352                     } else {
353                         Iterator JavaDoc dit=chain.iterator();
354                         while (dit.hasNext()) {
355                             if (fne.name.equals(((InspectorDescriptor) dit.next()).getName())) {
356                                 dit.remove();
357                             }
358                         }
359                     }
360                 }
361             } else {
362                 if (fne.name==null) {
363                     Iterator JavaDoc dit=inspectorSet.getDescriptors().iterator();
364                     while (dit.hasNext()) {
365                         InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next();
366                         if (fne.category.equals(inspectorDescriptor.getCategory())) {
367                             chain.add(inspectorDescriptor);
368                         }
369                     }
370                 } else {
371                     if ("*".equals(fne.name)) {
372                         chain.addAll(inspectorSet.getDescriptors());
373                     } else {
374                         Iterator JavaDoc dit=inspectorSet.getDescriptors().iterator();
375                         while (dit.hasNext()) {
376                             InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next();
377                             if (fne.name.equals(inspectorDescriptor.getName())) {
378                                 chain.add(inspectorDescriptor);
379                             }
380                         }
381                     }
382                 }
383             }
384         }
385         
386         return chain;
387     }
388 }
389
Popular Tags