2 * Created on Jun 24, 2003
5 package isac.util.genhtml;
7 import isac.util.InformationProcessor;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.StringReader;
13 import java.util.HashSet;
14 import java.util.Iterator;
16 import java.util.StringTokenizer;
17 import java.util.Vector;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.transform.Source;
23 import javax.xml.transform.stream.StreamSource;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.NodeList;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
32 * @author Andreas Griesmayer
35 public class GenHTML {
37 static final int PBLTREE_XSL = 1;
39 static final int METTREE_XSL = 2;
41 static final int EXPTREE_XSL = 3;
43 static final int SIMPLE_HIERARCHY_XSL = 4;
45 static final int SIMPLE_PBL_NODE = 5;
47 static String destination_ = "/home/neuper/proto2/www/kbase/";
49 public static void main(String[] argv) {
50 if (argv.length > 0) destination_ = argv[0];
51 GenHTML genhtml = new GenHTML();
52 genhtml.buildHierarchy("met/code.html");
53 genhtml.buildHierarchy("met/index.html");
54 genhtml.buildHierarchy("met/heading.html");
55 genhtml.buildHierarchy("met/menu_empty.html");
56 genhtml.buildHierarchy("met/main.html");
58 genhtml.buildHierarchy("pbl/code.html");
59 genhtml.buildHierarchy("pbl/index.html");
60 genhtml.buildHierarchy("pbl/heading.html");
61 genhtml.buildHierarchy("pbl/menu_empty.html");
62 genhtml.buildHierarchy("pbl/main.html");
64 genhtml.buildHierarchy("exp/code.html");
65 genhtml.buildHierarchy("exp/index.html");
66 genhtml.buildHierarchy("exp/heading.html");
67 genhtml.buildHierarchy("exp/menu_empty.html");
68 genhtml.buildHierarchy("exp/main.html");
70 Set exp_set = genhtml.loadFileList("exp");
71 Iterator exp_it = exp_set.iterator();
72 int index = exp_set.size();
73 while (exp_it.hasNext()) {
74 System.out.println("exp to generate:" + (index--));
75 genhtml.loadContent("exp/" + exp_it.next());
78 Set pbl_set = genhtml.loadFileList("pbl");
79 index = pbl_set.size();
80 Iterator pbl_it = pbl_set.iterator();
81 while (pbl_it.hasNext()) {
82 System.out.println("pbl to generate:" + (index--));
83 genhtml.loadContent("pbl/" + pbl_it.next());
86 Set met_set = genhtml.loadFileList("met");
87 index = met_set.size();
88 Iterator met_it = met_set.iterator();
89 while (met_it.hasNext()) {
90 System.out.println("met to generate:" + (index--));
91 genhtml.loadContent("met/" + met_it.next());
96 Set loadFileList(String type) {
97 Set return_set = new HashSet();
100 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
101 DocumentBuilder db = dbf.newDocumentBuilder();
102 String h_xml = InformationProcessor.loadHierarchy("", type);
103 InputSource inputsource = new InputSource(new StringReader(h_xml));
104 Document document = db.parse(inputsource);
106 Element doc_elem = document.getDocumentElement();
107 NodeList nodelist = doc_elem.getElementsByTagName("CONTENTREF");
108 for (int index = 0; index < nodelist.getLength(); index++) {
109 return_set.add(nodelist.item(index).getFirstChild()
110 .getNodeValue().trim());
113 nodelist = doc_elem.getElementsByTagName("EXAMPLE");
114 for (int index = 0; index < nodelist.getLength(); index++) {
115 return_set.add(nodelist.item(index).getFirstChild()
116 .getNodeValue().trim());
118 } catch (ParserConfigurationException e) {
120 } catch (SAXException e) {
122 } catch (IOException e) {
129 * Loads all hierarchies which are displayed on the web-browser
131 protected void buildHierarchy(String path) {
133 StringTokenizer tokens = new StringTokenizer(path, "/");
134 // (String)request.getSession().getAttribute("session_id");
138 String type = tokens.nextToken();
140 if (!tokens.hasMoreTokens())
141 path = "/index.html";
143 while (tokens.hasMoreTokens())
144 path += "/" + tokens.nextToken();
146 if (path.endsWith("code.html")) {
147 Vector parameter = new Vector();
148 parameter.add(session);
151 Source xslSource = null;
152 Source xmlSource = new StreamSource(new StringReader(
153 InformationProcessor.loadHierarchy(session, type)));
154 if (type.equals("pbl"))
155 xslSource = new StreamSource(
157 .getResourceAsStream(
158 "/isac/util/genhtml/templates/pbl_hierarchy_jmenu.xsl"));
159 else if (type.equals("met"))
160 xslSource = new StreamSource(
162 .getResourceAsStream(
163 "/isac/util/genhtml/templates/met_hierarchy_jmenu.xsl"));
165 xslSource = new StreamSource(
167 .getResourceAsStream(
168 "/isac/util/genhtml/templates/exp_hierarchy_jmenu.xsl"));
169 String header = toString(getClass().getResourceAsStream(
170 "/isac/util/genhtml/templates/codeheader.html"));
171 String footer = toString(getClass().getResourceAsStream(
172 "/isac/util/genhtml/templates/codefooter.html"));
174 // Perform the transformation, sending the output to the
176 //java.io.PrintWriter out = response.getWriter();
177 FileWriter out = new FileWriter(destination_ + type + "/"
180 Encoder.encodeInformation(xslSource, xmlSource, out);
184 if (path.trim().endsWith("index.html")) {
185 if (type.equals("pbl")) {
186 path = "index_pbl.html";
187 } else if (type.equals("met"))
188 path = "index_met.html";
190 path = "index_exp.html";
192 System.out.println(path);
193 InputStream istream = getClass().getResourceAsStream(
194 "/isac/util/genhtml/jscript/" + path);
195 FileWriter out = new FileWriter(destination_ + type + "/"
198 while ((input = istream.read()) != -1) {
204 } catch (IOException exc) {
205 System.out.println("IOException:" + exc.getMessage());
209 private String toString(InputStream istream) {
210 StringBuffer buf = new StringBuffer(10000);
213 while ((input = istream.read()) != -1) {
214 buf.append((char) input);
216 } catch (IOException e) {
217 return getErrorXML("Sorry, IOException while accessing the InputStream "
220 return buf.toString();
223 private static String getErrorXML(String string) {
224 return "<ERROR><DESCRIPTION> <h1> Error in file LoadHierarchy.java </h1>"
225 + string + "</DESCRIPTION></ERROR>";
228 protected void loadFile(String path) {
229 StringTokenizer tokens = new StringTokenizer(path, "/");
230 // (String)request.getSession().getAttribute("session_id");
233 String session_id = "";
235 String type = (String) tokens.nextToken();
237 if (!tokens.hasMoreTokens())
238 path = "/index.html";
240 while (tokens.hasMoreTokens())
241 path += "/" + tokens.nextToken();
243 if (path.endsWith("code.html")) {
244 Vector parameter = new Vector();
245 parameter.add(session_id);
248 Source xmlSource = new StreamSource(new StringReader(
249 InformationProcessor.loadHierarchy(session_id, type)));
251 // Perform the transformation, sending the output to the
253 java.io.FileWriter out = new FileWriter(destination_ + type
255 Encoder.encodeHierarchy(type, xmlSource, out);
257 } else if (path.trim().endsWith("heading.html")) {
258 java.io.FileWriter out = new FileWriter(destination_ + type
260 out.write(getHeader(session_id));
262 } else if (path.trim().endsWith("treecontent.html")) {
263 Vector parameter = new Vector();
264 parameter.add(session_id);
267 Source xmlSource = new StreamSource(new StringReader(
268 InformationProcessor.loadHierarchy(session_id, type)));
270 // Perform the transformation, sending the output to the
272 java.io.FileWriter out = new FileWriter(destination_ + type
275 Encoder.encodeHContent(type, xmlSource, out);
278 //TODO this type should not result in different files but in an
279 // autogeneration of a fitting file
280 if (path.trim().endsWith("index.html")) {
281 if (type.equals("pbl")) {
282 path = "index_pbl.html";
283 } else if (type.equals("met"))
284 path = "index_met.html";
286 path = "index_exp.html";
288 InputStream istream = getClass().getResourceAsStream(
289 "/isac/BrowserServlet/jscript/" + path);
290 java.io.FileWriter out = new FileWriter(destination_ + type + "/"
293 while ((input = istream.read()) != -1) {
297 } catch (IOException exc) {
298 System.out.println("bla");
302 private String getHeader(String session_id) {
303 String headercontent = "<html>"
305 + "<a href=\"../../isac.BrowserServlet.Login\" target=\"text\">"
308 + "<a href=\"../../isac.BrowserServlet.Logout\" target=\"text\">"
310 + "<br> <a href=\"../../isac.BrowserServlet.LoadFile/pbl/index.html\" target=\"_top\">PBLHierarchy</a>"
311 + " <a href=\"../../isac.BrowserServlet.LoadFile/met/index.html\" target=\"_top\">METHierarchy</a>"
312 + " <a href=\"../../isac.BrowserServlet.LoadFile/exp/index.html\" target=\"_top\">ExampleHierarchy</a>"
316 if ((username = InformationProcessor.getUsername(session_id)) == null)
317 headercontent += "not logged in";
319 headercontent += " welcome <b>" + username + "</b>";
320 headercontent += "</body> </html>";
321 return headercontent;
324 protected void loadContent(String path) {
325 StringTokenizer tokens = new StringTokenizer(path, "/");
329 Vector parameter = new Vector();
330 parameter.add(session);
332 Source xslSource = null;
333 Source xmlSource = null;
335 String command = tokens.nextToken();
336 String header = "<?xml version=\"1.0\"?> \n"
337 + "<?xml-stylesheet href=\"http://pear.math.pitt.edu/mathzilla/Examples/mml.css\" type=\"text/css\"?>\n"
338 + "<!DOCTYPE html SYSTEM \"mathml.dtd_\">\n" +
339 //"<?xml-stylesheet type=\"text/xsl\"
340 // href=\"http://www.w3.org/Math/XSL/pmathml.xsl\"?>\n"+
341 //" <HTML xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
342 "<HTML xmlns=\"http://www.w3.org/1999/xhtml\"\n"
343 + "xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n"
344 + "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
347 String footer = "</BODY>\n</HTML>";
348 // if(command.equals("pbl-node")){
350 parameter.add(command);
351 if (!tokens.hasMoreElements()) {
352 out = new FileWriter(destination_ + path + ".html");
354 out.write("no details");
358 String contentfile = tokens.nextToken();
359 parameter.add(contentfile);
361 if (!contentfile.trim().endsWith(".xml")) { return; }
362 out = new FileWriter(destination_ + path + ".html");
363 xmlSource = new StreamSource(new StringReader(InformationProcessor
364 .loadXML(session, command, contentfile)));
365 xslSource = new StreamSource(getClass().getResourceAsStream(
366 "/isac/util/genhtml/templates/pbl_node_content.xsl"));
368 // Perform the transformation, sending the output to the response.
370 Encoder.encodeInformation(xslSource, xmlSource, out);
372 } catch (IOException exc) {
373 exc.printStackTrace();
375 // arg1.getWriter().print("pathinfo: "+arg0.getPathInfo()+"\n
376 // transl:"+arg0.getPathTranslated());