KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > report > IndexPage


1 /*
2  * RoundTripReport.java
3  *
4  * Created on May 9, 2005, 9:59 PM
5  */

6
7 package com.sun.japex.report;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.DataInputStream JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.OutputStreamWriter JavaDoc;
14
15
16 public class IndexPage {
17     static final String JavaDoc REPORT_NEWROW = "<!--{new row}-->";
18     
19     TrendReportParams _params;
20     String JavaDoc _chartName;
21     /** Creates a new instance of RoundTripReport */
22     public IndexPage(TrendReportParams params, String JavaDoc chartName) {
23         _params = params;
24         _chartName = chartName;
25     }
26     
27     public void report() {
28         try {
29             //String filename = args[INDEX_REPORT];
30
String JavaDoc filename = _params.outputPath()+"/index.html";
31             File JavaDoc file = new File JavaDoc(filename);
32             String JavaDoc content = reportContent(file);
33             OutputStreamWriter JavaDoc osr = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file));
34             osr.write(content);
35             osr.close();
36         } catch (Exception JavaDoc e) {
37             e.printStackTrace();
38         }
39     }
40     public String JavaDoc reportContent(File JavaDoc file) {
41         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
42         if (file.exists()) {
43             content.append(readFromFile(file));
44         } else {
45             content.append(getTemplate());
46         }
47         
48         int start = 0;
49         int end = 0;
50         StringBuffer JavaDoc newrow = new StringBuffer JavaDoc();
51         newrow.append("<table width=\"100%\" border=\"0\">");
52         newrow.append("<tr><th width=\"50%\"><h2>"+_params.title());
53         newrow.append("</h2></th><th align=\"right\"><a HREF=\"#top\">Top</a></th></tr>");
54         newrow.append("</table>");
55         newrow.append("<ul>\n<li>Report Path: " + _params.reportPath() + "</li>\n");
56         newrow.append("<li>Oupput Path: " + _params.outputPath() + "</li>\n");
57         newrow.append("<li>Report Period: " + _params.dateFrom() + " - " + _params.dateTo() + "</li>\n");
58         if (_params.isDriverSpecified()) {
59             newrow.append("<li>Driver: " + _params.driver() + "</li>\n");
60         } else {
61             newrow.append("<li>Driver: not specified</li>\n");
62         }
63         newrow.append("<li>Testcase(s): ");
64         if (_params.isTestSpecified()) {
65             for(int i=0; i<_params.test().length; i++) {
66                 newrow.append(_params.test()[i]+" ");
67             }
68         } else {
69             newrow.append("not specified");
70         }
71         newrow.append("</li>\n</ul>\n");
72         newrow.append("<table width=\"80%\" border=\"0\">");
73         newrow.append("<tr><td colspan=\"2\" align=\"center\"><img SRC=\""+_chartName+"\"></td></tr>");
74         newrow.append("</table>");
75         newrow.append(REPORT_NEWROW+"\n");
76
77         start = content.indexOf(REPORT_NEWROW);
78         end = start + REPORT_NEWROW.length();
79         content.replace(start, end, newrow.toString());
80         return content.toString();
81     }
82
83     private String JavaDoc readFromFile(File JavaDoc file) {
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
85         try
86         {
87             FileInputStream JavaDoc fstream = new FileInputStream JavaDoc(file);
88
89             // Convert our input stream to a
90
// DataInputStream
91
DataInputStream JavaDoc in = new DataInputStream JavaDoc(fstream);
92
93             while (in.available() !=0)
94             {
95                     sb.append(in.readLine());
96             }
97
98             in.close();
99         }
100         catch (Exception JavaDoc e)
101         {
102             e.printStackTrace();
103         }
104         return sb.toString();
105     }
106     
107     private String JavaDoc getTemplate() {
108         StringBuffer JavaDoc template = new StringBuffer JavaDoc();
109         template.append("<html>\n<link HREF=\"report.css\" type=\"text/css\" rel=\"stylesheet\"/>\n");
110         template.append("<head><title>Japex Trend Report</title></head>\n<body>\n");
111         template.append("<a name=\"top\"><h1>Japex Trend Report</h1>\n");
112         template.append(REPORT_NEWROW);
113         template.append("<small><hr/><i><font size=\"-2\"><!--datetime--></font></i></small>\n");
114         template.append("</body>\n</html>");
115         return template.toString();
116     }
117     
118 }
119
Popular Tags