1 23 24 package org.hammurapi.render.dom; 25 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Properties ; 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 ; 42 import org.w3c.dom.Element ; 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 54 public class AggregatedResultsRenderer extends BasicResultsRenderer { 55 56 public AggregatedResultsRenderer(RenderRequest request) { 57 super(request); 58 } 59 60 public AggregatedResultsRenderer(RenderRequest request, String profile) { 61 super(request, profile); 62 } 63 64 public Element render(Document document) throws RenderingException { 65 Element ret=super.render(document); 66 AggregatedResults ar=(AggregatedResults) request.getRenderee(); 67 68 ret.setAttribute("new", ar.isNew() ? "yes" : "no"); 69 70 Map severitySummary = ar.getSeveritySummary(); 71 if (severitySummary!=null) { 72 Iterator it=severitySummary.entrySet().iterator(); 73 while (it.hasNext()) { 74 Map.Entry entry=(Map.Entry ) it.next(); 75 Element summaryElement=document.createElement("severity-summary"); 76 ret.appendChild(summaryElement); 77 summaryElement.setAttribute("severity", entry.getKey().toString()); 78 List srs=new LinkedList (((Map ) entry.getValue()).values()); 79 Collections.sort(srs); 80 inspectorSummary(srs, summaryElement); 81 } 82 } 83 84 Collection warnings = ar.getWarnings(); 85 if (warnings!=null && !warnings.isEmpty()) { 86 Element we=document.createElement("warnings"); 87 ret.appendChild(we); 88 Iterator 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 annotations = ar.getAnnotations(); 97 if (annotations!=null && !annotations.isEmpty()) { 98 Element annotationsElement=document.createElement("annotations"); 99 ret.appendChild(annotationsElement); 100 Iterator ait=annotations.iterator(); 101 while (ait.hasNext()) { 102 Annotation annotation=(Annotation) ait.next(); 103 Element 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 annotationContentElement=document.createElement("content"); 112 annotationElement.appendChild(annotationContentElement); 113 annotationContentElement.appendChild(document.createTextNode(((InlineAnnotation) annotation).getContent())); 114 } 115 116 Properties properties=annotation.getProperties(); 117 if (properties!=null) { 118 Iterator prit=properties.entrySet().iterator(); 119 while (prit.hasNext()) { 120 Map.Entry entry=(Map.Entry ) prit.next(); 121 Element propertyElement=document.createElement("property"); 122 annotationElement.appendChild(propertyElement); 123 propertyElement.setAttribute("name", (String ) entry.getKey()); 124 propertyElement.appendChild(document.createTextNode((String ) entry.getValue())); 125 } 126 } 127 } 128 } 129 130 Map metrics = ar.getMetrics(); 131 if (metrics!=null) { 132 Iterator it=metrics.entrySet().iterator(); 133 while (it.hasNext()) { 134 Map.Entry entry=(Map.Entry ) it.next(); 135 Element metricElement=document.createElement("metric"); 136 ret.appendChild(metricElement); 137 metricElement.setAttribute("name", (String ) 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 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 175 private void inspectorSummary(Collection inspectorSummaryList, Element holder) { 176 Iterator it=inspectorSummaryList.iterator(); 177 while (it.hasNext()) { 178 Element 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 format(double d) { 196 String 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 |