/testability-explorer/src/main/java/com/google/test/metric/report/XMLReportGenerator.java
Java | 122 lines | 91 code | 16 blank | 15 comment | 5 complexity | 6c1946b6606c9d01df19e5db55502ef9 MD5 | raw file
1/* 2 * Copyright 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16package com.google.test.metric.report; 17 18import java.util.HashMap; 19import java.util.Map; 20import java.util.TreeSet; 21 22import org.xml.sax.ContentHandler; 23import org.xml.sax.SAXException; 24import org.xml.sax.helpers.AttributesImpl; 25 26import com.google.test.metric.ClassCost; 27import com.google.test.metric.CostModel; 28import com.google.test.metric.MethodCost; 29import com.google.test.metric.ViolationCost; 30 31public class XMLReportGenerator extends SummaryReportModel implements ReportGenerator { 32 33 private final ContentHandler out; 34 private final CostModel costModel; 35 public static final String CLASS_NODE = "class"; 36 public static final String CLASS_NAME_ATTRIBUTE = ClassCost.CLASS_NAME; 37 public static final String CLASS_COST_ATTRIBUTE = "cost"; 38 public static final String METHOD_NODE = "method"; 39 public static final String METHOD_NAME_ATTRIBUTE = MethodCost.METHOD_NAME_ATTRIBUTE; 40 public static final String METHOD_OVERALL_COST_ATTRIBUTE = "overall"; 41 42 public XMLReportGenerator(ContentHandler out, CostModel costModel, int maxExcellentCost, 43 int maxAcceptableCost, int worstOffenderCount) { 44 super(costModel, maxExcellentCost, maxAcceptableCost, worstOffenderCount); 45 this.out = out; 46 this.costModel = costModel; 47 } 48 49 public XMLReportGenerator(ContentHandler out, CostModel costModel, ReportOptions options) { 50 this(out, costModel, options.getMaxExcellentCost(), 51 options.getMaxAcceptableCost(), options.getWorstOffenderCount()); 52 } 53 54 public void printHeader() { 55 } 56 57 public void printFooter() { 58 try { 59 out.startDocument(); 60 Map<String, Object> values = new HashMap<String, Object>(); 61 values.put("overall", getOverall()); 62 values.put("excellent", excellentCount); 63 values.put("good", goodCount); 64 values.put("needsWork", needsWorkCount); 65 startElement("testability", values); 66 for (ClassCost classCost : worstOffenders) { 67 writeCost(classCost); 68 } 69 endElement("testability"); 70 out.endDocument(); 71 } catch (SAXException e) { 72 throw new RuntimeException(e); 73 } 74 } 75 76 private void writeElement(String elementName, Map<String, Object> values) 77 throws SAXException { 78 startElement(elementName, values); 79 endElement(elementName); 80 } 81 82 private void endElement(String elementName) throws SAXException { 83 out.endElement(null, elementName, elementName); 84 } 85 86 private void startElement(String elementName, Map<String, Object> values) 87 throws SAXException { 88 AttributesImpl atts = new AttributesImpl(); 89 for (String key : new TreeSet<String>(values.keySet())) { 90 atts.addAttribute(null, key, key, null, 91 values.get(key) == null ? "" : values.get(key).toString()); 92 } 93 out.startElement(null, elementName, elementName, atts); 94 } 95 96 void writeCost(ViolationCost violation) throws SAXException { 97 Map<String, Object> attributes = violation.getAttributes(); 98 attributes.put("overall", costModel.computeOverall(violation.getCost())); 99 writeElement("cost", attributes); 100 } 101 102 void writeCost(MethodCost methodCost) throws SAXException { 103 Map<String, Object> attributes = methodCost.getAttributes(); 104 attributes.put(METHOD_OVERALL_COST_ATTRIBUTE, costModel.computeOverall(methodCost.getTotalCost())); 105 startElement(METHOD_NODE, attributes); 106 for (ViolationCost violation : methodCost.getViolationCosts()) { 107 writeCost(violation); 108 } 109 endElement(METHOD_NODE); 110 } 111 112 void writeCost(ClassCost classCost) throws SAXException { 113 Map<String, Object> attributes = classCost.getAttributes(); 114 attributes.put(CLASS_COST_ATTRIBUTE, costModel.computeClass(classCost)); 115 startElement(CLASS_NODE, attributes); 116 for (MethodCost cost : classCost.getMethods()) { 117 writeCost(cost); 118 } 119 endElement(CLASS_NODE); 120 } 121 122}