src/java/isac/util/genhtml/GenHTML.java
author mlang
Wed, 06 Jul 2005 11:27:02 +0200
branchML_test
changeset 291 80f98637eff6
parent 290 3f76f7d39384
permissions -rw-r--r--
ML: comitting after merge
     1 /*
     2  * Created on Jun 24, 2003
     3  *  
     4  */
     5 package isac.util.genhtml;
     6 
     7 import isac.util.InformationProcessor;
     8 
     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;
    15 import java.util.Set;
    16 import java.util.StringTokenizer;
    17 import java.util.Vector;
    18 
    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;
    24 
    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;
    30 
    31 /**
    32  * @author Andreas Griesmayer
    33  *  
    34  */
    35 public class GenHTML {
    36 
    37     static final int PBLTREE_XSL = 1;
    38 
    39     static final int METTREE_XSL = 2;
    40 
    41     static final int EXPTREE_XSL = 3;
    42 
    43     static final int SIMPLE_HIERARCHY_XSL = 4;
    44 
    45     static final int SIMPLE_PBL_NODE = 5;
    46 
    47     static String destination_ = "/home/neuper/proto2/www/kbase/";
    48 
    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");
    57 
    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");
    63 
    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");
    69 
    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());
    76         }
    77 
    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());
    84         }
    85 
    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());
    92         }
    93 
    94     }
    95 
    96     Set loadFileList(String type) {
    97         Set return_set = new HashSet();
    98         try {
    99 
   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);
   105 
   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());
   111             }
   112 
   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());
   117             }
   118         } catch (ParserConfigurationException e) {
   119             e.printStackTrace();
   120         } catch (SAXException e) {
   121             e.printStackTrace();
   122         } catch (IOException e) {
   123             e.printStackTrace();
   124         }
   125         return return_set;
   126     }
   127 
   128     /**
   129      * Loads all hierarchies which are displayed on the web-browser
   130      */
   131     protected void buildHierarchy(String path) {
   132         String session = "";
   133         StringTokenizer tokens = new StringTokenizer(path, "/");
   134         //	(String)request.getSession().getAttribute("session_id");
   135         //	if(session==null)
   136         //	    session="";
   137         path = "";
   138         String type = tokens.nextToken();
   139 
   140         if (!tokens.hasMoreTokens())
   141             path = "/index.html";
   142         else
   143             while (tokens.hasMoreTokens())
   144                 path += "/" + tokens.nextToken();
   145         try {
   146             if (path.endsWith("code.html")) {
   147                 Vector parameter = new Vector();
   148                 parameter.add(session);
   149                 parameter.add(type);
   150                 Vector result;
   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(
   156                             getClass()
   157                                     .getResourceAsStream(
   158                                             "/isac/util/genhtml/templates/pbl_hierarchy_jmenu.xsl"));
   159                 else if (type.equals("met"))
   160                     xslSource = new StreamSource(
   161                             getClass()
   162                                     .getResourceAsStream(
   163                                             "/isac/util/genhtml/templates/met_hierarchy_jmenu.xsl"));
   164                 else
   165                     xslSource = new StreamSource(
   166                             getClass()
   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"));
   173 
   174                 // Perform the transformation, sending the output to the
   175                 // response.
   176                 //java.io.PrintWriter out = response.getWriter();
   177                 FileWriter out = new FileWriter(destination_ + type + "/"
   178                         + path);
   179                 out.write(header);
   180                 Encoder.encodeInformation(xslSource, xmlSource, out);
   181                 out.write(footer);
   182                 out.close();
   183             } else {
   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";
   189                     else
   190                         path = "index_exp.html";
   191                 }
   192                 System.out.println(path);
   193                 InputStream istream = getClass().getResourceAsStream(
   194                         "/isac/util/genhtml/jscript/" + path);
   195                 FileWriter out = new FileWriter(destination_ + type + "/"
   196                         + path);
   197                 int input;
   198                 while ((input = istream.read()) != -1) {
   199                     out.write(input);
   200                 }
   201                 out.close();
   202             }
   203 
   204         } catch (IOException exc) {
   205             System.out.println("IOException:" + exc.getMessage());
   206         }
   207     }
   208 
   209     private String toString(InputStream istream) {
   210         StringBuffer buf = new StringBuffer(10000);
   211         int input;
   212         try {
   213             while ((input = istream.read()) != -1) {
   214                 buf.append((char) input);
   215             }
   216         } catch (IOException e) {
   217             return getErrorXML("Sorry, IOException while accessing the InputStream "
   218                     + e.getMessage());
   219         }
   220         return buf.toString();
   221     }
   222 
   223     private static String getErrorXML(String string) {
   224         return "<ERROR><DESCRIPTION> <h1> Error in file LoadHierarchy.java </h1>"
   225                 + string + "</DESCRIPTION></ERROR>";
   226     }
   227 
   228     protected void loadFile(String path) {
   229         StringTokenizer tokens = new StringTokenizer(path, "/");
   230         //		(String)request.getSession().getAttribute("session_id");
   231         //		if(session==null)
   232         //			session="";
   233         String session_id = "";
   234         path = "";
   235         String type = (String) tokens.nextToken();
   236 
   237         if (!tokens.hasMoreTokens())
   238             path = "/index.html";
   239         else
   240             while (tokens.hasMoreTokens())
   241                 path += "/" + tokens.nextToken();
   242         try {
   243             if (path.endsWith("code.html")) {
   244                 Vector parameter = new Vector();
   245                 parameter.add(session_id);
   246                 parameter.add(type);
   247                 Vector result;
   248                 Source xmlSource = new StreamSource(new StringReader(
   249                         InformationProcessor.loadHierarchy(session_id, type)));
   250 
   251                 // Perform the transformation, sending the output to the
   252                 // response.
   253                 java.io.FileWriter out = new FileWriter(destination_ + type
   254                         + "/" + path);
   255                 Encoder.encodeHierarchy(type, xmlSource, out);
   256                 return;
   257             } else if (path.trim().endsWith("heading.html")) {
   258                 java.io.FileWriter out = new FileWriter(destination_ + type
   259                         + "/" + path);
   260                 out.write(getHeader(session_id));
   261                 return;
   262             } else if (path.trim().endsWith("treecontent.html")) {
   263                 Vector parameter = new Vector();
   264                 parameter.add(session_id);
   265                 parameter.add(type);
   266                 Vector result;
   267                 Source xmlSource = new StreamSource(new StringReader(
   268                         InformationProcessor.loadHierarchy(session_id, type)));
   269 
   270                 // Perform the transformation, sending the output to the
   271                 // response.
   272                 java.io.FileWriter out = new FileWriter(destination_ + type
   273                         + "/" + path);
   274 
   275                 Encoder.encodeHContent(type, xmlSource, out);
   276                 return;
   277             }
   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";
   285                 else
   286                     path = "index_exp.html";
   287             }
   288             InputStream istream = getClass().getResourceAsStream(
   289                     "/isac/BrowserServlet/jscript/" + path);
   290             java.io.FileWriter out = new FileWriter(destination_ + type + "/"
   291                     + path);
   292             int input;
   293             while ((input = istream.read()) != -1) {
   294                 out.write(input);
   295             }
   296 
   297         } catch (IOException exc) {
   298             System.out.println("bla");
   299         }
   300     }
   301 
   302     private String getHeader(String session_id) {
   303         String headercontent = "<html>"
   304                 + "<body>"
   305                 + "<a href=\"../../isac.BrowserServlet.Login\" target=\"text\">"
   306                 + " login </a>"
   307                 + "&nbsp;&nbsp;"
   308                 + "<a href=\"../../isac.BrowserServlet.Logout\" target=\"text\">"
   309                 + " logout </a>"
   310                 + "<br> <a href=\"../../isac.BrowserServlet.LoadFile/pbl/index.html\" target=\"_top\">PBLHierarchy</a>"
   311                 + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/met/index.html\" target=\"_top\">METHierarchy</a>"
   312                 + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/exp/index.html\" target=\"_top\">ExampleHierarchy</a>"
   313                 + "&nbsp;&nbsp;";
   314 
   315         String username;
   316         if ((username = InformationProcessor.getUsername(session_id)) == null)
   317             headercontent += "not logged in";
   318         else
   319             headercontent += " welcome <b>" + username + "</b>";
   320         headercontent += "</body> </html>";
   321         return headercontent;
   322     }
   323 
   324     protected void loadContent(String path) {
   325         StringTokenizer tokens = new StringTokenizer(path, "/");
   326 
   327         String session = "";
   328 
   329         Vector parameter = new Vector();
   330         parameter.add(session);
   331         try {
   332             Source xslSource = null;
   333             Source xmlSource = null;
   334 
   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"
   345                     + "<BODY>";
   346 
   347             String footer = "</BODY>\n</HTML>";
   348             //		if(command.equals("pbl-node")){
   349             FileWriter out;
   350             parameter.add(command);
   351             if (!tokens.hasMoreElements()) {
   352                 out = new FileWriter(destination_ + path + ".html");
   353                 out.write(header);
   354                 out.write("no details");
   355                 out.write(footer);
   356                 return;
   357             }
   358             String contentfile = tokens.nextToken();
   359             parameter.add(contentfile);
   360 
   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"));
   367             //		}
   368             // Perform the transformation, sending the output to the response.
   369             out.write(header);
   370             Encoder.encodeInformation(xslSource, xmlSource, out);
   371             out.write(footer);
   372         } catch (IOException exc) {
   373             exc.printStackTrace();
   374         }
   375         //	arg1.getWriter().print("pathinfo: "+arg0.getPathInfo()+"\n
   376         // transl:"+arg0.getPathTranslated());
   377     }
   378 
   379 }