1 package org.simantics.document.linking.report.html;
4 import java.io.IOException;
5 import java.io.PrintStream;
7 import java.net.URLDecoder;
8 import java.util.HashMap;
11 import org.eclipse.core.runtime.FileLocator;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.core.runtime.Platform;
14 import org.osgi.framework.Bundle;
15 import org.simantics.document.linking.Activator;
16 import org.simantics.document.linking.report.Document;
17 import org.simantics.document.linking.report.DocumentElement;
18 import org.simantics.document.linking.report.DocumentItem;
19 import org.simantics.document.linking.report.DocumentTitlePage;
20 import org.simantics.document.linking.report.Table;
21 import org.simantics.document.linking.report.TableOfContents;
22 import org.simantics.document.linking.report.TextItem;
23 import org.simantics.document.linking.report.URLItem;
24 import org.simantics.document.linking.report.base.TextItemImpl;
25 import org.simantics.utils.datastructures.Arrays;
29 * HTML format reporter.
31 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34 public class HTMLDocument extends HTMLStreamElement implements Document {
37 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.
42 HTMLStreamElement content;
44 private Map<Class<? extends HTMLElement>, Integer> uniqueIndex = new HashMap<Class<? extends HTMLElement>, Integer>();
48 public String getId() {
52 public HTMLDocument(File file) throws Exception {
54 content = new HTMLStreamElement(this);
57 String getUniqueId(Class<? extends HTMLElement> cls) {
58 Integer index = uniqueIndex.get(cls);
63 uniqueIndex.put(cls,index);
65 return cls.getSimpleName() +"_"+index;
67 String getUniqueId(HTMLElement element) {
68 Class<? extends HTMLElement> cls = element.getClass();
69 return getUniqueId(cls);
72 private static String getDataUrl() throws IOException {
73 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
74 URL dataUrl = FileLocator.find(b, new Path("report"), null);
75 URL fileUrl = FileLocator.toFileURL(dataUrl);
76 return URLDecoder.decode(fileUrl.getPath(), "UTF-8");
79 private void copyBaseCSS(String cssUrl) throws Exception {
80 File f = new File(URLDecoder.decode(cssUrl, "UTF-8")).getAbsoluteFile();
85 private void header() throws Exception {
86 PrintStream resultOs = os;
87 String cssUrl = getDataUrl() +"/report.css";
88 resultOs.println("<!DOCTYPE html>");
89 resultOs.println("<html lang=\"en\">");
90 resultOs.println("<head>");
91 resultOs.println(" <meta charset=\"utf-8\">");
92 resultOs.println(" <title>Report</title>");
94 resultOs.println(" <link rel=\"stylesheet\" href=\"" + cssUrl + "\" type=\"text/css\">");
95 resultOs.println(" <style>");
100 resultOs.println(" </style>");
101 resultOs.println("</head>");
102 resultOs.println("<body>");
105 private void footer() throws Exception{
106 if (currentTable != null)
107 currentTable.endTable();
111 PrintStream resultOs = os;
112 resultOs.println("</body>");
113 resultOs.println("</html>");
117 public void close() throws Exception {
124 public int getCurrentLine() {
129 public int getAvailableLines() {
130 return Integer.MAX_VALUE;
134 public void nextPage() throws Exception {
135 if (currentPage == 0)
141 @SuppressWarnings("unchecked")
143 public <T extends DocumentElement> T newElement(Class<T> cls,String...options) throws Exception {
144 if (cls == Table.class) {
145 return (T)newTable(Arrays.contains(options, Document.TOC));
146 } else if (cls == TableOfContents.class) {
147 return (T)getOrCreateToc();
148 } else if (cls == DocumentTitlePage.class) {
149 if (titlePage != null)
150 throw new Exception("Document many have only one title page");
151 titlePage = new HTMLTitlePage(this);
157 @SuppressWarnings("unchecked")
159 public <T extends DocumentElement> T nextElement(Class<T> cls,String...options) throws Exception {
160 if (cls == Table.class) {
161 return (T)nextTable(Arrays.contains(options, Document.TOC));
162 } else if (cls == TableOfContents.class) {
163 return (T)getOrCreateToc();
168 @SuppressWarnings("unchecked")
170 public <T extends DocumentElement> T getCurrentElement(Class<T> cls) {
171 if (cls == Table.class) {
172 return (T)getCurrentTable();
173 } else if (cls == TableOfContents.class) {
175 } else if (cls == DocumentTitlePage.class) {
181 @SuppressWarnings("unchecked")
183 public <T extends DocumentItem> T newItem(Class<T> cls, String... options)
185 if (cls == TextItem.class)
186 return (T)new TextItemImpl();
187 if (cls == URLItem.class)
188 return (T)new HTMLURLItemImpl();
192 private HTMLTable currentTable;
195 public Table nextTable(boolean id) throws Exception {
196 if (currentTable != null) {
197 currentTable.endTable();
198 currentTable = new HTMLTable(currentTable,id);
207 public Table newTable(boolean id) throws Exception {
208 if (currentTable != null) {
209 currentTable.endTable();
211 currentTable = new HTMLTable(this,content.os,id);
216 public Table getCurrentTable() {
222 public TableOfContents getOrCreateToc() throws Exception{
224 toc = new HTMLTocElement(this);
229 HTMLTitlePage titlePage;