001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.reporters;
021:
022: import java.awt.image.*;
023: import java.io.*;
024: import java.util.*;
025:
026: import javax.imageio.*;
027:
028: import org.apache.velocity.*;
029: import org.apache.velocity.app.*;
030: import org.jfree.chart.*;
031: import org.polepos.framework.*;
032:
033: public class HTMLReporter extends GraphReporter {
034: private static final String TEMPLATEDIRNAME = "templates";
035: public final static String ENCODING = "utf-8";
036: public final static String OUTDIRNAME = "doc/results/html";
037:
038: private File outdir = null;
039: private List<Circuit> circuits = new ArrayList<Circuit>();
040: private List<Lap> laps = new ArrayList<Lap>();
041: private VelocityEngine engine = null;
042: private Graph graph = null;
043:
044: protected void report(Graph graph) {
045: try {
046: Circuit oldcircuit = circuit();
047: if (oldcircuit != graph.circuit()) {
048: if (oldcircuit == null) {
049: setup();
050: }
051: renderCircuitPage();
052: circuits.add(graph.circuit());
053: laps.clear();
054: }
055: this .graph = graph;
056: laps.add(graph.lap());
057: renderLapGraph(graph);
058: renderLapPage();
059: } catch (Exception exc) {
060: exc.printStackTrace();
061: }
062: }
063:
064: private String lapFilePrefix() {
065: return circuit().internalName() + "_" + lap().name();
066: }
067:
068: protected void finish() {
069: renderCircuitPage();
070: renderIndexPage();
071: copyStylesheet();
072: }
073:
074: private void setup() throws Exception {
075: outdir = new File(OUTDIRNAME);
076: outdir.mkdirs();
077: engine = new VelocityEngine();
078: engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
079: engine.setProperty(VelocityEngine.ENCODING_DEFAULT, ENCODING);
080: engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
081: TEMPLATEDIRNAME);
082: engine.init();
083: }
084:
085: private void renderIndexPage() {
086: List<TeamCar> distinct = new ArrayList<TeamCar>();
087: for (TeamCar teamCar : graph.teamCars()) {
088: if (!distinct.contains(teamCar)) {
089: distinct.add(teamCar);
090: }
091: }
092: VelocityContext context = new VelocityContext();
093: context.put("includefile", "index.vhtml");
094: context.put("teamcars", distinct);
095: context.put("circuits", circuits);
096: renderPage("index.html", context);
097: }
098:
099: private void renderCircuitPage() {
100: if (circuit() == null) {
101: return;
102: }
103: VelocityContext context = new VelocityContext();
104: context.put("includefile", "circuitresults.vhtml");
105: context.put("circuit", circuit());
106: context.put("laps", laps);
107: renderPage(circuit().internalName() + ".html", context);
108: }
109:
110: private void renderLapPage() {
111: String lapfileprefix = lapFilePrefix();
112: VelocityContext context = new VelocityContext();
113: context.put("includefile", "lapresult.vhtml");
114: context.put("graph", graph);
115: context.put("fileprefix", lapfileprefix);
116: renderPage(lapfileprefix + ".html", context);
117: }
118:
119: private void renderLapGraph(Graph graph) throws IOException {
120: JFreeChart chart = new ChartBuilder().createChart(graph);
121: BufferedImage img = chart.createBufferedImage(750, 500);
122: ImageIO.write(img, "jpg", new File(outdir, lapFilePrefix()
123: + ".jpg"));
124: }
125:
126: private void renderPage(String targetName, VelocityContext context) {
127: BufferedWriter out = null;
128: try {
129: Template template = engine.getTemplate("baselayout.vhtml");
130: out = new BufferedWriter(new OutputStreamWriter(
131: new FileOutputStream(new File(outdir, targetName)),
132: ENCODING));
133: template.merge(context, out);
134: out.close();
135: } catch (Exception exc) {
136: exc.printStackTrace();
137: } finally {
138: if (out != null) {
139: try {
140: out.close();
141: } catch (IOException e) {
142: e.printStackTrace();
143: }
144: }
145: }
146: }
147:
148: private void copyStylesheet() {
149: File sourcefile = new File(new File(TEMPLATEDIRNAME),
150: "style.css");
151: File targetfile = new File(new File(OUTDIRNAME), "style.css");
152: copyFile(sourcefile, targetfile);
153: }
154:
155: private void copyFile(File sourcefile, File targetfile) {
156: BufferedInputStream in = null;
157: BufferedOutputStream out = null;
158: try {
159: in = new BufferedInputStream(
160: new FileInputStream(sourcefile));
161: out = new BufferedOutputStream(new FileOutputStream(
162: targetfile));
163: byte[] buffer = new byte[4096];
164: int bytesread = 0;
165: while ((bytesread = in.read(buffer)) >= 0) {
166: out.write(buffer, 0, bytesread);
167: }
168: } catch (IOException e) {
169: e.printStackTrace();
170: } finally {
171: try {
172: if (in != null) {
173: in.close();
174: }
175: if (out != null) {
176: out.close();
177: }
178: } catch (IOException e) {
179: e.printStackTrace();
180: }
181: }
182: }
183:
184: private Lap lap() {
185: return getLast(laps);
186: }
187:
188: private Circuit circuit() {
189: return getLast(circuits);
190: }
191:
192: private <Item> Item getLast(List<Item> list) {
193: if (list.isEmpty()) {
194: return null;
195: }
196: return list.get(list.size() - 1);
197: }
198: }
|