+package org.simantics.document.linking.report.html;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.PrintStream;\r
+\r
+/**\r
+ * Base class for single HTML stream.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+\r
+public class HTMLStreamElement implements HTMLElement{\r
+ \r
+ protected HTMLStreamElement parent;\r
+ protected File file;\r
+ protected PrintStream os;\r
+ \r
+ public HTMLStreamElement(File file) throws Exception{\r
+ parent = null;\r
+ this.file = file;\r
+ os = new PrintStream(file,"UTF-8");\r
+ }\r
+ \r
+ public HTMLStreamElement(HTMLStreamElement parent) throws Exception{\r
+ this.parent = parent;\r
+ openStream();\r
+ }\r
+ \r
+ private void openStream() throws IOException {\r
+ file = File.createTempFile("report_content", ".html");\r
+ os = new PrintStream(file,"UTF-8");\r
+ }\r
+ \r
+\r
+ protected void copyData(File source, PrintStream dest)throws Exception {\r
+ BufferedInputStream is = new BufferedInputStream(new FileInputStream(source));\r
+ while (true) {\r
+ int read = is.read();\r
+ if (read == -1)\r
+ break;\r
+ dest.write(read);\r
+ }\r
+ is.close();\r
+ }\r
+ \r
+ public PrintStream getPrintStream() {\r
+ return os;\r
+ }\r
+ \r
+ /**\r
+ * Closes the stream and copies the contents to the parent stream.\r
+ * @throws Exception\r
+ */\r
+ public void close() throws Exception {\r
+ os.flush();\r
+ os.close();\r
+ if (parent != null) {\r
+ copyData(file, parent.os);\r
+ file.delete();\r
+ }\r
+ os = null;\r
+ }\r
+ \r
+ @Override\r
+ public String getId() {\r
+ return null;\r
+ }\r
+}\r