java GenHTML added a testline ML_test
authormlang
Thu, 30 Jun 2005 12:03:59 +0200
branchML_test
changeset 2860ce74d76ae3d
parent 4 40100c976180
child 287 75e7d0720b23
java GenHTML added a testline
src/java/isac/util/genhtml/GenHTML.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/java/isac/util/genhtml/GenHTML.java	Thu Jun 30 12:03:59 2005 +0200
     1.3 @@ -0,0 +1,395 @@
     1.4 +/*
     1.5 + * Created on Jun 24, 2003
     1.6 + *  
     1.7 + */
     1.8 +package isac.util.genhtml;
     1.9 +
    1.10 +import isac.util.InformationProcessor;
    1.11 +
    1.12 +import java.io.FileWriter;
    1.13 +import java.io.IOException;
    1.14 +import java.io.InputStream;
    1.15 +import java.io.StringReader;
    1.16 +import java.util.HashSet;
    1.17 +import java.util.Iterator;
    1.18 +import java.util.Set;
    1.19 +import java.util.StringTokenizer;
    1.20 +import java.util.Vector;
    1.21 +
    1.22 +import javax.xml.parsers.DocumentBuilder;
    1.23 +import javax.xml.parsers.DocumentBuilderFactory;
    1.24 +import javax.xml.parsers.ParserConfigurationException;
    1.25 +import javax.xml.transform.Source;
    1.26 +import javax.xml.transform.stream.StreamSource;
    1.27 +
    1.28 +import org.w3c.dom.Document;
    1.29 +import org.w3c.dom.Element;
    1.30 +import org.w3c.dom.NodeList;
    1.31 +import org.xml.sax.InputSource;
    1.32 +import org.xml.sax.SAXException;
    1.33 +
    1.34 +/**
    1.35 + * @author Andreas Griesmayer
    1.36 + *  
    1.37 + */
    1.38 +public class GenHTML {
    1.39 +
    1.40 +    static final int PBLTREE_XSL = 1;
    1.41 +
    1.42 +    static final int METTREE_XSL = 2;
    1.43 +
    1.44 +    static final int EXPTREE_XSL = 3;
    1.45 +
    1.46 +    static final int SIMPLE_HIERARCHY_XSL = 4;
    1.47 +
    1.48 +    static final int SIMPLE_PBL_NODE = 5;
    1.49 +
    1.50 +    //static String destination_ = "/netshares/studenthomes/mlang/proto_v2/isac/proto2/isac/xmldata/";
    1.51 +    static String destination_ = "/netshares/studenthomes/mlang/xmldata/";	
    1.52 +    
    1.53 +    
    1.54 +    /**
    1.55 +     * Method derived from HttpServlet to react on an request by an web-browser
    1.56 +     * AG? ................^^^^^^^^^^^ ???
    1.57 +     * 
    1.58 +     * @param request
    1.59 +     *            object which holds informations about the request like the URL
    1.60 +     *            used to access this servlet
    1.61 +     * @param arg1
    1.62 +     *            object which provides methods to return the results to the
    1.63 +     *            web-browser.
    1.64 +     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
    1.65 +     *      javax.servlet.http.HttpServletResponse)
    1.66 +     */
    1.67 +
    1.68 +    public static void main(String[] argv) {
    1.69 +        if (argv.length > 0) destination_ = argv[0];
    1.70 +        GenHTML genhtml = new GenHTML();
    1.71 +        genhtml.buildHierarchy("met/code.html");
    1.72 +        genhtml.buildHierarchy("met/index.html");
    1.73 +        genhtml.buildHierarchy("met/heading.html");
    1.74 +        genhtml.buildHierarchy("met/menu_empty.html");
    1.75 +        genhtml.buildHierarchy("met/main.html");
    1.76 +
    1.77 +        genhtml.buildHierarchy("pbl/code.html");
    1.78 +        genhtml.buildHierarchy("pbl/index.html");
    1.79 +        genhtml.buildHierarchy("pbl/heading.html");
    1.80 +        genhtml.buildHierarchy("pbl/menu_empty.html");
    1.81 +        genhtml.buildHierarchy("pbl/main.html");
    1.82 +
    1.83 +        genhtml.buildHierarchy("exp/code.html");
    1.84 +        genhtml.buildHierarchy("exp/index.html");
    1.85 +        genhtml.buildHierarchy("exp/heading.html");
    1.86 +        genhtml.buildHierarchy("exp/menu_empty.html");
    1.87 +        genhtml.buildHierarchy("exp/main.html");
    1.88 +
    1.89 +        Set exp_set = genhtml.loadFileList("exp");
    1.90 +        Iterator exp_it = exp_set.iterator();
    1.91 +        int index = exp_set.size();
    1.92 +        while (exp_it.hasNext()) {
    1.93 +            System.out.println("exp to generate:" + (index--));
    1.94 +            genhtml.loadContent("exp/" + exp_it.next());
    1.95 +        }
    1.96 +
    1.97 +        Set pbl_set = genhtml.loadFileList("pbl");
    1.98 +        index = pbl_set.size();
    1.99 +        Iterator pbl_it = pbl_set.iterator();
   1.100 +        while (pbl_it.hasNext()) {
   1.101 +            System.out.println("pbl to generate:" + (index--));
   1.102 +            genhtml.loadContent("pbl/" + pbl_it.next());
   1.103 +        }
   1.104 +
   1.105 +        Set met_set = genhtml.loadFileList("met");
   1.106 +        index = met_set.size();
   1.107 +        Iterator met_it = met_set.iterator();
   1.108 +        while (met_it.hasNext()) {
   1.109 +            System.out.println("met to generate:" + (index--));
   1.110 +            genhtml.loadContent("met/" + met_it.next());
   1.111 +        }
   1.112 +
   1.113 +    }
   1.114 +
   1.115 +    Set loadFileList(String type) {
   1.116 +        Set return_set = new HashSet();
   1.117 +        try {
   1.118 +
   1.119 +            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   1.120 +            DocumentBuilder db = dbf.newDocumentBuilder();
   1.121 +            String h_xml = InformationProcessor.loadHierarchy("", type);
   1.122 +            InputSource inputsource = new InputSource(new StringReader(h_xml));
   1.123 +            Document document = db.parse(inputsource);
   1.124 +
   1.125 +            Element doc_elem = document.getDocumentElement();
   1.126 +            NodeList nodelist = doc_elem.getElementsByTagName("CONTENTREF");
   1.127 +            for (int index = 0; index < nodelist.getLength(); index++) {
   1.128 +                return_set.add(nodelist.item(index).getFirstChild()
   1.129 +                        .getNodeValue().trim());
   1.130 +            }
   1.131 +
   1.132 +            nodelist = doc_elem.getElementsByTagName("EXAMPLE");
   1.133 +            for (int index = 0; index < nodelist.getLength(); index++) {
   1.134 +                return_set.add(nodelist.item(index).getFirstChild()
   1.135 +                        .getNodeValue().trim());
   1.136 +            }
   1.137 +        } catch (ParserConfigurationException e) {
   1.138 +            e.printStackTrace();
   1.139 +        } catch (SAXException e) {
   1.140 +            e.printStackTrace();
   1.141 +        } catch (IOException e) {
   1.142 +            e.printStackTrace();
   1.143 +        }
   1.144 +        return return_set;
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Loads all hierarchies which are displayed on the web-browser
   1.149 +     */
   1.150 +    protected void buildHierarchy(String path) {
   1.151 +        String session = "";
   1.152 +        StringTokenizer tokens = new StringTokenizer(path, "/");
   1.153 +        //	(String)request.getSession().getAttribute("session_id");
   1.154 +        //	if(session==null)
   1.155 +        //	    session="";
   1.156 +        path = "";
   1.157 +        String type = tokens.nextToken();
   1.158 +
   1.159 +        if (!tokens.hasMoreTokens())
   1.160 +            path = "/index.html";
   1.161 +        else
   1.162 +            while (tokens.hasMoreTokens())
   1.163 +                path += "/" + tokens.nextToken();
   1.164 +        try {
   1.165 +            if (path.endsWith("code.html")) {
   1.166 +                Vector parameter = new Vector();
   1.167 +                parameter.add(session);
   1.168 +                parameter.add(type);
   1.169 +                Vector result;
   1.170 +                Source xslSource = null;
   1.171 +                Source xmlSource = new StreamSource(new StringReader(
   1.172 +                        InformationProcessor.loadHierarchy(session, type)));
   1.173 +                if (type.equals("pbl"))
   1.174 +                    xslSource = new StreamSource(
   1.175 +                            getClass()
   1.176 +                                    .getResourceAsStream(
   1.177 +                                            "/isac/util/genhtml/templates/pbl_hierarchy_jmenu.xsl"));
   1.178 +                else if (type.equals("met"))
   1.179 +                    xslSource = new StreamSource(
   1.180 +                            getClass()
   1.181 +                                    .getResourceAsStream(
   1.182 +                                            "/isac/util/genhtml/templates/met_hierarchy_jmenu.xsl"));
   1.183 +                else
   1.184 +                    xslSource = new StreamSource(
   1.185 +                            getClass()
   1.186 +                                    .getResourceAsStream(
   1.187 +                                            "/isac/util/genhtml/templates/exp_hierarchy_jmenu.xsl"));
   1.188 +                String header = toString(getClass().getResourceAsStream(
   1.189 +                        "/isac/util/genhtml/templates/codeheader.html"));
   1.190 +                String footer = toString(getClass().getResourceAsStream(
   1.191 +                        "/isac/util/genhtml/templates/codefooter.html"));
   1.192 +
   1.193 +                // Perform the transformation, sending the output to the
   1.194 +                // response.
   1.195 +                //java.io.PrintWriter out = response.getWriter();
   1.196 +                FileWriter out = new FileWriter(destination_ + type + "/"
   1.197 +                        + path);
   1.198 +                out.write(header);
   1.199 +                Encoder.encodeInformation(xslSource, xmlSource, out);
   1.200 +                out.write(footer);
   1.201 +                out.close();
   1.202 +            } else {
   1.203 +                if (path.trim().endsWith("index.html")) {
   1.204 +                    if (type.equals("pbl")) {
   1.205 +                        path = "index_pbl.html";
   1.206 +                    } else if (type.equals("met"))
   1.207 +                        path = "index_met.html";
   1.208 +                    else
   1.209 +                        path = "index_exp.html";
   1.210 +                }
   1.211 +                System.out.println(path);
   1.212 +                InputStream istream = getClass().getResourceAsStream(
   1.213 +                        "/isac/util/genhtml/jscript/" + path);
   1.214 +                FileWriter out = new FileWriter(destination_ + type + "/"
   1.215 +                        + path);
   1.216 +                int input;
   1.217 +                while ((input = istream.read()) != -1) {
   1.218 +                    out.write(input);
   1.219 +                }
   1.220 +                out.close();
   1.221 +            }
   1.222 +
   1.223 +        } catch (IOException exc) {
   1.224 +            System.out.println("IOException:" + exc.getMessage());
   1.225 +        }
   1.226 +    }
   1.227 +
   1.228 +    private String toString(InputStream istream) {
   1.229 +        StringBuffer buf = new StringBuffer(10000);
   1.230 +        int input;
   1.231 +        try {
   1.232 +            while ((input = istream.read()) != -1) {
   1.233 +                buf.append((char) input);
   1.234 +            }
   1.235 +        } catch (IOException e) {
   1.236 +            return getErrorXML("Sorry, IOException while accessing the InputStream "
   1.237 +                    + e.getMessage());
   1.238 +        }
   1.239 +        return buf.toString();
   1.240 +    }
   1.241 +
   1.242 +    private static String getErrorXML(String string) {
   1.243 +        return "<ERROR><DESCRIPTION> <h1> Error in file LoadHierarchy.java </h1>"
   1.244 +                + string + "</DESCRIPTION></ERROR>";
   1.245 +    }
   1.246 +
   1.247 +    protected void loadFile(String path) {
   1.248 +        StringTokenizer tokens = new StringTokenizer(path, "/");
   1.249 +        //		(String)request.getSession().getAttribute("session_id");
   1.250 +        //		if(session==null)
   1.251 +        //			session="";
   1.252 +        String session_id = "";
   1.253 +        path = "";
   1.254 +        String type = (String) tokens.nextToken();
   1.255 +
   1.256 +        if (!tokens.hasMoreTokens())
   1.257 +            path = "/index.html";
   1.258 +        else
   1.259 +            while (tokens.hasMoreTokens())
   1.260 +                path += "/" + tokens.nextToken();
   1.261 +        try {
   1.262 +            if (path.endsWith("code.html")) {
   1.263 +                Vector parameter = new Vector();
   1.264 +                parameter.add(session_id);
   1.265 +                parameter.add(type);
   1.266 +                Vector result;
   1.267 +                Source xmlSource = new StreamSource(new StringReader(
   1.268 +                        InformationProcessor.loadHierarchy(session_id, type)));
   1.269 +
   1.270 +                // Perform the transformation, sending the output to the
   1.271 +                // response.
   1.272 +                java.io.FileWriter out = new FileWriter(destination_ + type
   1.273 +                        + "/" + path);
   1.274 +                Encoder.encodeHierarchy(type, xmlSource, out);
   1.275 +                return;
   1.276 +            } else if (path.trim().endsWith("heading.html")) {
   1.277 +                java.io.FileWriter out = new FileWriter(destination_ + type
   1.278 +                        + "/" + path);
   1.279 +                out.write(getHeader(session_id));
   1.280 +                return;
   1.281 +            } else if (path.trim().endsWith("treecontent.html")) {
   1.282 +                Vector parameter = new Vector();
   1.283 +                parameter.add(session_id);
   1.284 +                parameter.add(type);
   1.285 +                Vector result;
   1.286 +                Source xmlSource = new StreamSource(new StringReader(
   1.287 +                        InformationProcessor.loadHierarchy(session_id, type)));
   1.288 +
   1.289 +                // Perform the transformation, sending the output to the
   1.290 +                // response.
   1.291 +                java.io.FileWriter out = new FileWriter(destination_ + type
   1.292 +                        + "/" + path);
   1.293 +
   1.294 +                Encoder.encodeHContent(type, xmlSource, out);
   1.295 +                return;
   1.296 +            }
   1.297 +            //TODO this type should not result in different files but in an
   1.298 +            // autogeneration of a fitting file
   1.299 +            if (path.trim().endsWith("index.html")) {
   1.300 +                if (type.equals("pbl")) {
   1.301 +                    path = "index_pbl.html";
   1.302 +                } else if (type.equals("met"))
   1.303 +                    path = "index_met.html";
   1.304 +                else
   1.305 +                    path = "index_exp.html";
   1.306 +            }
   1.307 +            InputStream istream = getClass().getResourceAsStream(
   1.308 +                    "/isac/BrowserServlet/jscript/" + path);
   1.309 +            java.io.FileWriter out = new FileWriter(destination_ + type + "/"
   1.310 +                    + path);
   1.311 +            int input;
   1.312 +            while ((input = istream.read()) != -1) {
   1.313 +                out.write(input);
   1.314 +            }
   1.315 +
   1.316 +        } catch (IOException exc) {
   1.317 +            System.out.println("bla");
   1.318 +        }
   1.319 +    }
   1.320 +
   1.321 +    private String getHeader(String session_id) {
   1.322 +        String headercontent = "<html>"
   1.323 +                + "<body>"
   1.324 +                + "<a href=\"../../isac.BrowserServlet.Login\" target=\"text\">"
   1.325 +                + " login </a>"
   1.326 +                + "&nbsp;&nbsp;"
   1.327 +                + "<a href=\"../../isac.BrowserServlet.Logout\" target=\"text\">"
   1.328 +                + " logout </a>"
   1.329 +                + "<br> <a href=\"../../isac.BrowserServlet.LoadFile/pbl/index.html\" target=\"_top\">PBLHierarchy</a>"
   1.330 +                + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/met/index.html\" target=\"_top\">METHierarchy</a>"
   1.331 +                + "&nbsp;&nbsp;<a href=\"../../isac.BrowserServlet.LoadFile/exp/index.html\" target=\"_top\">ExampleHierarchy</a>"
   1.332 +                + "&nbsp;&nbsp;";
   1.333 +
   1.334 +        String username;
   1.335 +        if ((username = InformationProcessor.getUsername(session_id)) == null)
   1.336 +            headercontent += "not logged in";
   1.337 +        else
   1.338 +            headercontent += " welcome <b>" + username + "</b>";
   1.339 +        headercontent += "</body> </html>";
   1.340 +        return headercontent;
   1.341 +    }
   1.342 +
   1.343 +    protected void loadContent(String path) {
   1.344 +        StringTokenizer tokens = new StringTokenizer(path, "/");
   1.345 +
   1.346 +        String session = "";
   1.347 +
   1.348 +        Vector parameter = new Vector();
   1.349 +        parameter.add(session);
   1.350 +        try {
   1.351 +            Source xslSource = null;
   1.352 +            Source xmlSource = null;
   1.353 +
   1.354 +            String command = tokens.nextToken();
   1.355 +            String header = "<?xml version=\"1.0\"?> \n"
   1.356 +                    + "<?xml-stylesheet href=\"http://pear.math.pitt.edu/mathzilla/Examples/mml.css\" type=\"text/css\"?>\n"
   1.357 +                    + "<!DOCTYPE html SYSTEM \"mathml.dtd_\">\n" +
   1.358 +                    //"<?xml-stylesheet type=\"text/xsl\"
   1.359 +                    // href=\"http://www.w3.org/Math/XSL/pmathml.xsl\"?>\n"+
   1.360 +                    //" <HTML xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
   1.361 +                    "<HTML xmlns=\"http://www.w3.org/1999/xhtml\"\n"
   1.362 +                    + "xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n"
   1.363 +                    + "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
   1.364 +                    + "<BODY>";
   1.365 +
   1.366 +            String footer = "</BODY>\n</HTML>";
   1.367 +            //		if(command.equals("pbl-node")){
   1.368 +            FileWriter out;
   1.369 +            parameter.add(command);
   1.370 +            if (!tokens.hasMoreElements()) {
   1.371 +                out = new FileWriter(destination_ + path + ".html");
   1.372 +                out.write(header);
   1.373 +                out.write("no details");
   1.374 +                out.write(footer);
   1.375 +                return;
   1.376 +            }
   1.377 +            String contentfile = tokens.nextToken();
   1.378 +            parameter.add(contentfile);
   1.379 +
   1.380 +            if (!contentfile.trim().endsWith(".xml")) { return; }
   1.381 +            out = new FileWriter(destination_ + path + ".html");
   1.382 +            xmlSource = new StreamSource(new StringReader(InformationProcessor
   1.383 +                    .loadXML(session, command, contentfile)));
   1.384 +            xslSource = new StreamSource(getClass().getResourceAsStream(
   1.385 +                    "/isac/util/genhtml/templates/pbl_node_content.xsl"));
   1.386 +            //		}
   1.387 +            // Perform the transformation, sending the output to the response.
   1.388 +            out.write(header);
   1.389 +            Encoder.encodeInformation(xslSource, xmlSource, out);
   1.390 +            out.write(footer);
   1.391 +        } catch (IOException exc) {
   1.392 +            exc.printStackTrace();
   1.393 +        }
   1.394 +        //	arg1.getWriter().print("pathinfo: "+arg0.getPathInfo()+"\n
   1.395 +        // transl:"+arg0.getPathTranslated());
   1.396 +    }
   1.397 +
   1.398 +}
   1.399 \ No newline at end of file