1 32 33 package com.jeantessier.metrics; 34 35 import java.io.*; 36 import java.util.*; 37 38 public class XMLPrinter extends Printer { 39 public static final String DEFAULT_ENCODING = "utf-8"; 40 public static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd"; 41 42 private MetricsConfiguration configuration; 43 44 public XMLPrinter(PrintWriter out, MetricsConfiguration configuration) { 45 this(out, configuration, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX); 46 } 47 48 public XMLPrinter(PrintWriter out, MetricsConfiguration configuration, String encoding, String dtdPrefix) { 49 super(out); 50 51 this.configuration = configuration; 52 53 appendHeader(encoding, dtdPrefix); 54 } 55 56 private void appendHeader(String encoding, String dtdPrefix) { 57 append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>").eol(); 58 eol(); 59 append("<!DOCTYPE metrics SYSTEM \"").append(dtdPrefix).append("/metrics.dtd\">").eol(); 60 eol(); 61 } 62 63 public void visitMetrics(Metrics metrics) { 64 indent().append("<metrics>").eol(); 65 raiseIndent(); 66 67 visitProjectMetrics(metrics); 68 69 lowerIndent(); 70 indent().append("</metrics>").eol(); 71 } 72 73 private void visitProjectMetrics(Metrics metrics) { 74 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 75 indent().append("<project>").eol(); 76 raiseIndent(); 77 indent().append("<name>").append(metrics.getName()).append("</name>").eol(); 78 79 visitMeasurements(metrics, configuration.getProjectMeasurements()); 80 81 Iterator i = metrics.getSubMetrics().iterator(); 82 while (i.hasNext()) { 83 visitGroupMetrics((Metrics) i.next()); 84 } 85 86 lowerIndent(); 87 indent().append("</project>").eol(); 88 } 89 } 90 91 private void visitGroupMetrics(Metrics metrics) { 92 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 93 indent().append("<group>").eol(); 94 raiseIndent(); 95 indent().append("<name>").append(metrics.getName()).append("</name>").eol(); 96 97 visitMeasurements(metrics, configuration.getGroupMeasurements()); 98 99 Iterator i = metrics.getSubMetrics().iterator(); 100 while (i.hasNext()) { 101 visitClassMetrics((Metrics) i.next()); 102 } 103 104 lowerIndent(); 105 indent().append("</group>").eol(); 106 } 107 } 108 109 private void visitClassMetrics(Metrics metrics) { 110 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 111 indent().append("<class>").eol(); 112 raiseIndent(); 113 indent().append("<name>").append(metrics.getName()).append("</name>").eol(); 114 115 visitMeasurements(metrics, configuration.getClassMeasurements()); 116 117 Iterator i = metrics.getSubMetrics().iterator(); 118 while (i.hasNext()) { 119 visitMethodMetrics((Metrics) i.next()); 120 } 121 122 lowerIndent(); 123 indent().append("</class>").eol(); 124 } 125 } 126 127 private void visitMethodMetrics(Metrics metrics) { 128 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 129 indent().append("<method>").eol(); 130 raiseIndent(); 131 indent().append("<name>").append(metrics.getName()).append("</name>").eol(); 132 133 visitMeasurements(metrics, configuration.getMethodMeasurements()); 134 135 lowerIndent(); 136 indent().append("</method>").eol(); 137 } 138 } 139 140 private void visitMeasurements(Metrics metrics, List descriptors) { 141 Iterator i = descriptors.iterator(); 142 while (i.hasNext()) { 143 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 144 145 if (isShowHiddenMeasurements() || descriptor.isVisible()) { 146 metrics.getMeasurement(descriptor.getShortName()).accept(this); 147 } 148 } 149 } 150 151 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 152 indent().append("<measurement>").eol(); 153 raiseIndent(); 154 indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); 155 indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); 156 indent().append("<value>").append(measurement.doubleValue()).append("</value>").eol(); 157 indent().append("<minimum>").append(measurement.getMinimum()).append("</minimum>").eol(); 158 indent().append("<median>").append(measurement.getMedian()).append("</median>").eol(); 159 indent().append("<average>").append(measurement.getAverage()).append("</average>").eol(); 160 indent().append("<standard-deviation>").append(measurement.getStandardDeviation()).append("</standard-deviation>").eol(); 161 indent().append("<maximum>").append(measurement.getMaximum()).append("</maximum>").eol(); 162 indent().append("<sum>").append(measurement.getSum()).append("</sum>").eol(); 163 indent().append("<nb-data-points>").append(measurement.getNbDataPoints()).append("</nb-data-points>").eol(); 164 lowerIndent(); 165 indent().append("</measurement>").eol(); 166 } 167 168 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 169 visitCollectionMeasurement(measurement); 170 } 171 172 public void visitNameListMeasurement(NameListMeasurement measurement) { 173 visitCollectionMeasurement(measurement); 174 } 175 176 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 177 visitCollectionMeasurement(measurement); 178 } 179 180 protected void visitCollectionMeasurement(CollectionMeasurement measurement) { 181 indent().append("<measurement>").eol(); 182 raiseIndent(); 183 indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); 184 indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); 185 indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); 186 indent().append("<members>").eol(); 187 raiseIndent(); 188 Iterator i = measurement.getValues().iterator(); 189 while (i.hasNext()) { 190 indent().append("<member>").append(i.next()).append("</member>").eol(); 191 } 192 lowerIndent(); 193 indent().append("</members>").eol(); 194 lowerIndent(); 195 indent().append("</measurement>").eol(); 196 } 197 198 protected void visitMeasurement(Measurement measurement) { 199 indent().append("<measurement>").eol(); 200 raiseIndent(); 201 indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); 202 indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); 203 indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); 204 lowerIndent(); 205 indent().append("</measurement>").eol(); 206 } 207 } 208 | Popular Tags |