KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > reporters > HTMLReporter


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.reporters;
21
22 import java.awt.image.*;
23 import java.io.*;
24 import java.util.*;
25
26 import javax.imageio.*;
27
28 import org.apache.velocity.*;
29 import org.apache.velocity.app.*;
30 import org.jfree.chart.*;
31 import org.polepos.framework.*;
32
33
34 public class HTMLReporter extends GraphReporter {
35     private static final String JavaDoc TEMPLATEDIRNAME = "templates";
36     public final static String JavaDoc ENCODING = "utf-8";
37     public final static String JavaDoc OUTDIRNAME="doc/results/html";
38     
39     private File outdir=null;
40     private List<Circuit> circuits=new ArrayList<Circuit>();
41     private List<Lap> laps=new ArrayList<Lap>();
42     private VelocityEngine engine=null;
43     private Graph graph=null;
44     
45     protected void report(Graph graph) {
46         try {
47             Circuit oldcircuit=circuit();
48             if(oldcircuit!=graph.circuit()) {
49                 if(oldcircuit==null) {
50                     setup();
51                 }
52                 renderCircuitPage();
53                 circuits.add(graph.circuit());
54                 laps.clear();
55             }
56             this.graph=graph;
57             laps.add(graph.lap());
58             renderLapGraph(graph);
59             renderLapPage();
60         } catch (Exception JavaDoc exc) {
61             exc.printStackTrace();
62         }
63     }
64
65     private String JavaDoc lapFilePrefix() {
66         return circuit().internalName()+"_"+lap().name();
67     }
68
69     protected void finish() {
70         renderCircuitPage();
71         renderIndexPage();
72         copyStylesheet();
73     }
74     
75     private void setup() throws Exception JavaDoc {
76         outdir=new File(OUTDIRNAME);
77         outdir.mkdirs();
78         engine = new VelocityEngine();
79         engine.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
80         engine.setProperty(VelocityEngine.ENCODING_DEFAULT,ENCODING);
81         engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,TEMPLATEDIRNAME);
82         engine.init();
83     }
84
85     private void renderIndexPage() {
86         List<TeamCar> distinct = new ArrayList<TeamCar>();
87         for(TeamCar teamCar :graph.teamCars()){
88             if(!distinct.contains(teamCar)){
89                 distinct.add(teamCar);
90             }
91         }
92         VelocityContext context = new VelocityContext();
93         context.put("includefile", "index.vhtml");
94         context.put("teamcars", distinct);
95         context.put("circuits", circuits);
96         renderPage("index.html", context);
97     }
98
99     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 JavaDoc 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()+".jpg"));
123     }
124
125     private void renderPage(String JavaDoc targetName,VelocityContext context) {
126         BufferedWriter out=null;
127         try {
128             Template template=engine.getTemplate("baselayout.vhtml");
129             out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir,targetName)),ENCODING));
130             template.merge(context,out);
131             out.close();
132         } catch (Exception JavaDoc exc) {
133             exc.printStackTrace();
134         }
135         finally {
136             if(out!=null) {
137                 try {
138                     out.close();
139                 } catch (IOException e) {
140                     e.printStackTrace();
141                 }
142             }
143         }
144     }
145     
146     private void copyStylesheet() {
147         File sourcefile=new File(new File(TEMPLATEDIRNAME),"style.css");
148         File targetfile=new File(new File(OUTDIRNAME),"style.css");
149         copyFile(sourcefile, targetfile);
150     }
151
152     private void copyFile(File sourcefile, File targetfile) {
153         BufferedInputStream in=null;
154         BufferedOutputStream out=null;
155         try {
156             in=new BufferedInputStream(new FileInputStream(sourcefile));
157             out=new BufferedOutputStream(new FileOutputStream(targetfile));
158             byte[] buffer=new byte[4096];
159             int bytesread=0;
160             while((bytesread=in.read(buffer))>=0) {
161                 out.write(buffer,0,bytesread);
162             }
163         } catch (IOException e) {
164             e.printStackTrace();
165         }
166         finally {
167             try {
168                 if(in!=null) {
169                     in.close();
170                 }
171                 if(out!=null) {
172                     out.close();
173                 }
174             } catch (IOException e) {
175                 e.printStackTrace();
176             }
177         }
178     }
179     
180     private Lap lap() {
181         return getLast(laps);
182     }
183
184     private Circuit circuit() {
185         return getLast(circuits);
186     }
187     
188     private <Item> Item getLast(List<Item> list) {
189         if(list.isEmpty()) {
190             return null;
191         }
192         return list.get(list.size()-1);
193     }
194 }
195
Popular Tags