X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fhtml%2FHTMLDocument.java;fp=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fhtml%2FHTMLDocument.java;h=11cc7a8f1cbfd333ada4dc28871385ce62bdb5ec;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLDocument.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLDocument.java new file mode 100644 index 000000000..11cc7a8f1 --- /dev/null +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLDocument.java @@ -0,0 +1,231 @@ +package org.simantics.document.linking.report.html; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URL; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.osgi.framework.Bundle; +import org.simantics.document.linking.Activator; +import org.simantics.document.linking.report.Document; +import org.simantics.document.linking.report.DocumentElement; +import org.simantics.document.linking.report.DocumentItem; +import org.simantics.document.linking.report.DocumentTitlePage; +import org.simantics.document.linking.report.Table; +import org.simantics.document.linking.report.TableOfContents; +import org.simantics.document.linking.report.TextItem; +import org.simantics.document.linking.report.URLItem; +import org.simantics.document.linking.report.base.TextItemImpl; +import org.simantics.utils.datastructures.Arrays; + + +/** + * HTML format reporter. + * + * @author Marko Luukkainen + * + */ +public class HTMLDocument extends HTMLStreamElement implements Document { + + + boolean referCSS = false; // if true, generated report refers to bas css in the plug-in. Otherwise the base css's contents are copied to report file. + + int currentPage = 0; + int currentLine = 1; + + HTMLStreamElement content; + + private Map, Integer> uniqueIndex = new HashMap, Integer>(); + + + @Override + public String getId() { + return null; + } + + public HTMLDocument(File file) throws Exception { + super(file); + content = new HTMLStreamElement(this); + } + + String getUniqueId(Class cls) { + Integer index = uniqueIndex.get(cls); + if (index == null) + index = 1; + else + index = index + 1; + uniqueIndex.put(cls,index); + + return cls.getSimpleName() +"_"+index; + } + String getUniqueId(HTMLElement element) { + Class cls = element.getClass(); + return getUniqueId(cls); + } + + private static String getDataUrl() throws IOException { + Bundle b = Platform.getBundle(Activator.PLUGIN_ID); + URL dataUrl = FileLocator.find(b, new Path("report"), null); + URL fileUrl = FileLocator.toFileURL(dataUrl); + return URLDecoder.decode(fileUrl.getPath(), "UTF-8"); + } + + private void copyBaseCSS(String cssUrl) throws Exception { + File f = new File(URLDecoder.decode(cssUrl, "UTF-8")).getAbsoluteFile(); + copyData(f, os); + } + + + private void header() throws Exception { + PrintStream resultOs = os; + String cssUrl = getDataUrl() +"/report.css"; + resultOs.println(""); + resultOs.println(""); + resultOs.println(""); + resultOs.println(" "); + resultOs.println(" Report"); + if (referCSS) + resultOs.println(" "); + resultOs.println(" "); + resultOs.println(""); + resultOs.println(""); + } + + private void footer() throws Exception{ + if (currentTable != null) + currentTable.endTable(); + if (toc != null) + toc.close(); + content.close(); + PrintStream resultOs = os; + resultOs.println(""); + resultOs.println(""); + } + + @Override + public void close() throws Exception { + footer(); + super.close(); + } + + + @Override + public int getCurrentLine() { + return currentLine; + } + + @Override + public int getAvailableLines() { + return Integer.MAX_VALUE; + } + + @Override + public void nextPage() throws Exception { + if (currentPage == 0) + header(); + currentPage++; + currentLine = 1; + } + + @SuppressWarnings("unchecked") + @Override + public T newElement(Class cls,String...options) throws Exception { + if (cls == Table.class) { + return (T)newTable(Arrays.contains(options, Document.TOC)); + } else if (cls == TableOfContents.class) { + return (T)getOrCreateToc(); + } else if (cls == DocumentTitlePage.class) { + if (titlePage != null) + throw new Exception("Document many have only one title page"); + titlePage = new HTMLTitlePage(this); + return (T)titlePage; + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public T nextElement(Class cls,String...options) throws Exception { + if (cls == Table.class) { + return (T)nextTable(Arrays.contains(options, Document.TOC)); + } else if (cls == TableOfContents.class) { + return (T)getOrCreateToc(); + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public T getCurrentElement(Class cls) { + if (cls == Table.class) { + return (T)getCurrentTable(); + } else if (cls == TableOfContents.class) { + return (T)toc; + } else if (cls == DocumentTitlePage.class) { + return (T)titlePage; + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public T newItem(Class cls, String... options) + throws Exception { + if (cls == TextItem.class) + return (T)new TextItemImpl(); + if (cls == URLItem.class) + return (T)new HTMLURLItemImpl(); + return null; + } + + private HTMLTable currentTable; + + + public Table nextTable(boolean id) throws Exception { + if (currentTable != null) { + currentTable.endTable(); + currentTable = new HTMLTable(currentTable,id); + return currentTable; + } else { + return currentTable; + } + + } + + + public Table newTable(boolean id) throws Exception { + if (currentTable != null) { + currentTable.endTable(); + } + currentTable = new HTMLTable(this,content.os,id); + return currentTable; + } + + + public Table getCurrentTable() { + return currentTable; + } + + HTMLTocElement toc; + + public TableOfContents getOrCreateToc() throws Exception{ + if (toc == null) { + toc = new HTMLTocElement(this); + } + return toc; + } + + HTMLTitlePage titlePage; + +}