1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
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 | 2 | this(out, configuration, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX); |
46 | 2 | } |
47 | |
|
48 | |
public XMLPrinter(PrintWriter out, MetricsConfiguration configuration, String encoding, String dtdPrefix) { |
49 | 5 | super(out); |
50 | |
|
51 | 5 | this.configuration = configuration; |
52 | |
|
53 | 5 | appendHeader(encoding, dtdPrefix); |
54 | 5 | } |
55 | |
|
56 | |
private void appendHeader(String encoding, String dtdPrefix) { |
57 | 5 | append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>").eol(); |
58 | 5 | eol(); |
59 | 5 | append("<!DOCTYPE metrics SYSTEM \"").append(dtdPrefix).append("/metrics.dtd\">").eol(); |
60 | 5 | eol(); |
61 | 5 | } |
62 | |
|
63 | |
public void visitMetrics(Metrics metrics) { |
64 | 1 | indent().append("<metrics>").eol(); |
65 | 1 | raiseIndent(); |
66 | |
|
67 | 1 | visitProjectMetrics(metrics); |
68 | |
|
69 | 1 | lowerIndent(); |
70 | 1 | indent().append("</metrics>").eol(); |
71 | 1 | } |
72 | |
|
73 | |
private void visitProjectMetrics(Metrics metrics) { |
74 | 1 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
75 | 1 | indent().append("<project>").eol(); |
76 | 1 | raiseIndent(); |
77 | 1 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
78 | |
|
79 | 1 | visitMeasurements(metrics, configuration.getProjectMeasurements()); |
80 | |
|
81 | 1 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
82 | 1 | visitGroupMetrics(subMetrics); |
83 | |
} |
84 | |
|
85 | 1 | lowerIndent(); |
86 | 1 | indent().append("</project>").eol(); |
87 | |
} |
88 | 1 | } |
89 | |
|
90 | |
private void visitGroupMetrics(Metrics metrics) { |
91 | 1 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
92 | 1 | indent().append("<group>").eol(); |
93 | 1 | raiseIndent(); |
94 | 1 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
95 | |
|
96 | 1 | visitMeasurements(metrics, configuration.getGroupMeasurements()); |
97 | |
|
98 | 1 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
99 | 1 | visitClassMetrics(subMetrics); |
100 | |
} |
101 | |
|
102 | 1 | lowerIndent(); |
103 | 1 | indent().append("</group>").eol(); |
104 | |
} |
105 | 1 | } |
106 | |
|
107 | |
private void visitClassMetrics(Metrics metrics) { |
108 | 1 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
109 | 1 | indent().append("<class>").eol(); |
110 | 1 | raiseIndent(); |
111 | 1 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
112 | |
|
113 | 1 | visitMeasurements(metrics, configuration.getClassMeasurements()); |
114 | |
|
115 | 1 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
116 | 2 | visitMethodMetrics(subMetrics); |
117 | |
} |
118 | |
|
119 | 1 | lowerIndent(); |
120 | 1 | indent().append("</class>").eol(); |
121 | |
} |
122 | 1 | } |
123 | |
|
124 | |
private void visitMethodMetrics(Metrics metrics) { |
125 | 2 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
126 | 2 | indent().append("<method>").eol(); |
127 | 2 | raiseIndent(); |
128 | 2 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
129 | |
|
130 | 2 | visitMeasurements(metrics, configuration.getMethodMeasurements()); |
131 | |
|
132 | 2 | lowerIndent(); |
133 | 2 | indent().append("</method>").eol(); |
134 | |
} |
135 | 2 | } |
136 | |
|
137 | |
private void visitMeasurements(Metrics metrics, List<MeasurementDescriptor> descriptors) { |
138 | 5 | for (MeasurementDescriptor descriptor : descriptors) { |
139 | 140 | if (isShowHiddenMeasurements() || descriptor.isVisible()) { |
140 | 138 | metrics.getMeasurement(descriptor.getShortName()).accept(this); |
141 | |
} |
142 | |
} |
143 | 5 | } |
144 | |
|
145 | |
public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { |
146 | 19 | indent().append("<measurement>").eol(); |
147 | 19 | raiseIndent(); |
148 | 19 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
149 | 19 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
150 | 19 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
151 | 19 | indent().append("<minimum>").append(measurement.getMinimum()).append("</minimum>").eol(); |
152 | 19 | indent().append("<median>").append(measurement.getMedian()).append("</median>").eol(); |
153 | 19 | indent().append("<average>").append(measurement.getAverage()).append("</average>").eol(); |
154 | 19 | indent().append("<standard-deviation>").append(measurement.getStandardDeviation()).append("</standard-deviation>").eol(); |
155 | 19 | indent().append("<maximum>").append(measurement.getMaximum()).append("</maximum>").eol(); |
156 | 19 | indent().append("<sum>").append(measurement.getSum()).append("</sum>").eol(); |
157 | 19 | indent().append("<nb-data-points>").append(measurement.getNbDataPoints()).append("</nb-data-points>").eol(); |
158 | 19 | lowerIndent(); |
159 | 19 | indent().append("</measurement>").eol(); |
160 | 19 | } |
161 | |
|
162 | |
public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { |
163 | 0 | visitCollectionMeasurement(measurement); |
164 | 0 | } |
165 | |
|
166 | |
public void visitNameListMeasurement(NameListMeasurement measurement) { |
167 | 23 | visitCollectionMeasurement(measurement); |
168 | 23 | } |
169 | |
|
170 | |
public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { |
171 | 0 | visitCollectionMeasurement(measurement); |
172 | 0 | } |
173 | |
|
174 | |
protected void visitCollectionMeasurement(CollectionMeasurement measurement) { |
175 | 23 | indent().append("<measurement>").eol(); |
176 | 23 | raiseIndent(); |
177 | 23 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
178 | 23 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
179 | 23 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
180 | 23 | indent().append("<members>").eol(); |
181 | 23 | raiseIndent(); |
182 | 23 | for (Object member : measurement.getValues()) { |
183 | 13 | indent().append("<member>").append(member).append("</member>").eol(); |
184 | |
} |
185 | 23 | lowerIndent(); |
186 | 23 | indent().append("</members>").eol(); |
187 | 23 | lowerIndent(); |
188 | 23 | indent().append("</measurement>").eol(); |
189 | 23 | } |
190 | |
|
191 | |
protected void visitMeasurement(Measurement measurement) { |
192 | 96 | indent().append("<measurement>").eol(); |
193 | 96 | raiseIndent(); |
194 | 96 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
195 | 96 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
196 | 96 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
197 | 96 | lowerIndent(); |
198 | 96 | indent().append("</measurement>").eol(); |
199 | 96 | } |
200 | |
} |