KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > XMLPrinter


1 /*
2  * Copyright (c) 2001-2005, 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 JavaDoc DEFAULT_ENCODING = "utf-8";
40     public static final String JavaDoc 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 JavaDoc encoding, String JavaDoc dtdPrefix) {
49         super(out);
50         
51         this.configuration = configuration;
52
53         appendHeader(encoding, dtdPrefix);
54     }
55
56     private void appendHeader(String JavaDoc encoding, String JavaDoc 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