1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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 | 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 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
82 | visitGroupMetrics(subMetrics); |
83 | } |
84 | |
85 | lowerIndent(); |
86 | indent().append("</project>").eol(); |
87 | } |
88 | } |
89 | |
90 | private void visitGroupMetrics(Metrics metrics) { |
91 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
92 | indent().append("<group>").eol(); |
93 | raiseIndent(); |
94 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
95 | |
96 | visitMeasurements(metrics, configuration.getGroupMeasurements()); |
97 | |
98 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
99 | visitClassMetrics(subMetrics); |
100 | } |
101 | |
102 | lowerIndent(); |
103 | indent().append("</group>").eol(); |
104 | } |
105 | } |
106 | |
107 | private void visitClassMetrics(Metrics metrics) { |
108 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
109 | indent().append("<class>").eol(); |
110 | raiseIndent(); |
111 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
112 | |
113 | visitMeasurements(metrics, configuration.getClassMeasurements()); |
114 | |
115 | for (Metrics subMetrics : metrics.getSubMetrics()) { |
116 | visitMethodMetrics(subMetrics); |
117 | } |
118 | |
119 | lowerIndent(); |
120 | indent().append("</class>").eol(); |
121 | } |
122 | } |
123 | |
124 | private void visitMethodMetrics(Metrics metrics) { |
125 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
126 | indent().append("<method>").eol(); |
127 | raiseIndent(); |
128 | indent().append("<name>").append(metrics.getName()).append("</name>").eol(); |
129 | |
130 | visitMeasurements(metrics, configuration.getMethodMeasurements()); |
131 | |
132 | lowerIndent(); |
133 | indent().append("</method>").eol(); |
134 | } |
135 | } |
136 | |
137 | private void visitMeasurements(Metrics metrics, List<MeasurementDescriptor> descriptors) { |
138 | for (MeasurementDescriptor descriptor : descriptors) { |
139 | if (isShowHiddenMeasurements() || descriptor.isVisible()) { |
140 | metrics.getMeasurement(descriptor.getShortName()).accept(this); |
141 | } |
142 | } |
143 | } |
144 | |
145 | public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { |
146 | indent().append("<measurement>").eol(); |
147 | raiseIndent(); |
148 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
149 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
150 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
151 | indent().append("<minimum>").append(measurement.getMinimum()).append("</minimum>").eol(); |
152 | indent().append("<median>").append(measurement.getMedian()).append("</median>").eol(); |
153 | indent().append("<average>").append(measurement.getAverage()).append("</average>").eol(); |
154 | indent().append("<standard-deviation>").append(measurement.getStandardDeviation()).append("</standard-deviation>").eol(); |
155 | indent().append("<maximum>").append(measurement.getMaximum()).append("</maximum>").eol(); |
156 | indent().append("<sum>").append(measurement.getSum()).append("</sum>").eol(); |
157 | indent().append("<nb-data-points>").append(measurement.getNbDataPoints()).append("</nb-data-points>").eol(); |
158 | lowerIndent(); |
159 | indent().append("</measurement>").eol(); |
160 | } |
161 | |
162 | public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { |
163 | visitCollectionMeasurement(measurement); |
164 | } |
165 | |
166 | public void visitNameListMeasurement(NameListMeasurement measurement) { |
167 | visitCollectionMeasurement(measurement); |
168 | } |
169 | |
170 | public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { |
171 | visitCollectionMeasurement(measurement); |
172 | } |
173 | |
174 | protected void visitCollectionMeasurement(CollectionMeasurement measurement) { |
175 | indent().append("<measurement>").eol(); |
176 | raiseIndent(); |
177 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
178 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
179 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
180 | indent().append("<members>").eol(); |
181 | raiseIndent(); |
182 | for (Object member : measurement.getValues()) { |
183 | indent().append("<member>").append(member).append("</member>").eol(); |
184 | } |
185 | lowerIndent(); |
186 | indent().append("</members>").eol(); |
187 | lowerIndent(); |
188 | indent().append("</measurement>").eol(); |
189 | } |
190 | |
191 | protected void visitMeasurement(Measurement measurement) { |
192 | indent().append("<measurement>").eol(); |
193 | raiseIndent(); |
194 | indent().append("<short-name>").append(measurement.getShortName()).append("</short-name>").eol(); |
195 | indent().append("<long-name>").append(measurement.getLongName()).append("</long-name>").eol(); |
196 | indent().append("<value>").append(measurement.getValue()).append("</value>").eol(); |
197 | lowerIndent(); |
198 | indent().append("</measurement>").eol(); |
199 | } |
200 | } |