]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLStreamElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / html / HTMLStreamElement.java
1 package org.simantics.document.linking.report.html;\r
2 \r
3 import java.io.BufferedInputStream;\r
4 import java.io.File;\r
5 import java.io.FileInputStream;\r
6 import java.io.IOException;\r
7 import java.io.PrintStream;\r
8 \r
9 /**\r
10  * Base class for single HTML stream.\r
11  * \r
12  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
13  *\r
14  */\r
15 \r
16 public class HTMLStreamElement implements HTMLElement{\r
17         \r
18         protected HTMLStreamElement parent;\r
19         protected File file;\r
20         protected PrintStream os;\r
21         \r
22         public HTMLStreamElement(File file) throws Exception{\r
23                 parent = null;\r
24                 this.file = file;\r
25                 os = new PrintStream(file,"UTF-8");\r
26         }\r
27         \r
28         public HTMLStreamElement(HTMLStreamElement parent) throws Exception{\r
29                 this.parent = parent;\r
30                 openStream();\r
31         }\r
32         \r
33         private void openStream() throws IOException {\r
34                 file = File.createTempFile("report_content", ".html");\r
35                 os = new PrintStream(file,"UTF-8");\r
36         }\r
37         \r
38 \r
39         protected void copyData(File source, PrintStream dest)throws Exception {\r
40                 BufferedInputStream is = new BufferedInputStream(new FileInputStream(source));\r
41                 while (true) {\r
42                         int read = is.read();\r
43                         if (read == -1)\r
44                                 break;\r
45                         dest.write(read);\r
46                 }\r
47                 is.close();\r
48         }\r
49         \r
50         public PrintStream getPrintStream() {\r
51                 return os;\r
52         }\r
53         \r
54         /**\r
55          * Closes the stream and copies the contents to the parent stream.\r
56          * @throws Exception\r
57          */\r
58         public void close() throws Exception {\r
59                 os.flush();\r
60                 os.close();\r
61                 if (parent != null) {\r
62                         copyData(file, parent.os);\r
63                         file.delete();\r
64                 }\r
65                 os = null;\r
66         }\r
67         \r
68         @Override\r
69         public String getId() {\r
70                 return null;\r
71         }\r
72 }\r