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