src/java/isac/util/genhtml/GenHTML.java
author mlang
Thu, 30 Jun 2005 12:03:59 +0200
branchML_test
changeset 286 0ce74d76ae3d
child 289 6d7b07dc8e91
permissions -rw-r--r--
java GenHTML added a testline
     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_ = "/netshares/studenthomes/mlang/proto_v2/isac/proto2/isac/xmldata/";
    48     static String destination_ = "/netshares/studenthomes/mlang/xmldata/";	
    49     
    50     
    51     /**
    52      * Method derived from HttpServlet to react on an request by an web-browser
    53      * AG? ................^^^^^^^^^^^ ???
    54      * 
    55      * @param request
    56      *            object which holds informations about the request like the URL
    57      *            used to access this servlet
    58      * @param arg1
    59      *            object which provides methods to return the results to the
    60      *            web-browser.
    61      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
    62      *      javax.servlet.http.HttpServletResponse)
    63      */
    64 
    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");
    73 
    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");
    79 
    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");
    85 
    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());
    92         }
    93 
    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());
   100         }
   101 
   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());
   108         }
   109 
   110     }
   111 
   112     Set loadFileList(String type) {
   113         Set return_set = new HashSet();
   114         try {
   115 
   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);
   121 
   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());
   127             }
   128 
   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());
   133             }
   134         } catch (ParserConfigurationException e) {
   135             e.printStackTrace();
   136         } catch (SAXException e) {
   137             e.printStackTrace();
   138         } catch (IOException e) {
   139             e.printStackTrace();
   140         }
   141         return return_set;
   142     }
   143 
   144     /**
   145      * Loads all hierarchies which are displayed on the web-browser
   146      */
   147     protected void buildHierarchy(String path) {
   148         String session = "";
   149         StringTokenizer tokens = new StringTokenizer(path, "/");
   150         //	(String)request.getSession().getAttribute("session_id");
   151         //	if(session==null)
   152         //	    session="";
   153         path = "";
   154         String type = tokens.nextToken();
   155 
   156         if (!tokens.hasMoreTokens())
   157             path = "/index.html";
   158         else
   159             while (tokens.hasMoreTokens())
   160                 path += "/" + tokens.nextToken();
   161         try {
   162             if (path.endsWith("code.html")) {
   163                 Vector parameter = new Vector();
   164                 parameter.add(session);
   165                 parameter.add(type);
   166                 Vector result;
   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(
   172                             getClass()
   173                                     .getResourceAsStream(
   174                                             "/isac/util/genhtml/templates/pbl_hierarchy_jmenu.xsl"));
   175                 else if (type.equals("met"))
   176                     xslSource = new StreamSource(
   177                             getClass()
   178                                     .getResourceAsStream(
   179                                             "/isac/util/genhtml/templates/met_hierarchy_jmenu.xsl"));
   180                 else
   181                     xslSource = new StreamSource(
   182                             getClass()
   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"));
   189 
   190                 // Perform the transformation, sending the output to the
   191                 // response.
   192                 //java.io.PrintWriter out = response.getWriter();
   193                 FileWriter out = new FileWriter(destination_ + type + "/"
   194                         + path);
   195                 out.write(header);
   196                 Encoder.encodeInformation(xslSource, xmlSource, out);
   197                 out.write(footer);
   198                 out.close();
   199             } else {
   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";
   205                     else
   206                         path = "index_exp.html";
   207                 }
   208                 System.out.println(path);
   209                 InputStream istream = getClass().getResourceAsStream(
   210                         "/isac/util/genhtml/jscript/" + path);
   211                 FileWriter out = new FileWriter(destination_ + type + "/"
   212                         + path);
   213                 int input;
   214                 while ((input = istream.read()) != -1) {
   215                     out.write(input);
   216                 }
   217                 out.close();
   218             }
   219 
   220         } catch (IOException exc) {
   221             System.out.println("IOException:" + exc.getMessage());
   222         }
   223     }
   224 
   225     private String toString(InputStream istream) {
   226         StringBuffer buf = new StringBuffer(10000);
   227         int input;
   228         try {
   229             while ((input = istream.read()) != -1) {
   230                 buf.append((char) input);
   231             }
   232         } catch (IOException e) {
   233             return getErrorXML("Sorry, IOException while accessing the InputStream "
   234                     + e.getMessage());
   235         }
   236         return buf.toString();
   237     }
   238 
   239     private static String getErrorXML(String string) {
   240         return "<ERROR><DESCRIPTION> <h1> Error in file LoadHierarchy.java </h1>"
   241                 + string + "</DESCRIPTION></ERROR>";
   242     }
   243 
   244     protected void loadFile(String path) {
   245         StringTokenizer tokens = new StringTokenizer(path, "/");
   246         //		(String)request.getSession().getAttribute("session_id");
   247         //		if(session==null)
   248         //			session="";
   249         String session_id = "";
   250         path = "";
   251         String type = (String) tokens.nextToken();
   252 
   253         if (!tokens.hasMoreTokens())
   254             path = "/index.html";
   255         else
   256             while (tokens.hasMoreTokens())
   257                 path += "/" + tokens.nextToken();
   258         try {
   259             if (path.endsWith("code.html")) {
   260                 Vector parameter = new Vector();
   261                 parameter.add(session_id);
   262                 parameter.add(type);
   263                 Vector result;
   264                 Source xmlSource = new StreamSource(new StringReader(
   265                         InformationProcessor.loadHierarchy(session_id, type)));
   266 
   267                 // Perform the transformation, sending the output to the
   268                 // response.
   269                 java.io.FileWriter out = new FileWriter(destination_ + type
   270                         + "/" + path);
   271                 Encoder.encodeHierarchy(type, xmlSource, out);
   272                 return;
   273             } else if (path.trim().endsWith("heading.html")) {
   274                 java.io.FileWriter out = new FileWriter(destination_ + type
   275                         + "/" + path);
   276                 out.write(getHeader(session_id));
   277                 return;
   278             } else if (path.trim().endsWith("treecontent.html")) {
   279                 Vector parameter = new Vector();
   280                 parameter.add(session_id);
   281                 parameter.add(type);
   282                 Vector result;
   283                 Source xmlSource = new StreamSource(new StringReader(
   284                         InformationProcessor.loadHierarchy(session_id, type)));
   285 
   286                 // Perform the transformation, sending the output to the
   287                 // response.
   288                 java.io.FileWriter out = new FileWriter(destination_ + type
   289                         + "/" + path);
   290 
   291                 Encoder.encodeHContent(type, xmlSource, out);
   292                 return;
   293             }
   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";
   301                 else
   302                     path = "index_exp.html";
   303             }
   304             InputStream istream = getClass().getResourceAsStream(
   305                     "/isac/BrowserServlet/jscript/" + path);
   306             java.io.FileWriter out = new FileWriter(destination_ + type + "/"
   307                     + path);
   308             int input;
   309             while ((input = istream.read()) != -1) {
   310                 out.write(input);
   311             }
   312 
   313         } catch (IOException exc) {
   314             System.out.println("bla");
   315         }
   316     }
   317 
   318     private String getHeader(String session_id) {
   319         String headercontent = "<html>"
   320                 + "<body>"
   321                 + "<a href=\"../../isac.BrowserServlet.Login\" target=\"text\">"
   322                 + " login </a>"
   323                 + "&nbsp;&nbsp;"
   324                 + "<a href=\"../../isac.BrowserServlet.Logout\" target=\"text\">"
   325                 + " logout </a>"
   326                 + "<br> <a href=\"../../isac.BrowserServlet.LoadFile/pbl/index.html\" target=\"_top\">PBLHierarchy</a>"
   327                 + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/met/index.html\" target=\"_top\">METHierarchy</a>"
   328                 + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/exp/index.html\" target=\"_top\">ExampleHierarchy</a>"
   329                 + "&nbsp;&nbsp;";
   330 
   331         String username;
   332         if ((username = InformationProcessor.getUsername(session_id)) == null)
   333             headercontent += "not logged in";
   334         else
   335             headercontent += " welcome <b>" + username + "</b>";
   336         headercontent += "</body> </html>";
   337         return headercontent;
   338     }
   339 
   340     protected void loadContent(String path) {
   341         StringTokenizer tokens = new StringTokenizer(path, "/");
   342 
   343         String session = "";
   344 
   345         Vector parameter = new Vector();
   346         parameter.add(session);
   347         try {
   348             Source xslSource = null;
   349             Source xmlSource = null;
   350 
   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"
   361                     + "<BODY>";
   362 
   363             String footer = "</BODY>\n</HTML>";
   364             //		if(command.equals("pbl-node")){
   365             FileWriter out;
   366             parameter.add(command);
   367             if (!tokens.hasMoreElements()) {
   368                 out = new FileWriter(destination_ + path + ".html");
   369                 out.write(header);
   370                 out.write("no details");
   371                 out.write(footer);
   372                 return;
   373             }
   374             String contentfile = tokens.nextToken();
   375             parameter.add(contentfile);
   376 
   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"));
   383             //		}
   384             // Perform the transformation, sending the output to the response.
   385             out.write(header);
   386             Encoder.encodeInformation(xslSource, xmlSource, out);
   387             out.write(footer);
   388         } catch (IOException exc) {
   389             exc.printStackTrace();
   390         }
   391         //	arg1.getWriter().print("pathinfo: "+arg0.getPathInfo()+"\n
   392         // transl:"+arg0.getPathTranslated());
   393     }
   394 
   395 }