PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/report/XMLReportGenerator.java

http://testability-explorer.googlecode.com/
Java | 122 lines | 91 code | 16 blank | 15 comment | 5 complexity | 6c1946b6606c9d01df19e5db55502ef9 MD5 | raw file
Possible License(s): Apache-2.0
  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. */
  16. package com.google.test.metric.report;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.TreeSet;
  20. import org.xml.sax.ContentHandler;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. import com.google.test.metric.ClassCost;
  24. import com.google.test.metric.CostModel;
  25. import com.google.test.metric.MethodCost;
  26. import com.google.test.metric.ViolationCost;
  27. public class XMLReportGenerator extends SummaryReportModel implements ReportGenerator {
  28. private final ContentHandler out;
  29. private final CostModel costModel;
  30. public static final String CLASS_NODE = "class";
  31. public static final String CLASS_NAME_ATTRIBUTE = ClassCost.CLASS_NAME;
  32. public static final String CLASS_COST_ATTRIBUTE = "cost";
  33. public static final String METHOD_NODE = "method";
  34. public static final String METHOD_NAME_ATTRIBUTE = MethodCost.METHOD_NAME_ATTRIBUTE;
  35. public static final String METHOD_OVERALL_COST_ATTRIBUTE = "overall";
  36. public XMLReportGenerator(ContentHandler out, CostModel costModel, int maxExcellentCost,
  37. int maxAcceptableCost, int worstOffenderCount) {
  38. super(costModel, maxExcellentCost, maxAcceptableCost, worstOffenderCount);
  39. this.out = out;
  40. this.costModel = costModel;
  41. }
  42. public XMLReportGenerator(ContentHandler out, CostModel costModel, ReportOptions options) {
  43. this(out, costModel, options.getMaxExcellentCost(),
  44. options.getMaxAcceptableCost(), options.getWorstOffenderCount());
  45. }
  46. public void printHeader() {
  47. }
  48. public void printFooter() {
  49. try {
  50. out.startDocument();
  51. Map<String, Object> values = new HashMap<String, Object>();
  52. values.put("overall", getOverall());
  53. values.put("excellent", excellentCount);
  54. values.put("good", goodCount);
  55. values.put("needsWork", needsWorkCount);
  56. startElement("testability", values);
  57. for (ClassCost classCost : worstOffenders) {
  58. writeCost(classCost);
  59. }
  60. endElement("testability");
  61. out.endDocument();
  62. } catch (SAXException e) {
  63. throw new RuntimeException(e);
  64. }
  65. }
  66. private void writeElement(String elementName, Map<String, Object> values)
  67. throws SAXException {
  68. startElement(elementName, values);
  69. endElement(elementName);
  70. }
  71. private void endElement(String elementName) throws SAXException {
  72. out.endElement(null, elementName, elementName);
  73. }
  74. private void startElement(String elementName, Map<String, Object> values)
  75. throws SAXException {
  76. AttributesImpl atts = new AttributesImpl();
  77. for (String key : new TreeSet<String>(values.keySet())) {
  78. atts.addAttribute(null, key, key, null,
  79. values.get(key) == null ? "" : values.get(key).toString());
  80. }
  81. out.startElement(null, elementName, elementName, atts);
  82. }
  83. void writeCost(ViolationCost violation) throws SAXException {
  84. Map<String, Object> attributes = violation.getAttributes();
  85. attributes.put("overall", costModel.computeOverall(violation.getCost()));
  86. writeElement("cost", attributes);
  87. }
  88. void writeCost(MethodCost methodCost) throws SAXException {
  89. Map<String, Object> attributes = methodCost.getAttributes();
  90. attributes.put(METHOD_OVERALL_COST_ATTRIBUTE, costModel.computeOverall(methodCost.getTotalCost()));
  91. startElement(METHOD_NODE, attributes);
  92. for (ViolationCost violation : methodCost.getViolationCosts()) {
  93. writeCost(violation);
  94. }
  95. endElement(METHOD_NODE);
  96. }
  97. void writeCost(ClassCost classCost) throws SAXException {
  98. Map<String, Object> attributes = classCost.getAttributes();
  99. attributes.put(CLASS_COST_ATTRIBUTE, costModel.computeClass(classCost));
  100. startElement(CLASS_NODE, attributes);
  101. for (MethodCost cost : classCost.getMethods()) {
  102. writeCost(cost);
  103. }
  104. endElement(CLASS_NODE);
  105. }
  106. }