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