X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fhtml%2FHTMLStreamElement.java;fp=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fhtml%2FHTMLStreamElement.java;h=fdb821c8488bc91be43cbfeaa7b3bcb4533310b6;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLStreamElement.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLStreamElement.java new file mode 100644 index 000000000..fdb821c84 --- /dev/null +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLStreamElement.java @@ -0,0 +1,72 @@ +package org.simantics.document.linking.report.html; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintStream; + +/** + * Base class for single HTML stream. + * + * @author Marko Luukkainen + * + */ + +public class HTMLStreamElement implements HTMLElement{ + + protected HTMLStreamElement parent; + protected File file; + protected PrintStream os; + + public HTMLStreamElement(File file) throws Exception{ + parent = null; + this.file = file; + os = new PrintStream(file,"UTF-8"); + } + + public HTMLStreamElement(HTMLStreamElement parent) throws Exception{ + this.parent = parent; + openStream(); + } + + private void openStream() throws IOException { + file = File.createTempFile("report_content", ".html"); + os = new PrintStream(file,"UTF-8"); + } + + + protected void copyData(File source, PrintStream dest)throws Exception { + BufferedInputStream is = new BufferedInputStream(new FileInputStream(source)); + while (true) { + int read = is.read(); + if (read == -1) + break; + dest.write(read); + } + is.close(); + } + + public PrintStream getPrintStream() { + return os; + } + + /** + * Closes the stream and copies the contents to the parent stream. + * @throws Exception + */ + public void close() throws Exception { + os.flush(); + os.close(); + if (parent != null) { + copyData(file, parent.os); + file.delete(); + } + os = null; + } + + @Override + public String getId() { + return null; + } +}