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_ = "/netshares/studenthomes/mlang/proto_v2/isac/proto2/isac/xmldata/";
48 static String destination_ = "/netshares/studenthomes/mlang/xmldata/";
52 * Method derived from HttpServlet to react on an request by an web-browser
53 * AG? ................^^^^^^^^^^^ ???
56 * object which holds informations about the request like the URL
57 * used to access this servlet
59 * object which provides methods to return the results to the
61 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
62 * javax.servlet.http.HttpServletResponse)
65 public static void main(String[] argv) {
66 if (argv.length > 0) destination_ = argv[0];
67 GenHTML genhtml = new GenHTML();
68 genhtml.buildHierarchy("met/code.html");
69 genhtml.buildHierarchy("met/index.html");
70 genhtml.buildHierarchy("met/heading.html");
71 genhtml.buildHierarchy("met/menu_empty.html");
72 genhtml.buildHierarchy("met/main.html");
74 genhtml.buildHierarchy("pbl/code.html");
75 genhtml.buildHierarchy("pbl/index.html");
76 genhtml.buildHierarchy("pbl/heading.html");
77 genhtml.buildHierarchy("pbl/menu_empty.html");
78 genhtml.buildHierarchy("pbl/main.html");
80 genhtml.buildHierarchy("exp/code.html");
81 genhtml.buildHierarchy("exp/index.html");
82 genhtml.buildHierarchy("exp/heading.html");
83 genhtml.buildHierarchy("exp/menu_empty.html");
84 genhtml.buildHierarchy("exp/main.html");
86 Set exp_set = genhtml.loadFileList("exp");
87 Iterator exp_it = exp_set.iterator();
88 int index = exp_set.size();
89 while (exp_it.hasNext()) {
90 System.out.println("exp to generate:" + (index--));
91 genhtml.loadContent("exp/" + exp_it.next());
94 Set pbl_set = genhtml.loadFileList("pbl");
95 index = pbl_set.size();
96 Iterator pbl_it = pbl_set.iterator();
97 while (pbl_it.hasNext()) {
98 System.out.println("pbl to generate:" + (index--));
99 genhtml.loadContent("pbl/" + pbl_it.next());
102 Set met_set = genhtml.loadFileList("met");
103 index = met_set.size();
104 Iterator met_it = met_set.iterator();
105 while (met_it.hasNext()) {
106 System.out.println("met to generate:" + (index--));
107 genhtml.loadContent("met/" + met_it.next());
112 Set loadFileList(String type) {
113 Set return_set = new HashSet();
116 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
117 DocumentBuilder db = dbf.newDocumentBuilder();
118 String h_xml = InformationProcessor.loadHierarchy("", type);
119 InputSource inputsource = new InputSource(new StringReader(h_xml));
120 Document document = db.parse(inputsource);
122 Element doc_elem = document.getDocumentElement();
123 NodeList nodelist = doc_elem.getElementsByTagName("CONTENTREF");
124 for (int index = 0; index < nodelist.getLength(); index++) {
125 return_set.add(nodelist.item(index).getFirstChild()
126 .getNodeValue().trim());
129 nodelist = doc_elem.getElementsByTagName("EXAMPLE");
130 for (int index = 0; index < nodelist.getLength(); index++) {
131 return_set.add(nodelist.item(index).getFirstChild()
132 .getNodeValue().trim());
134 } catch (ParserConfigurationException e) {
136 } catch (SAXException e) {
138 } catch (IOException e) {
145 * Loads all hierarchies which are displayed on the web-browser
147 protected void buildHierarchy(String path) {
149 StringTokenizer tokens = new StringTokenizer(path, "/");
150 // (String)request.getSession().getAttribute("session_id");
154 String type = tokens.nextToken();
156 if (!tokens.hasMoreTokens())
157 path = "/index.html";
159 while (tokens.hasMoreTokens())
160 path += "/" + tokens.nextToken();
162 if (path.endsWith("code.html")) {
163 Vector parameter = new Vector();
164 parameter.add(session);
167 Source xslSource = null;
168 Source xmlSource = new StreamSource(new StringReader(
169 InformationProcessor.loadHierarchy(session, type)));
170 if (type.equals("pbl"))
171 xslSource = new StreamSource(
173 .getResourceAsStream(
174 "/isac/util/genhtml/templates/pbl_hierarchy_jmenu.xsl"));
175 else if (type.equals("met"))
176 xslSource = new StreamSource(
178 .getResourceAsStream(
179 "/isac/util/genhtml/templates/met_hierarchy_jmenu.xsl"));
181 xslSource = new StreamSource(
183 .getResourceAsStream(
184 "/isac/util/genhtml/templates/exp_hierarchy_jmenu.xsl"));
185 String header = toString(getClass().getResourceAsStream(
186 "/isac/util/genhtml/templates/codeheader.html"));
187 String footer = toString(getClass().getResourceAsStream(
188 "/isac/util/genhtml/templates/codefooter.html"));
190 // Perform the transformation, sending the output to the
192 //java.io.PrintWriter out = response.getWriter();
193 FileWriter out = new FileWriter(destination_ + type + "/"
196 Encoder.encodeInformation(xslSource, xmlSource, out);
200 if (path.trim().endsWith("index.html")) {
201 if (type.equals("pbl")) {
202 path = "index_pbl.html";
203 } else if (type.equals("met"))
204 path = "index_met.html";
206 path = "index_exp.html";
208 System.out.println(path);
209 InputStream istream = getClass().getResourceAsStream(
210 "/isac/util/genhtml/jscript/" + path);
211 FileWriter out = new FileWriter(destination_ + type + "/"
214 while ((input = istream.read()) != -1) {
220 } catch (IOException exc) {
221 System.out.println("IOException:" + exc.getMessage());
225 private String toString(InputStream istream) {
226 StringBuffer buf = new StringBuffer(10000);
229 while ((input = istream.read()) != -1) {
230 buf.append((char) input);
232 } catch (IOException e) {
233 return getErrorXML("Sorry, IOException while accessing the InputStream "
236 return buf.toString();
239 private static String getErrorXML(String string) {
240 return "<ERROR><DESCRIPTION> <h1> Error in file LoadHierarchy.java </h1>"
241 + string + "</DESCRIPTION></ERROR>";
244 protected void loadFile(String path) {
245 StringTokenizer tokens = new StringTokenizer(path, "/");
246 // (String)request.getSession().getAttribute("session_id");
249 String session_id = "";
251 String type = (String) tokens.nextToken();
253 if (!tokens.hasMoreTokens())
254 path = "/index.html";
256 while (tokens.hasMoreTokens())
257 path += "/" + tokens.nextToken();
259 if (path.endsWith("code.html")) {
260 Vector parameter = new Vector();
261 parameter.add(session_id);
264 Source xmlSource = new StreamSource(new StringReader(
265 InformationProcessor.loadHierarchy(session_id, type)));
267 // Perform the transformation, sending the output to the
269 java.io.FileWriter out = new FileWriter(destination_ + type
271 Encoder.encodeHierarchy(type, xmlSource, out);
273 } else if (path.trim().endsWith("heading.html")) {
274 java.io.FileWriter out = new FileWriter(destination_ + type
276 out.write(getHeader(session_id));
278 } else if (path.trim().endsWith("treecontent.html")) {
279 Vector parameter = new Vector();
280 parameter.add(session_id);
283 Source xmlSource = new StreamSource(new StringReader(
284 InformationProcessor.loadHierarchy(session_id, type)));
286 // Perform the transformation, sending the output to the
288 java.io.FileWriter out = new FileWriter(destination_ + type
291 Encoder.encodeHContent(type, xmlSource, out);
294 //TODO this type should not result in different files but in an
295 // autogeneration of a fitting file
296 if (path.trim().endsWith("index.html")) {
297 if (type.equals("pbl")) {
298 path = "index_pbl.html";
299 } else if (type.equals("met"))
300 path = "index_met.html";
302 path = "index_exp.html";
304 InputStream istream = getClass().getResourceAsStream(
305 "/isac/BrowserServlet/jscript/" + path);
306 java.io.FileWriter out = new FileWriter(destination_ + type + "/"
309 while ((input = istream.read()) != -1) {
313 } catch (IOException exc) {
314 System.out.println("bla");
318 private String getHeader(String session_id) {
319 String headercontent = "<html>"
321 + "<a href=\"../../isac.BrowserServlet.Login\" target=\"text\">"
324 + "<a href=\"../../isac.BrowserServlet.Logout\" target=\"text\">"
326 + "<br> <a href=\"../../isac.BrowserServlet.LoadFile/pbl/index.html\" target=\"_top\">PBLHierarchy</a>"
327 + " <a href=\"../../isac.BrowserServlet.LoadFile/met/index.html\" target=\"_top\">METHierarchy</a>"
328 + " <a href=\"../../isac.BrowserServlet.LoadFile/exp/index.html\" target=\"_top\">ExampleHierarchy</a>"
332 if ((username = InformationProcessor.getUsername(session_id)) == null)
333 headercontent += "not logged in";
335 headercontent += " welcome <b>" + username + "</b>";
336 headercontent += "</body> </html>";
337 return headercontent;
340 protected void loadContent(String path) {
341 StringTokenizer tokens = new StringTokenizer(path, "/");
345 Vector parameter = new Vector();
346 parameter.add(session);
348 Source xslSource = null;
349 Source xmlSource = null;
351 String command = tokens.nextToken();
352 String header = "<?xml version=\"1.0\"?> \n"
353 + "<?xml-stylesheet href=\"http://pear.math.pitt.edu/mathzilla/Examples/mml.css\" type=\"text/css\"?>\n"
354 + "<!DOCTYPE html SYSTEM \"mathml.dtd_\">\n" +
355 //"<?xml-stylesheet type=\"text/xsl\"
356 // href=\"http://www.w3.org/Math/XSL/pmathml.xsl\"?>\n"+
357 //" <HTML xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
358 "<HTML xmlns=\"http://www.w3.org/1999/xhtml\"\n"
359 + "xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n"
360 + "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
363 String footer = "</BODY>\n</HTML>";
364 // if(command.equals("pbl-node")){
366 parameter.add(command);
367 if (!tokens.hasMoreElements()) {
368 out = new FileWriter(destination_ + path + ".html");
370 out.write("no details");
374 String contentfile = tokens.nextToken();
375 parameter.add(contentfile);
377 if (!contentfile.trim().endsWith(".xml")) { return; }
378 out = new FileWriter(destination_ + path + ".html");
379 xmlSource = new StreamSource(new StringReader(InformationProcessor
380 .loadXML(session, command, contentfile)));
381 xslSource = new StreamSource(getClass().getResourceAsStream(
382 "/isac/util/genhtml/templates/pbl_node_content.xsl"));
384 // Perform the transformation, sending the output to the response.
386 Encoder.encodeInformation(xslSource, xmlSource, out);
388 } catch (IOException exc) {
389 exc.printStackTrace();
391 // arg1.getWriter().print("pathinfo: "+arg0.getPathInfo()+"\n
392 // transl:"+arg0.getPathTranslated());