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:
024: package org.hammurapi.render.dom;
025:
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: import org.hammurapi.HammurapiMeasurement;
035: import org.hammurapi.Violation;
036: import org.hammurapi.results.AggregatedResults;
037: import org.hammurapi.results.Annotation;
038: import org.hammurapi.results.InlineAnnotation;
039: import org.hammurapi.results.InspectorSummary;
040: import org.hammurapi.results.LinkedAnnotation;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043:
044: import com.pavelvlasov.metrics.Metric;
045: import com.pavelvlasov.render.RenderRequest;
046: import com.pavelvlasov.render.RenderingException;
047: import com.pavelvlasov.review.SourceMarker;
048:
049: /**
050: *
051: * @author Pavel Vlasov
052: * @version $Revision: 1.9 $
053: */
054: public class AggregatedResultsRenderer extends BasicResultsRenderer {
055:
056: public AggregatedResultsRenderer(RenderRequest request) {
057: super (request);
058: }
059:
060: public AggregatedResultsRenderer(RenderRequest request,
061: String profile) {
062: super (request, profile);
063: }
064:
065: public Element render(Document document) throws RenderingException {
066: Element ret = super .render(document);
067: AggregatedResults ar = (AggregatedResults) request
068: .getRenderee();
069:
070: ret.setAttribute("new", ar.isNew() ? "yes" : "no");
071:
072: Map severitySummary = ar.getSeveritySummary();
073: if (severitySummary != null) {
074: Iterator it = severitySummary.entrySet().iterator();
075: while (it.hasNext()) {
076: Map.Entry entry = (Map.Entry) it.next();
077: Element summaryElement = document
078: .createElement("severity-summary");
079: ret.appendChild(summaryElement);
080: summaryElement.setAttribute("severity", entry.getKey()
081: .toString());
082: List srs = new LinkedList(((Map) entry.getValue())
083: .values());
084: Collections.sort(srs);
085: inspectorSummary(srs, summaryElement);
086: }
087: }
088:
089: Collection warnings = ar.getWarnings();
090: if (warnings != null && !warnings.isEmpty()) {
091: Element we = document.createElement("warnings");
092: ret.appendChild(we);
093: Iterator wit = warnings.iterator();
094: while (wit.hasNext()) {
095: Violation warning = (Violation) wit.next();
096: ViolationRenderer vr = new ViolationRenderer(
097: new RenderRequest(warning));
098: we.appendChild(vr.render(document));
099: }
100: }
101:
102: Collection annotations = ar.getAnnotations();
103: if (annotations != null && !annotations.isEmpty()) {
104: Element annotationsElement = document
105: .createElement("annotations");
106: ret.appendChild(annotationsElement);
107: Iterator ait = annotations.iterator();
108: while (ait.hasNext()) {
109: Annotation annotation = (Annotation) ait.next();
110: Element annotationElement = document
111: .createElement("annotation");
112: annotationsElement.appendChild(annotationElement);
113: annotationElement.setAttribute("name", annotation
114: .getName());
115: if (annotation instanceof LinkedAnnotation) {
116: annotationElement.setAttribute("path",
117: ((LinkedAnnotation) annotation).getPath());
118: }
119:
120: if (annotation instanceof InlineAnnotation) {
121: Element annotationContentElement = document
122: .createElement("content");
123: annotationElement
124: .appendChild(annotationContentElement);
125: annotationContentElement
126: .appendChild(document
127: .createTextNode(((InlineAnnotation) annotation)
128: .getContent()));
129: }
130:
131: Properties properties = annotation.getProperties();
132: if (properties != null) {
133: Iterator prit = properties.entrySet().iterator();
134: while (prit.hasNext()) {
135: Map.Entry entry = (Map.Entry) prit.next();
136: Element propertyElement = document
137: .createElement("property");
138: annotationElement.appendChild(propertyElement);
139: propertyElement.setAttribute("name",
140: (String) entry.getKey());
141: propertyElement.appendChild(document
142: .createTextNode((String) entry
143: .getValue()));
144: }
145: }
146: }
147: }
148:
149: Map metrics = ar.getMetrics();
150: if (metrics != null) {
151: Iterator it = metrics.entrySet().iterator();
152: while (it.hasNext()) {
153: Map.Entry entry = (Map.Entry) it.next();
154: Element metricElement = document
155: .createElement("metric");
156: ret.appendChild(metricElement);
157: metricElement.setAttribute("name", (String) entry
158: .getKey());
159: Metric metric = (Metric) entry.getValue();
160: metricElement.setAttribute("number", String
161: .valueOf(metric.getNumber()));
162: metricElement.setAttribute("avg", format(metric
163: .getAvg()));
164: metricElement.setAttribute("min", format(metric
165: .getMin()));
166: metricElement.setAttribute("max", format(metric
167: .getMax()));
168: metricElement.setAttribute("total", format(metric
169: .getTotal()));
170: if (metric.getMeasurements() != null) {
171: metricElement.setAttribute("has-measurements",
172: "yes");
173: }
174: }
175: }
176:
177: if (ar.getBaseLine() != null) {
178: Element baseLineElement = document
179: .createElement("baseline");
180: ret.appendChild(baseLineElement);
181: baseLineElement.appendChild(new BasicResultsRenderer(
182: new RenderRequest(ar.getBaseLine()))
183: .render(document));
184: }
185:
186: return ret;
187: }
188:
189: // /**
190: // * @param metricElement
191: // * @param metric
192: // */
193: // private void renderMeasurement(Element metricElement, String prefix, Metric.Measurement measurement) {
194: // if (measurement instanceof HammurapiMeasurement) {
195: // SourceMarker source=((HammurapiMeasurement) measurement).getSource();
196: // if (source.getSourceURL()!=null) {
197: // metricElement.setAttribute(prefix+"-source", source.getSourceURL());
198: // }
199: //
200: // metricElement.setAttribute(prefix+"-line", String.valueOf(source.getLine()));
201: // metricElement.setAttribute(prefix+"-col", String.valueOf(source.getColumn()));
202: // }
203: // }
204:
205: private void inspectorSummary(Collection inspectorSummaryList,
206: Element holder) {
207: Iterator it = inspectorSummaryList.iterator();
208: while (it.hasNext()) {
209: Element summaryElement = holder.getOwnerDocument()
210: .createElement("inspector-summary");
211: holder.appendChild(summaryElement);
212: InspectorSummary rsEntry = (InspectorSummary) it.next();
213: summaryElement.setAttribute("inspector", rsEntry.getName());
214: if (rsEntry.getVersion() != null) {
215: summaryElement.setAttribute("version", rsEntry
216: .getVersion());
217: }
218: summaryElement.setAttribute("description", rsEntry
219: .getDescription());
220: summaryElement.setAttribute("severity", rsEntry
221: .getSeverity().toString());
222: summaryElement.setAttribute("count", String.valueOf(rsEntry
223: .getLocationsCount()));
224: summaryElement.setAttribute("baseline", String
225: .valueOf(rsEntry.getBaseLineLocationsCount()));
226: if (rsEntry.getLocations() != null) {
227: summaryElement.setAttribute("has-locations", "yes");
228: }
229: }
230: }
231:
232: public static String format(double d) {
233: String ret = String.valueOf(d);
234: int idx = ret.lastIndexOf('.');
235: if (idx == -1) {
236: return ret + ".00";
237: } else {
238: return (ret + "000").substring(0, idx + 3);
239: }
240: }
241: }
|