Coverage Report - com.jeantessier.metrics.XMLPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLPrinter
96%
98/102
65%
25/38
2.267
 
 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  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  
 }