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

http://testability-explorer.googlecode.com/ · Java · 66 lines · 37 code · 9 blank · 20 comment · 0 complexity · e930112b49cbd14c1e6a290de949f5e7 MD5 · raw file

  1. /*
  2. * Copyright 2009 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 com.google.test.metric.ClassCost;
  18. import freemarker.template.Configuration;
  19. import freemarker.template.TemplateException;
  20. import java.io.IOException;
  21. import java.io.PrintStream;
  22. import java.io.PrintWriter;
  23. /**
  24. * The base class for ReportGenerator's that use Freemarker for rendering.
  25. * @author alexeagle@google.com (Alex Eagle)
  26. */
  27. public class FreemarkerReportGenerator implements ReportGenerator {
  28. public static final String HTML_REPORT_TEMPLATE = "html/Report.html";
  29. private final ReportModel model;
  30. private final PrintStream out;
  31. private final Configuration cfg;
  32. private final String templateFile;
  33. public FreemarkerReportGenerator(ReportModel model, PrintStream out,
  34. String templateFile, Configuration cfg) {
  35. this.model = model;
  36. this.out = out;
  37. this.cfg = cfg;
  38. this.templateFile = templateFile;
  39. }
  40. public void renderReport(PrintStream out) throws IOException {
  41. try {
  42. cfg.getTemplate(templateFile).process(model, new PrintWriter(out));
  43. out.flush();
  44. } catch (TemplateException e) {
  45. throw new RuntimeException(e);
  46. }
  47. }
  48. public void printHeader() throws IOException {
  49. // do nothing
  50. }
  51. public void addClassCost(ClassCost classCost) {
  52. model.addClassCost(classCost);
  53. }
  54. public void printFooter() throws IOException {
  55. renderReport(out);
  56. }
  57. }