]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/html/HTMLDocument.java
Externalize org.simantics.document.linking.ui
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / html / HTMLDocument.java
1 package org.simantics.document.linking.report.html;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintStream;
6 import java.net.URL;
7 import java.net.URLDecoder;
8 import java.util.HashMap;
9 import java.util.Map;
10
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;
26
27
28 /**
29  * HTML format reporter.
30  * 
31  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32  *
33  */
34 public class HTMLDocument extends HTMLStreamElement implements Document {
35         
36         
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.
38         
39         int currentPage = 0;
40         int currentLine = 1;
41
42         HTMLStreamElement content;
43         
44         private Map<Class<? extends HTMLElement>, Integer> uniqueIndex = new HashMap<Class<? extends HTMLElement>, Integer>();
45         
46         
47         @Override
48         public String getId() {
49                 return null;
50         }
51         
52         public HTMLDocument(File file) throws Exception {
53                 super(file);
54                 content = new HTMLStreamElement(this);
55         }
56         
57         String getUniqueId(Class<? extends HTMLElement> cls) {
58                 Integer index = uniqueIndex.get(cls);
59                 if (index == null)
60                         index = 1;
61                 else
62                         index = index + 1;
63                 uniqueIndex.put(cls,index);
64                 
65                 return cls.getSimpleName() +"_"+index; //$NON-NLS-1$
66         }
67         String getUniqueId(HTMLElement element) {
68                 Class<? extends HTMLElement> cls = element.getClass();
69                 return getUniqueId(cls);
70         }
71         
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); //$NON-NLS-1$
75                  URL fileUrl = FileLocator.toFileURL(dataUrl);
76                  return URLDecoder.decode(fileUrl.getPath(), "UTF-8");  //$NON-NLS-1$
77         }
78         
79         private void copyBaseCSS(String cssUrl) throws Exception {
80                 File f = new File(URLDecoder.decode(cssUrl, "UTF-8")).getAbsoluteFile(); //$NON-NLS-1$
81                 copyData(f, os);
82         }
83
84         
85         private void header() throws Exception {
86                 PrintStream resultOs = os;
87                 String cssUrl = getDataUrl() +"/report.css"; //$NON-NLS-1$
88                 resultOs.println("<!DOCTYPE html>"); //$NON-NLS-1$
89                 resultOs.println("<html lang=\"en\">"); //$NON-NLS-1$
90                 resultOs.println("<head>"); //$NON-NLS-1$
91                 resultOs.println("  <meta charset=\"utf-8\">"); //$NON-NLS-1$
92                 resultOs.println("  <title>Report</title>"); //$NON-NLS-1$
93                 if (referCSS)
94                         resultOs.println("  <link rel=\"stylesheet\" href=\"" + cssUrl + "\" type=\"text/css\">"); //$NON-NLS-1$ //$NON-NLS-2$
95                 resultOs.println("  <style>"); //$NON-NLS-1$
96                 if (!referCSS) {
97                         copyBaseCSS(cssUrl);
98                 }
99                 
100                 resultOs.println("  </style>"); //$NON-NLS-1$
101                 resultOs.println("</head>"); //$NON-NLS-1$
102                 resultOs.println("<body>"); //$NON-NLS-1$
103         }
104
105         private void footer() throws Exception{
106                 if (currentTable != null)
107                         currentTable.endTable();
108                 if (toc != null)
109                         toc.close();
110                 content.close();
111                 PrintStream resultOs = os;
112                 resultOs.println("</body>"); //$NON-NLS-1$
113                 resultOs.println("</html>"); //$NON-NLS-1$
114         }
115         
116         @Override
117         public void close() throws Exception {
118                 footer();
119                 super.close();
120         }
121         
122
123         @Override
124         public int getCurrentLine() {
125                 return currentLine;
126         }
127         
128         @Override
129         public int getAvailableLines() {
130                 return Integer.MAX_VALUE;
131         }
132         
133         @Override
134         public void nextPage() throws Exception {
135                 if (currentPage == 0)
136                         header();       
137                 currentPage++;
138                 currentLine = 1;
139         }
140         
141         @SuppressWarnings("unchecked")
142         @Override
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"); //$NON-NLS-1$
151                         titlePage = new HTMLTitlePage(this);
152                         return (T)titlePage;
153                 }
154                 return null;
155         }
156         
157         @SuppressWarnings("unchecked")
158         @Override
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();
164                 }
165                 return null;
166         }
167         
168         @SuppressWarnings("unchecked")
169         @Override
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) {
174                         return (T)toc;
175                 } else if (cls == DocumentTitlePage.class) {
176                         return (T)titlePage;
177                 }
178                 return null;
179         }
180         
181         @SuppressWarnings("unchecked")
182         @Override
183         public <T extends DocumentItem> T newItem(Class<T> cls, String... options)
184                         throws Exception {
185                 if (cls == TextItem.class)
186                         return (T)new TextItemImpl();
187                 if (cls == URLItem.class)
188                         return (T)new HTMLURLItemImpl();
189                 return null;
190         }
191         
192         private HTMLTable currentTable;
193         
194         
195         public Table nextTable(boolean id) throws Exception {
196                 if (currentTable != null) {
197                         currentTable.endTable();
198                         currentTable = new HTMLTable(currentTable,id);
199                         return currentTable;
200                 } else {
201                         return currentTable;
202                 }
203                 
204         }
205         
206         
207         public Table newTable(boolean id) throws Exception {
208                 if (currentTable != null) {
209                         currentTable.endTable();
210                 }
211                 currentTable = new HTMLTable(this,content.os,id);
212                 return currentTable;
213         }
214         
215         
216         public Table getCurrentTable() {
217                 return currentTable;
218         }
219         
220         HTMLTocElement toc;
221         
222         public TableOfContents getOrCreateToc() throws Exception{
223                 if (toc == null) {
224                         toc = new HTMLTocElement(this);
225                 }
226                 return toc;
227         }
228         
229         HTMLTitlePage titlePage;
230
231 }