001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public class XMLPrinter extends Printer {
039: public static final String DEFAULT_ENCODING = "utf-8";
040: public static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd";
041:
042: private MetricsConfiguration configuration;
043:
044: public XMLPrinter(PrintWriter out,
045: MetricsConfiguration configuration) {
046: this (out, configuration, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
047: }
048:
049: public XMLPrinter(PrintWriter out,
050: MetricsConfiguration configuration, String encoding,
051: String dtdPrefix) {
052: super (out);
053:
054: this .configuration = configuration;
055:
056: appendHeader(encoding, dtdPrefix);
057: }
058:
059: private void appendHeader(String encoding, String dtdPrefix) {
060: append("<?xml version=\"1.0\" encoding=\"").append(encoding)
061: .append("\" ?>").eol();
062: eol();
063: append("<!DOCTYPE metrics SYSTEM \"").append(dtdPrefix).append(
064: "/metrics.dtd\">").eol();
065: eol();
066: }
067:
068: public void visitMetrics(Metrics metrics) {
069: indent().append("<metrics>").eol();
070: raiseIndent();
071:
072: visitProjectMetrics(metrics);
073:
074: lowerIndent();
075: indent().append("</metrics>").eol();
076: }
077:
078: private void visitProjectMetrics(Metrics metrics) {
079: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
080: || !metrics.isEmpty()) {
081: indent().append("<project>").eol();
082: raiseIndent();
083: indent().append("<name>").append(metrics.getName()).append(
084: "</name>").eol();
085:
086: visitMeasurements(metrics, configuration
087: .getProjectMeasurements());
088:
089: for (Metrics subMetrics : metrics.getSubMetrics()) {
090: visitGroupMetrics(subMetrics);
091: }
092:
093: lowerIndent();
094: indent().append("</project>").eol();
095: }
096: }
097:
098: private void visitGroupMetrics(Metrics metrics) {
099: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
100: || !metrics.isEmpty()) {
101: indent().append("<group>").eol();
102: raiseIndent();
103: indent().append("<name>").append(metrics.getName()).append(
104: "</name>").eol();
105:
106: visitMeasurements(metrics, configuration
107: .getGroupMeasurements());
108:
109: for (Metrics subMetrics : metrics.getSubMetrics()) {
110: visitClassMetrics(subMetrics);
111: }
112:
113: lowerIndent();
114: indent().append("</group>").eol();
115: }
116: }
117:
118: private void visitClassMetrics(Metrics metrics) {
119: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
120: || !metrics.isEmpty()) {
121: indent().append("<class>").eol();
122: raiseIndent();
123: indent().append("<name>").append(metrics.getName()).append(
124: "</name>").eol();
125:
126: visitMeasurements(metrics, configuration
127: .getClassMeasurements());
128:
129: for (Metrics subMetrics : metrics.getSubMetrics()) {
130: visitMethodMetrics(subMetrics);
131: }
132:
133: lowerIndent();
134: indent().append("</class>").eol();
135: }
136: }
137:
138: private void visitMethodMetrics(Metrics metrics) {
139: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
140: || !metrics.isEmpty()) {
141: indent().append("<method>").eol();
142: raiseIndent();
143: indent().append("<name>").append(metrics.getName()).append(
144: "</name>").eol();
145:
146: visitMeasurements(metrics, configuration
147: .getMethodMeasurements());
148:
149: lowerIndent();
150: indent().append("</method>").eol();
151: }
152: }
153:
154: private void visitMeasurements(Metrics metrics,
155: List<MeasurementDescriptor> descriptors) {
156: for (MeasurementDescriptor descriptor : descriptors) {
157: if (isShowHiddenMeasurements() || descriptor.isVisible()) {
158: metrics.getMeasurement(descriptor.getShortName())
159: .accept(this );
160: }
161: }
162: }
163:
164: public void visitStatisticalMeasurement(
165: StatisticalMeasurement measurement) {
166: indent().append("<measurement>").eol();
167: raiseIndent();
168: indent().append("<short-name>").append(
169: measurement.getShortName()).append("</short-name>")
170: .eol();
171: indent().append("<long-name>")
172: .append(measurement.getLongName()).append(
173: "</long-name>").eol();
174: indent().append("<value>").append(measurement.doubleValue())
175: .append("</value>").eol();
176: indent().append("<minimum>").append(measurement.getMinimum())
177: .append("</minimum>").eol();
178: indent().append("<median>").append(measurement.getMedian())
179: .append("</median>").eol();
180: indent().append("<average>").append(measurement.getAverage())
181: .append("</average>").eol();
182: indent().append("<standard-deviation>").append(
183: measurement.getStandardDeviation()).append(
184: "</standard-deviation>").eol();
185: indent().append("<maximum>").append(measurement.getMaximum())
186: .append("</maximum>").eol();
187: indent().append("<sum>").append(measurement.getSum()).append(
188: "</sum>").eol();
189: indent().append("<nb-data-points>").append(
190: measurement.getNbDataPoints()).append(
191: "</nb-data-points>").eol();
192: lowerIndent();
193: indent().append("</measurement>").eol();
194: }
195:
196: public void visitContextAccumulatorMeasurement(
197: ContextAccumulatorMeasurement measurement) {
198: visitCollectionMeasurement(measurement);
199: }
200:
201: public void visitNameListMeasurement(NameListMeasurement measurement) {
202: visitCollectionMeasurement(measurement);
203: }
204:
205: public void visitSubMetricsAccumulatorMeasurement(
206: SubMetricsAccumulatorMeasurement measurement) {
207: visitCollectionMeasurement(measurement);
208: }
209:
210: protected void visitCollectionMeasurement(
211: CollectionMeasurement measurement) {
212: indent().append("<measurement>").eol();
213: raiseIndent();
214: indent().append("<short-name>").append(
215: measurement.getShortName()).append("</short-name>")
216: .eol();
217: indent().append("<long-name>")
218: .append(measurement.getLongName()).append(
219: "</long-name>").eol();
220: indent().append("<value>").append(measurement.getValue())
221: .append("</value>").eol();
222: indent().append("<members>").eol();
223: raiseIndent();
224: for (Object member : measurement.getValues()) {
225: indent().append("<member>").append(member).append(
226: "</member>").eol();
227: }
228: lowerIndent();
229: indent().append("</members>").eol();
230: lowerIndent();
231: indent().append("</measurement>").eol();
232: }
233:
234: protected void visitMeasurement(Measurement measurement) {
235: indent().append("<measurement>").eol();
236: raiseIndent();
237: indent().append("<short-name>").append(
238: measurement.getShortName()).append("</short-name>")
239: .eol();
240: indent().append("<long-name>")
241: .append(measurement.getLongName()).append(
242: "</long-name>").eol();
243: indent().append("<value>").append(measurement.getValue())
244: .append("</value>").eol();
245: lowerIndent();
246: indent().append("</measurement>").eol();
247: }
248: }
|