1 package net.logAnalyzer.reports;
2
3 import java.awt.image.BufferedImage;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.StringReader;
8 import java.io.StringWriter;
9
10 import javax.swing.JComponent;
11 import javax.swing.JEditorPane;
12 import javax.swing.text.BadLocationException;
13 import javax.swing.text.html.HTMLEditorKit;
14 import javax.xml.transform.Result;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerConfigurationException;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.TransformerFactoryConfigurationError;
20 import javax.xml.transform.stream.StreamResult;
21 import javax.xml.transform.stream.StreamSource;
22
23 import net.logAnalyzer.resources.LAResourceBundle;
24 import net.logAnalyzer.utils.XMLUtils;
25
26 import org.w3c.dom.Document;
27
28 /***
29 * This class is a wrapper used to encapsulate an HTML doc in a
30 * {@link net.logAnalyzer.reports.LAReport} object.
31 *
32 * @author David Vicente
33 * @version 0.1
34 */
35 public class HTMLReport implements LAReport {
36 /***
37 * Report name.
38 */
39 private String name = null;
40
41 /***
42 * Wrapped document.
43 */
44 private Document document = null;
45
46 /***
47 * Wrap the specified document in a new wrapper instance.
48 *
49 * @param name
50 * Name of the report.
51 * @param report
52 * Document to wrap as a report.
53 */
54 public HTMLReport(String name, Document report) {
55 this.name = name;
56 this.document = report;
57 }
58
59 /***
60 * Return the file extension of the report : <tt>html</tt>.
61 *
62 * @return Report file name extension.
63 * @see LAReport#getFileExtension()
64 */
65 public String getFileExtension() {
66 return "html";
67 }
68
69 /***
70 * Return the name of the report.
71 *
72 * @return Report name.
73 * @see net.logAnalyzer.reports.LAReport#getName()
74 */
75 public String getName() {
76 return this.name;
77 }
78
79 /***
80 * Set the name of the report.
81 *
82 * @param name
83 * Report name.
84 * @see net.logAnalyzer.reports.LAReport#setName(String)
85 */
86 public void setName(String name) {
87 this.name = name;
88 }
89
90 /***
91 * Return the mime type of the report.
92 *
93 * @return Mime type of the report.
94 * @see net.logAnalyzer.reports.LAReport#getMimeType()
95 */
96 public String getMimeType() {
97 return "text/html";
98 }
99
100 /***
101 * Save the report with the specified filename. The filename can contain a
102 * relative or absolute path.
103 * <p>
104 * If the file exists, it is overwritten.
105 * </p>
106 *
107 * @param filename
108 * Name of the output file.
109 * @throws IOException
110 * If an I/O exception occurs.
111 * @see net.logAnalyzer.reports.LAReport#saveToFile(String)
112 */
113 public void saveToFile(String filename) throws IOException {
114 FileWriter writer = new FileWriter(filename);
115 writer.write(this.toString());
116 writer.close();
117 }
118
119 /***
120 * Create an image from the report as a {@link BufferedImage}.
121 * <p>
122 * This implementation returns always <tt>null</tt>.
123 * </p>
124 *
125 * @param width
126 * Image width.
127 * @param height
128 * Image height.
129 * @return Image from the report; <tt>null</tt> if unsupported feature.
130 */
131 public BufferedImage createBufferedImage(int width, int height) {
132 return null;
133 }
134
135 /***
136 * Create the GUI displaying the report as a {@link JEditorPane} component.
137 *
138 * @return GUI showing the report.
139 * @see net.logAnalyzer.reports.LAReport#createGUI()
140 */
141 public JComponent createGUI() {
142 JEditorPane area = new JEditorPane();
143 area.setName(getName());
144 area.setContentType("text/html");
145 area.setEditorKit(new HTMLEditorKit());
146 area.setEditable(false);
147 try {
148 HTMLEditorKit kit = (HTMLEditorKit) area.getEditorKit();
149 javax.swing.text.Document doc = area.getDocument();
150 doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
151 kit.read(new StringReader(this.toString()), doc, 0);
152 } catch (IOException e) {
153
154 e.printStackTrace();
155 } catch (BadLocationException e) {
156
157 e.printStackTrace();
158 }
159 return area;
160 }
161
162 /***
163 * Serialize the wrapped document to a string.
164 *
165 * @return String serialization of the wrapped document, or <tt>null</tt>
166 * if an {@link IOException} occurs.
167 * @see net.logAnalyzer.reports.LAReport#toString()
168 */
169 public String toString() {
170
171 StringWriter writer = new StringWriter();
172 try {
173 String xmlstring = XMLUtils.toString(document);
174
175 StreamSource source = new StreamSource(new StringReader(xmlstring));
176
177 Result result;
178 result = new StreamResult(writer);
179 String xsltpath = LAResourceBundle
180 .getLocalizedString("Reports.hmtl.renderer.xslt");
181 InputStream xsltstream = this.getClass().getResourceAsStream(
182 xsltpath);
183 Transformer xformer = TransformerFactory.newInstance()
184 .newTransformer(new StreamSource(xsltstream));
185 xformer.transform(source, result);
186
187 } catch (IOException e) {
188
189 e.printStackTrace();
190 } catch (TransformerConfigurationException e) {
191
192 e.printStackTrace();
193 } catch (TransformerFactoryConfigurationError e) {
194
195 e.printStackTrace();
196 } catch (TransformerException e) {
197
198 e.printStackTrace();
199 }
200
201 return writer.getBuffer().toString();
202 }
203 }