001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.util.Collection;
026: import java.util.Collections;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.Set;
031:
032: import com.pavelvlasov.config.ConfigurationException;
033: import com.pavelvlasov.config.Parameterizable;
034:
035: /**
036: * Contains information from multiple descriptors. Returns first not-null
037: * value from the stack.
038: * @author Pavel Vlasov
039: * @version $Revision: 1.6 $
040: */
041: public class InspectorDescriptorStack implements InspectorDescriptor {
042: private LinkedList stack = new LinkedList();
043:
044: public String getDescription() {
045: Iterator it = stack.iterator();
046: while (it.hasNext()) {
047: InspectorDescriptor descriptor = (InspectorDescriptor) it
048: .next();
049: if (descriptor.getDescription() != null) {
050: return descriptor.getDescription();
051: }
052: }
053: return null;
054: }
055:
056: public String getCategory() {
057: Iterator it = stack.iterator();
058: while (it.hasNext()) {
059: InspectorDescriptor descriptor = (InspectorDescriptor) it
060: .next();
061: if (descriptor.getCategory() != null) {
062: return descriptor.getCategory();
063: }
064: }
065: return "Miscellaneous";
066: }
067:
068: public Boolean isEnabled() {
069: Iterator it = stack.iterator();
070: while (it.hasNext()) {
071: InspectorDescriptor descriptor = (InspectorDescriptor) it
072: .next();
073: if (descriptor.isEnabled() != null) {
074: return descriptor.isEnabled();
075: }
076: }
077: return null;
078: }
079:
080: public String getName() {
081: Iterator it = stack.iterator();
082: while (it.hasNext()) {
083: InspectorDescriptor descriptor = (InspectorDescriptor) it
084: .next();
085: if (descriptor.getName() != null) {
086: return descriptor.getName();
087: }
088: }
089: return null;
090: }
091:
092: Integer defaultSeverity = new Integer(1);
093: private Set parameterized = new HashSet();
094:
095: public Integer getSeverity() {
096: Iterator it = stack.iterator();
097: while (it.hasNext()) {
098: InspectorDescriptor descriptor = (InspectorDescriptor) it
099: .next();
100: if (descriptor.getSeverity() != null) {
101: return descriptor.getSeverity();
102: }
103: }
104: return defaultSeverity;
105: }
106:
107: public Integer getOrder() {
108: Iterator it = stack.iterator();
109: while (it.hasNext()) {
110: InspectorDescriptor descriptor = (InspectorDescriptor) it
111: .next();
112: if (descriptor.getOrder() != null) {
113: return descriptor.getOrder();
114: }
115: }
116: return null;
117: }
118:
119: public String getRationale() {
120: Iterator it = stack.iterator();
121: while (it.hasNext()) {
122: InspectorDescriptor descriptor = (InspectorDescriptor) it
123: .next();
124: if (descriptor.getRationale() != null) {
125: return descriptor.getRationale();
126: }
127: }
128: return null;
129: }
130:
131: public String getViolationSample() {
132: Iterator it = stack.iterator();
133: while (it.hasNext()) {
134: InspectorDescriptor descriptor = (InspectorDescriptor) it
135: .next();
136: if (descriptor.getViolationSample() != null) {
137: return descriptor.getViolationSample();
138: }
139: }
140: return null;
141: }
142:
143: public String getFixSample() {
144: Iterator it = stack.iterator();
145: while (it.hasNext()) {
146: InspectorDescriptor descriptor = (InspectorDescriptor) it
147: .next();
148: if (descriptor.getFixSample() != null) {
149: return descriptor.getFixSample();
150: }
151: }
152: return null;
153: }
154:
155: public String getResources() {
156: Iterator it = stack.iterator();
157: while (it.hasNext()) {
158: InspectorDescriptor descriptor = (InspectorDescriptor) it
159: .next();
160: if (descriptor.getResources() != null) {
161: return descriptor.getResources();
162: }
163: }
164: return null;
165: }
166:
167: public String getMessage() {
168: Iterator it = stack.iterator();
169: while (it.hasNext()) {
170: InspectorDescriptor descriptor = (InspectorDescriptor) it
171: .next();
172: if (descriptor.getMessage() != null) {
173: return descriptor.getMessage();
174: }
175: }
176: return getDescription();
177: }
178:
179: /**
180: * Adds descriptor at the beginning of the stack (will be used first)
181: * @param descriptor
182: */
183: public void addFirst(InspectorDescriptor descriptor) {
184: stack.addFirst(descriptor);
185: }
186:
187: public void addLast(InspectorDescriptor descriptor) {
188: stack.addLast(descriptor);
189: }
190:
191: public Inspector getInspector() throws ConfigurationException {
192: Iterator it = stack.iterator();
193: while (it.hasNext()) {
194: InspectorDescriptor descriptor = (InspectorDescriptor) it
195: .next();
196: Inspector inspector = descriptor.getInspector();
197: if (inspector != null) {
198: if (!getParameters().isEmpty()) {
199: if (inspector instanceof Parameterizable) {
200: if (!parameterized.contains(inspector)) {
201: parameterized.add(inspector);
202: Iterator pit = getParameters().iterator();
203: while (pit.hasNext()) {
204: ParameterEntry pe = (ParameterEntry) pit
205: .next();
206: if (!((Parameterizable) inspector)
207: .setParameter(pe.getName(), pe
208: .getValue())) {
209: throw new ConfigurationException(
210: inspector.getClass()
211: .getName()
212: + " does not support parameter "
213: + pe.getName());
214: }
215: }
216: }
217: } else {
218: throw new ConfigurationException(inspector
219: .getClass().getName()
220: + " does not implement "
221: + Parameterizable.class.getName());
222: }
223: }
224: return inspector;
225: }
226: }
227: return null;
228: }
229:
230: public Collection getParameters() {
231: LinkedList ret = new LinkedList();
232: LinkedList rstack = new LinkedList(stack);
233: Collections.reverse(rstack);
234: Iterator it = rstack.iterator();
235: while (it.hasNext()) {
236: Collection params = ((InspectorDescriptor) it.next())
237: .getParameters();
238: if (params != null) {
239: ret.addAll(params);
240: }
241: }
242: return ret;
243: }
244:
245: public String getMessage(String key) {
246: Iterator it = stack.iterator();
247: while (it.hasNext()) {
248: InspectorDescriptor descriptor = (InspectorDescriptor) it
249: .next();
250: if (descriptor.getMessage(key) != null) {
251: return descriptor.getMessage(key);
252: }
253: }
254: return null;
255: }
256:
257: public Boolean isWaivable() {
258: Iterator it = stack.iterator();
259: while (it.hasNext()) {
260: InspectorDescriptor descriptor = (InspectorDescriptor) it
261: .next();
262: if (descriptor.isWaivable() != null) {
263: return descriptor.isWaivable();
264: }
265: }
266: return null;
267: }
268:
269: public Collection getWaiveCases() {
270: LinkedList ret = new LinkedList();
271: Iterator it = ret.iterator();
272: while (it.hasNext()) {
273: Collection waiveCases = ((InspectorDescriptor) it.next())
274: .getWaiveCases();
275: if (waiveCases != null) {
276: ret.addAll(waiveCases);
277: }
278: }
279: return ret;
280: }
281:
282: public String getWaivedInspectorName(String inspectorKey) {
283: Iterator it = stack.iterator();
284: while (it.hasNext()) {
285: InspectorDescriptor descriptor = (InspectorDescriptor) it
286: .next();
287: String ret = descriptor
288: .getWaivedInspectorName(inspectorKey);
289: if (ret != null) {
290: return ret;
291: }
292: }
293: return null;
294: }
295:
296: public String getWaiveReason(String inspectorKey) {
297: Iterator it = stack.iterator();
298: while (it.hasNext()) {
299: InspectorDescriptor descriptor = (InspectorDescriptor) it
300: .next();
301: String ret = descriptor.getWaiveReason(inspectorKey);
302: if (ret != null) {
303: return ret;
304: }
305: }
306: return null;
307: }
308:
309: public Collection getWaivedInspectorNames() {
310: LinkedList ret = new LinkedList();
311: Iterator it = ret.iterator();
312: while (it.hasNext()) {
313: Collection win = ((InspectorDescriptor) it.next())
314: .getWaivedInspectorNames();
315: if (win != null) {
316: ret.addAll(win);
317: }
318: }
319: return ret;
320: }
321:
322: public Collection getFilteredInspectorDesriptors(
323: InspectorSet inspectorSet, Collection chain) {
324: Iterator it = stack.iterator();
325: while (it.hasNext()) {
326: chain = ((InspectorDescriptor) it.next())
327: .getFilteredInspectorDesriptors(inspectorSet, chain);
328: }
329: return chain;
330: }
331:
332: public Collection getAfterInspectorNames() {
333: LinkedList ret = new LinkedList();
334: Iterator it = ret.iterator();
335: while (it.hasNext()) {
336: Collection ain = ((InspectorDescriptor) it.next())
337: .getAfterInspectorNames();
338: if (ain != null) {
339: ret.addAll(ain);
340: }
341: }
342: return ret;
343: }
344: }
|