KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > render > dom > AggregatedResultsRenderer


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
24 package org.hammurapi.render.dom;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Collections 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 import java.util.Properties JavaDoc;
33
34 import org.hammurapi.HammurapiMeasurement;
35 import org.hammurapi.Violation;
36 import org.hammurapi.results.AggregatedResults;
37 import org.hammurapi.results.Annotation;
38 import org.hammurapi.results.InlineAnnotation;
39 import org.hammurapi.results.InspectorSummary;
40 import org.hammurapi.results.LinkedAnnotation;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43
44 import com.pavelvlasov.metrics.Metric;
45 import com.pavelvlasov.render.RenderRequest;
46 import com.pavelvlasov.render.RenderingException;
47 import com.pavelvlasov.review.SourceMarker;
48
49 /**
50  *
51  * @author Pavel Vlasov
52  * @version $Revision: 1.9 $
53  */

54 public class AggregatedResultsRenderer extends BasicResultsRenderer {
55     
56     public AggregatedResultsRenderer(RenderRequest request) {
57         super(request);
58     }
59     
60     public AggregatedResultsRenderer(RenderRequest request, String JavaDoc profile) {
61         super(request, profile);
62     }
63     
64     public Element JavaDoc render(Document JavaDoc document) throws RenderingException {
65         Element JavaDoc ret=super.render(document);
66         AggregatedResults ar=(AggregatedResults) request.getRenderee();
67
68         ret.setAttribute("new", ar.isNew() ? "yes" : "no");
69         
70         Map JavaDoc severitySummary = ar.getSeveritySummary();
71         if (severitySummary!=null) {
72             Iterator JavaDoc it=severitySummary.entrySet().iterator();
73             while (it.hasNext()) {
74                 Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
75                 Element JavaDoc summaryElement=document.createElement("severity-summary");
76                 ret.appendChild(summaryElement);
77                 summaryElement.setAttribute("severity", entry.getKey().toString());
78                 List JavaDoc srs=new LinkedList JavaDoc(((Map JavaDoc) entry.getValue()).values());
79                 Collections.sort(srs);
80                 inspectorSummary(srs, summaryElement);
81             }
82         }
83         
84         Collection JavaDoc warnings = ar.getWarnings();
85         if (warnings!=null && !warnings.isEmpty()) {
86             Element JavaDoc we=document.createElement("warnings");
87             ret.appendChild(we);
88             Iterator JavaDoc wit=warnings.iterator();
89             while (wit.hasNext()) {
90                 Violation warning=(Violation) wit.next();
91                 ViolationRenderer vr=new ViolationRenderer(new RenderRequest(warning));
92                 we.appendChild(vr.render(document));
93             }
94         }
95         
96         Collection JavaDoc annotations = ar.getAnnotations();
97         if (annotations!=null && !annotations.isEmpty()) {
98             Element JavaDoc annotationsElement=document.createElement("annotations");
99             ret.appendChild(annotationsElement);
100             Iterator JavaDoc ait=annotations.iterator();
101             while (ait.hasNext()) {
102                 Annotation annotation=(Annotation) ait.next();
103                 Element JavaDoc annotationElement=document.createElement("annotation");
104                 annotationsElement.appendChild(annotationElement);
105                 annotationElement.setAttribute("name", annotation.getName());
106                 if (annotation instanceof LinkedAnnotation) {
107                     annotationElement.setAttribute("path", ((LinkedAnnotation) annotation).getPath());
108                 }
109                 
110                 if (annotation instanceof InlineAnnotation) {
111                     Element JavaDoc annotationContentElement=document.createElement("content");
112                     annotationElement.appendChild(annotationContentElement);
113                     annotationContentElement.appendChild(document.createTextNode(((InlineAnnotation) annotation).getContent()));
114                 }
115                 
116                 Properties JavaDoc properties=annotation.getProperties();
117                 if (properties!=null) {
118                     Iterator JavaDoc prit=properties.entrySet().iterator();
119                     while (prit.hasNext()) {
120                         Map.Entry JavaDoc entry=(Map.Entry JavaDoc) prit.next();
121                         Element JavaDoc propertyElement=document.createElement("property");
122                         annotationElement.appendChild(propertyElement);
123                         propertyElement.setAttribute("name", (String JavaDoc) entry.getKey());
124                         propertyElement.appendChild(document.createTextNode((String JavaDoc) entry.getValue()));
125                     }
126                 }
127             }
128         }
129
130         Map JavaDoc metrics = ar.getMetrics();
131         if (metrics!=null) {
132             Iterator JavaDoc it=metrics.entrySet().iterator();
133             while (it.hasNext()) {
134                 Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
135                 Element JavaDoc metricElement=document.createElement("metric");
136                 ret.appendChild(metricElement);
137                 metricElement.setAttribute("name", (String JavaDoc) entry.getKey());
138                 Metric metric=(Metric) entry.getValue();
139                 metricElement.setAttribute("number", String.valueOf(metric.getNumber()));
140                 metricElement.setAttribute("avg", format(metric.getAvg()));
141                 metricElement.setAttribute("min", format(metric.getMin()));
142                 metricElement.setAttribute("max", format(metric.getMax()));
143                 metricElement.setAttribute("total", format(metric.getTotal()));
144                 if (metric.getMeasurements()!=null) {
145                     metricElement.setAttribute("has-measurements", "yes");
146                 }
147             }
148         }
149         
150         if (ar.getBaseLine()!=null) {
151             Element JavaDoc baseLineElement=document.createElement("baseline");
152             ret.appendChild(baseLineElement);
153             baseLineElement.appendChild(new BasicResultsRenderer(new RenderRequest(ar.getBaseLine())).render(document));
154         }
155         
156         return ret;
157     }
158     
159 // /**
160
// * @param metricElement
161
// * @param metric
162
// */
163
// private void renderMeasurement(Element metricElement, String prefix, Metric.Measurement measurement) {
164
// if (measurement instanceof HammurapiMeasurement) {
165
// SourceMarker source=((HammurapiMeasurement) measurement).getSource();
166
// if (source.getSourceURL()!=null) {
167
// metricElement.setAttribute(prefix+"-source", source.getSourceURL());
168
// }
169
//
170
// metricElement.setAttribute(prefix+"-line", String.valueOf(source.getLine()));
171
// metricElement.setAttribute(prefix+"-col", String.valueOf(source.getColumn()));
172
// }
173
// }
174

175     private void inspectorSummary(Collection JavaDoc inspectorSummaryList, Element JavaDoc holder) {
176         Iterator JavaDoc it=inspectorSummaryList.iterator();
177         while (it.hasNext()) {
178             Element JavaDoc summaryElement=holder.getOwnerDocument().createElement("inspector-summary");
179             holder.appendChild(summaryElement);
180             InspectorSummary rsEntry=(InspectorSummary) it.next();
181             summaryElement.setAttribute("inspector", rsEntry.getName());
182             if (rsEntry.getVersion()!=null) {
183                 summaryElement.setAttribute("version", rsEntry.getVersion());
184             }
185             summaryElement.setAttribute("description", rsEntry.getDescription());
186             summaryElement.setAttribute("severity", rsEntry.getSeverity().toString());
187             summaryElement.setAttribute("count", String.valueOf(rsEntry.getLocationsCount()));
188             summaryElement.setAttribute("baseline", String.valueOf(rsEntry.getBaseLineLocationsCount()));
189             if (rsEntry.getLocations()!=null) {
190                 summaryElement.setAttribute("has-locations", "yes");
191             }
192         }
193     }
194
195     public static String JavaDoc format(double d) {
196         String JavaDoc ret=String.valueOf(d);
197         int idx=ret.lastIndexOf('.');
198         if (idx==-1) {
199             return ret+".00";
200         } else {
201             return (ret+"000").substring(0, idx+3);
202         }
203     }
204 }
205
Popular Tags