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.Date;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Set;
030:
031: import org.hammurapi.results.Annotation;
032: import org.hammurapi.results.DetailedResults;
033: import org.hammurapi.results.ResultsFactory;
034:
035: import com.pavelvlasov.logging.Logger;
036: import com.pavelvlasov.review.Signed;
037: import com.pavelvlasov.review.SimpleSourceMarker;
038: import com.pavelvlasov.review.SourceMarker;
039: import com.pavelvlasov.util.VisitorStackSource;
040:
041: /**
042: * @author Pavel Vlasov
043: * @version $Revision: 1.7 $
044: */
045: public class InspectorContextImpl extends InspectorContextBase {
046:
047: private Collection violationFilters;
048:
049: /**
050: * @param descriptor
051: * @param logger
052: * @param visitorStackSource
053: */
054: public InspectorContextImpl(InspectorDescriptor descriptor,
055: Logger logger, VisitorStackSource visitorStackSource,
056: SessionImpl session, Collection violationFilters) {
057: super (descriptor, logger, visitorStackSource, session);
058: this .violationFilters = violationFilters;
059: }
060:
061: /**
062: *
063: * @param source
064: * @param message
065: */
066: public void reportViolation(final SourceMarker source,
067: final String message) {
068: try {
069: final DetailedResults threadResults = ResultsFactory
070: .getThreadResults();
071: final SourceMarker detachedSource = detach(source);
072:
073: ResultsFactory.getInstance().execute(
074: new ResultsFactory.Task() {
075:
076: public void execute() throws HammurapiException {
077: SimpleViolation violation = new SimpleViolation(
078: detachedSource, message, descriptor);
079:
080: Iterator filterIterator = violationFilters == null ? null
081: : violationFilters.iterator();
082: while (filterIterator != null
083: && filterIterator.hasNext()) {
084: if (!((ViolationFilter) filterIterator
085: .next()).accept(violation)) {
086: return;
087: }
088: }
089:
090: threadResults.addViolation(violation);
091: }
092:
093: });
094: } catch (HammurapiException e) {
095: warn(source, e);
096: }
097: }
098:
099: public void annotate(Annotation annotation) {
100: ResultsFactory.getThreadResults().addAnnotation(annotation);
101: }
102:
103: public void addMetric(SourceMarker source, String name, double value) {
104: ResultsFactory.getThreadResults().addMetric(detach(source),
105: name, value);
106: }
107:
108: /**
109: * Report warning
110: * @param source
111: * @param message
112: */
113: public void warn(SourceMarker source, String message) {
114: Violation violation = new SimpleViolation(source == null ? null
115: : new SimpleSourceMarker(source), message, descriptor);
116: ResultsFactory.getThreadResults().addWarning(violation);
117: if (source == null) {
118: System.err.println("WARNING: " + message);
119: } else {
120: System.err.println("WARNING at " + source.getSourceURL()
121: + " " + source.getLine() + ":" + source.getColumn()
122: + " : " + message);
123: }
124: }
125:
126: /**
127: * Report warning
128: * @param source
129: */
130: public void warn(SourceMarker source, Throwable th) {
131: Violation violation = new SimpleViolation(detach(source), th
132: .toString(), descriptor);
133: ResultsFactory.getThreadResults().addWarning(violation);
134: // TODO better warning handling here
135: if (source == null) {
136: System.err.println("WARNING: " + th);
137: } else {
138: System.err.println("WARNING at " + source.getSourceURL()
139: + " " + source.getLine() + ":" + source.getColumn()
140: + " : " + th);
141: }
142: th.printStackTrace();
143: }
144:
145: /**
146: * Creates a waiver for inspector with a given key
147: * @param inspectorKey
148: */
149: public void waive(Signed signed, final String inspectorKey) {
150: final String iName = descriptor
151: .getWaivedInspectorName(inspectorKey);
152: if (iName == null) {
153: warn(signed instanceof SourceMarker ? (SourceMarker) signed
154: : null, descriptor.getName()
155: + ": Inspector with key '" + inspectorKey
156: + "' not found.");
157: } else {
158: final String signature = signed == null ? null : signed
159: .getSignature();
160: final Set signatures = new HashSet();
161: if (signature != null) {
162: signatures.add(signature);
163: }
164:
165: if (logger != null) {
166: logger.debug(this , "Inspector "
167: + getDescriptor().getName() + " autowaives "
168: + iName + " at " + signature);
169: }
170:
171: final Waiver waiver = new Waiver() {
172: boolean active = true;
173:
174: public String getInspectorName() {
175: return iName;
176: }
177:
178: public boolean waive(Violation violation, boolean peek) {
179: if (iName.equals(violation.getDescriptor()
180: .getName())) {
181: if (signature == null) {
182: return true;
183: }
184:
185: if (violation.getSource() instanceof Signed
186: && signature.equals(((Signed) violation
187: .getSource()).getSignature())) {
188: if (!peek) {
189: active = false;
190: }
191: return true;
192: }
193: }
194: return false;
195: }
196:
197: public Date getExpirationDate() {
198: return null;
199: }
200:
201: public String getReason() {
202: return descriptor.getWaiveReason(inspectorKey);
203: }
204:
205: public boolean isActive() {
206: return active;
207: }
208:
209: public Collection getSignatures() {
210: return signatures;
211: }
212: };
213:
214: final DetailedResults threadResults = ResultsFactory
215: .getThreadResults();
216: try {
217: ResultsFactory.getInstance().execute(
218: new ResultsFactory.Task() {
219:
220: public void execute() {
221: threadResults.getWaiverSet().addWaiver(
222: waiver, date);
223: }
224:
225: });
226: } catch (HammurapiException e) {
227: throw new HammurapiRuntimeException(e);
228: }
229: }
230: }
231: }
|