]> matita.cs.unibo.it Git - helm.git/commitdiff
Commit of Ferruccio changes:
authorClaudio Sacerdoti Coen <claudio.sacerdoticoen@unibo.it>
Tue, 6 Nov 2001 12:35:51 +0000 (12:35 +0000)
committerClaudio Sacerdoti Coen <claudio.sacerdoticoen@unibo.it>
Tue, 6 Nov 2001 12:35:51 +0000 (12:35 +0000)
* New syntax: every method now accepts a list of inputs
* Small bug fixes

helm/uwobo/build.xml
helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java
helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java

index c021b0b46556e48c41e29ea547fdef8f476f058d..de9d59a8764b0ba05d1971d99ad7b9b7e08c4643 100644 (file)
@@ -4,7 +4,7 @@
 
        <property name="Name" value="Uwobo"/>
        <property name="name" value="uwobo"/>
-       <property name="version" value="1.1.12"/>
+       <property name="version" value="1.1.14beta"/>
 
        <property file=".${name}.properties" />
        <property file="${user.home}/.${name}.properties" />
                <copy todir="${dist.dir}/lib" >
                        <fileset dir="${lib.dir}" />
                </copy>
+                <!-- CSC: next line needed only if docs is empty -->
+               <mkdir dir="${docs.dir}"/>
                <copy todir="${dist.dir}/docs" >
                        <fileset dir="${docs.dir}" />
                </copy>
+                <!-- CSC: next line needed only if resources is empty -->
+               <mkdir dir="resources"/>
                <copy todir="${dist.dir}/resources" >
                        <fileset dir="resources" />
                </copy>
        </target>
 
        <target name="dist-zip" depends="dist">
+               <mkdir dir="backup"/>
                <zip zipfile="backup/${Name}-${version}.zip" basedir="${dist.dir}" includes="**" />
        </target>
 
        </target>
 
        <target name="dist-tgz" depends="dist">
+               <mkdir dir="backup"/>
                <move todir="${name}">
                        <fileset dir="${dist.dir}"/>
                </move>
index da11a070cefb14f078ad45f9fe54f96b14822243..cd2f2fa30a2a05251d599ae687bab383a7be623c 100644 (file)
@@ -152,7 +152,8 @@ public class Server {
                hashMap.put(key, style);
        }
 
-       public void removeAll() throws TransformerConfigurationException, SAXException, IOException {
+       public void removeAll() 
+        {
                String key;
                Style style;
                Iterator i = hashMap.keySet().iterator();
@@ -212,9 +213,9 @@ public class Server {
                        return applyStylesheet(style.stylesheet, keys[idx].params, applyRec(keys, idx - 1, saxOutput));
                }
        }
-
+//FileNotFoundException
        public void apply(String inFile, OutputStream outputStream, Key[] keys, Properties userProperties)
-       throws FileNotFoundException, IOException, TransformerConfigurationException, SAXException, Exception
+       throws IOException, TransformerConfigurationException, SAXException, Exception
        {
 /*             File outFile = new File(outFilename);
                if (outFile.exists())
@@ -273,11 +274,14 @@ public class Server {
 
        public void reload(String key) throws TransformerConfigurationException, SAXException, IOException {
                Style style = (Style)hashMap.get(key);
-
-               log("reloading \"" + key + "\"... ");
-               style.stylesheet = compileStylesheet(style.fileName);
-               style.lastModified = new File(style.fileName).lastModified();
-               log("done!");
+                if (style != null) {
+                       log("reloading \"" + key + "\"... ");
+                       style.stylesheet = compileStylesheet(style.fileName);
+                       style.lastModified = new File(style.fileName).lastModified();
+                       log("done!");
+               } else {
+                       log("error, stylesheet \"" + key + "\" not loaded");
+               }
        }
 
        public void updateAll() throws TransformerConfigurationException, SAXException, IOException {
@@ -288,14 +292,18 @@ public class Server {
 
        public void update(String key) throws TransformerConfigurationException, SAXException, IOException {
                Style style = (Style)hashMap.get(key);
-
-               log("updating \"" + key + "\"... ");
-               File styleFile = new File(style.fileName);
-               if (styleFile.lastModified() > style.lastModified) {
-                       style.stylesheet = compileStylesheet(style.fileName);
-                       style.lastModified = styleFile.lastModified();
-                       log("done!");
-               } else
-                       log("\"" + key + "\" is up to date");
+                if (style != null) {
+                       log("updating \"" + key + "\"... ");
+                       File styleFile = new File(style.fileName);
+                       if (styleFile.lastModified() > style.lastModified) {
+                               style.stylesheet = compileStylesheet(style.fileName);
+                               style.lastModified = styleFile.lastModified();
+                               log("done!");
+                       } else {
+                               log("\"" + key + "\" is up to date");
+                       }
+               } else {
+                       log("error, stylesheet \"" + key + "\" not loaded");
+               }
        }
 }
index 0e28877fd42827e43518c48d8435a7d01a85e059..3b4766250a42298964bd9612dcd00399aaf23fa4 100644 (file)
@@ -40,208 +40,286 @@ import org.xml.sax.*;
 * bugs:
 *      directory base stylesheet inclusi
 *
-* @author Riccardo Solmi
+* @author The HELM team
 */
 public class Servlet extends HttpServlet {
-       public static final String[] usage = {
-               "http://<i>hostname</i>/helm/servlet/uwobo/help",
-               "http://<i>hostname</i>/helm/servlet/uwobo/add?xsluri=<i>stylesheet</i>&key=<i>name</i>",
-               "http://<i>hostname</i>/helm/servlet/uwobo/remove[?key=<i>name</i>]",
-               "http://<i>hostname</i>/helm/servlet/uwobo/list",
-               "http://<i>hostname</i>/helm/servlet/uwobo/reload[?key=<i>name</i>]",
-               "http://<i>hostname</i>/helm/servlet/uwobo/update[?key=<i>name</i>]",
-               "http://<i>hostname</i>/helm/servlet/uwobo/apply?xmluri=<i>xmldata</i>&keys=<i>key_1,...,key_n</i>[&param.<i>name</i>=<i>value</i>]*"
-       };
-       public static final String help;
-       
-       static {
-               StringBuffer sb = new StringBuffer();
-               sb.append("<ul>");
-               for (int i=0; i<usage.length; i++)
-                       sb.append("<li>").append(usage[i]).append("</li>");
-               sb.append("</ul>");
-               help = sb.toString();
-       }
+   
+   public static final String[] usage = {
+      "http://<i>hostname</i>/helm/servlet/uwobo/help",
+      "http://<i>hostname</i>/helm/servlet/uwobo/add?key=<i>key</i>,<i>stylesheet</i>[&key=<i>key</i>,<i>stylesheet</i>]*",
+      "http://<i>hostname</i>/helm/servlet/uwobo/remove[?keys=<i>key_1,...,key_n</i>]",
+      "http://<i>hostname</i>/helm/servlet/uwobo/list",
+      "http://<i>hostname</i>/helm/servlet/uwobo/reload[?keys=<i>key_1,...,key_n</i>]",
+      "http://<i>hostname</i>/helm/servlet/uwobo/update[?keys=<i>key_1,...,key_n</i>]",
+      "http://<i>hostname</i>/helm/servlet/uwobo/apply?xmluri=<i>xmldata</i>&keys=<i>key_1,...,key_n</i>[&param.<i>name</i>=<i>value</i>]*[&param.<i>key</i>.<i>name</i>=<i>value</i>]*[&prop.<i>name</i>=[<i>value</i>]]*"
+   };
+   public static final String help;
+          
+   static {
+      StringBuffer sb = new StringBuffer();
+      sb.append("<ul>");
+      for (int i=0; i<usage.length; i++)
+         sb.append("<li>").append(usage[i]).append("</li>");
+      sb.append("</ul>");
+      help = sb.toString();
+   }
 
-       private Server server;
+   private Server server;
 
-       public void init(ServletConfig config) throws ServletException
-       {
-               super.init(config);
+   public void init(ServletConfig config)
+   throws ServletException
+   {
+      super.init(config);
 
-               System.out.println("UWOBO init");
-               server = new Server();
-       }
+      System.out.println("UWOBO init");
+      server = new Server();
+   }
 
-       public static String[] split(final String s, final String delim)
-       {
-               StringTokenizer st = new StringTokenizer(s, delim);
-               String[] res = new String[st.countTokens()];
-               for (int i = 0; i < res.length; i++) res[i] = st.nextToken();
-               return res;
-       }
+   private static String[] split(final String s, final String delim)
+   {
+      String[] res = {null}; 
+      if (s == null) return res;
+      StringTokenizer st = new StringTokenizer(s, delim);
+      res = new String[st.countTokens()];
+      for (int i = 0; i < res.length; i++) res[i] = st.nextToken();
+      return res;
+   }
        
-       public void doGet(HttpServletRequest request, HttpServletResponse response)
-       throws ServletException, IOException
-       {
-               System.out.println("UWOBO "+request.getPathInfo());
-               ServletOutputStream out;
-
-               try {
-                       final String cmd = request.getPathInfo();
-                       if (cmd == null) {
-                               sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help);
-                               return;
-                       }
-                       if (cmd.equals("/add")) {
-                               final String filename = request.getParameter("xsluri");
-                               if (filename == null) {
-                                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[1]);
-                                       return;
-                               }
-                               final String key = request.getParameter("key");
-                               server.add(filename, key);
-                       } else if (cmd.equals("/apply")) {
-                               final String infile = request.getParameter("xmluri");
-                               final String keys = request.getParameter("keys");
+   private static String[] split2(final String s, final String delim)
+   {
+      String[] res = new String[2];
+      StringTokenizer st = new StringTokenizer(s);
+      res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1);
+      return res;
+   }
 
-                               if (infile == null || keys == null) {
-                                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]);
-                                       return;
-                               }
+   private void html_open(HttpServletResponse resp, ServletOutputStream out)
+   throws IOException
+   {  
+      resp.setContentType("text/html");
+      out.println("<html><body bgcolor=\"#ffffff\"><h1>Uwobo servlet</h1>");
+   }
+   
+   private void html_close(ServletOutputStream out)
+   throws IOException
+   {
+      out.println("<p>done</p></body></html>");
+      out.close();
+   }
 
-                               final String[] keyName = split(keys, ",");
-                               final Key[] keySeq = new Key[keyName.length];
-                               for (int i = 0; i < keySeq.length; i++) {
-                                       keySeq[i] = new Key();
-                                       keySeq[i].name = keyName[i];
-                                       keySeq[i].params = new HashMap();
-                               }
+   private String msg_out(String message)
+   {
+      return message+"<br>";
+   }
+   
+   private String exc_out(String message, Exception e)
+   {
+      if (message == null)
+      {
+        message = e.getClass().getName();
+        message = message.substring(message.lastIndexOf('.')+1);
+      }
+      String local = e.getLocalizedMessage();
+      local = local.substring(local.lastIndexOf(':')+1);
+      return "<b>"+message+": "+local+"</b><br>";
+   }
+   
+   public void doGet(HttpServletRequest request, HttpServletResponse response)
+   throws ServletException, IOException
+   {
+      ServletOutputStream out = response.getOutputStream();
+      
+      response.setHeader("Cache-Control", "no-cache");
+      response.setHeader("Pragma", "no-cache");
+      response.setHeader("Expires", "0");
+      System.out.println("UWOBO "+request.getPathInfo());
+               
+      try { 
+        final String cmd = request.getPathInfo();
+        if (cmd == null) { 
+           sendError(response, out, "unknown command", help); return;
+        } 
+        if (cmd.equals("/add")) {
+           final String[] xslkey = request.getParameterValues("key");
+           if (xslkey == null) {
+              sendError(response, out, "bad parameters", usage[1]); return;
+           } 
+           html_open(response, out);
+           for (int i = 0; i < xslkey.length; i++) {
+              final String data[] = split2(xslkey[i], ",");
+              out.println(msg_out("adding key "+data[0]+" ("+data[1]+")"));
+              try {
+                 server.add(data[1], data[0]);
+               } catch (TransformerConfigurationException tce) {
+                  out.println(exc_out("stylesheet error", tce));
+               } catch (Exception e) {
+                 out.println(exc_out(null, e));
+              }
+           }
+           html_close(out); return;
+        }
+        if (cmd.equals("/apply")) {
+           final String infile = request.getParameter("xmluri");
+           final String keys = request.getParameter("keys");
 
-                               final Properties props = new Properties();
-                               final Enumeration e = request.getParameterNames();
-                               while (e.hasMoreElements()) {
-                                       String param = (String) e.nextElement();
-                                       if (param.startsWith("param.")) {
-                                               final String name = param.substring(6);
-                                               final String value = request.getParameter(param);
-                                               final String[] keyParam = split(name, ".");
-                                               if (keyParam.length == 1) {
-                                                       // this is a global parameter
-                                                       Server.log("global parameter: " + keyParam[0] + " = " + value);
-                                                       for (int i = 0; i < keySeq.length; i++)
-                                                               // we add the global parameter only if there is no
-                                                               // local parameter with the same name
-                                                               if (!keySeq[i].params.containsKey(keyParam[0]))
-                                                                       keySeq[i].params.put(keyParam[0], value);
-                                               } else if (keyParam.length == 2) {
-                                                       // this is a local parameter
-                                                       Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value);
-                                                       for (int i = 0; i < keySeq.length; i++) {
-                                                               if (keySeq[i].name.equals(keyParam[0]))
-                                                                       keySeq[i].params.put(keyParam[1], value);
-                                                       }
-                                               } else {
-                                                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]);
-                                                       return;
-                                               }
-                                       } else if (param.startsWith("prop.")) {
-                                               final String name = param.substring(5);
-                                               final String value = request.getParameter(param);
-                                               Server.log("property: " + name + " = " + value);
-                                               props.setProperty(name, value);
-                                       }
-                               }
-                                       
-                               String contentType = props.getProperty(OutputKeys.MEDIA_TYPE);
-                               if (contentType == null && keySeq.length > 0) 
-                                       contentType = server.getContentType(keySeq[keySeq.length - 1].name);
-                               else if (contentType == null)
-                                       contentType = "text/xml";
-                               response.setContentType(contentType);
-                               Server.log("content type: " + contentType);
+           if (infile == null || keys == null) {
+              sendError(response, out, "bad parameters", usage[6]); return;
+           } 
 
-                               out = response.getOutputStream();
-                               server.apply(infile, out, keySeq, props);
-                               out.close();
-                               return;
-                       } else if (cmd.equals("/remove")) {
-                               final String key = request.getParameter("key");
-                               if (key == null)
-                                       server.removeAll();
-                               else
-                                       server.remove(key);
-                       } else if (cmd.equals("/list")) {
-                               Iterator i = server.list().iterator();
+           final String[] keyName = split(keys, ",");
+           final Key[] keySeq = new Key[keyName.length];
+           for (int i = 0; i < keySeq.length; i++) {
+               keySeq[i] = new Key();
+              keySeq[i].name = keyName[i];
+              keySeq[i].params = new HashMap();
+           }
 
-                               response.setContentType("text/html");
-                               out = response.getOutputStream();
-                               out.println("<html><body><h1>Uwobo servlet</h1><p>stylesheet list:</p><ul>");
-                               while (i.hasNext())
-                                       out.println("<li>"+i.next()+"</li>");
-                               out.println("</ul></body></html>");
-                               out.close();
-                               return;
-                       } else if (cmd.equals("/reload")) {
-                               final String key = request.getParameter("key");
-                               if (key == null)
-                                       server.reloadAll();
-                               else
-                                       server.reload(key);
-                       } else if (cmd.equals("/update")) {
-                               final String key = request.getParameter("key");
-                               if (key == null)
-                                       server.updateAll();
-                               else
-                                       server.update(key);
-                       } else if (cmd.equals("/help")) {
-                               response.setContentType("text/html");
-                               out = response.getOutputStream();
-                               out.println("<html><body><h1>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
-                               out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
-                               out.println("<p>usage:</p>"+help+"</body></html>");
-                               out.close();
-                               return;
-                       } else {
-                               sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help);
-                               return;
-                       }       
-               } catch (TransformerConfigurationException tce) {
-                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "stylesheet error", tce);
-                       return;
-               } catch (SAXException se) {
-                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "SAX Exception", se);
-                       return;
-               } catch (Exception e) {
-                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "exception", e);
-                       return;
-               }
+           final Properties props = new Properties();
+           final Enumeration e = request.getParameterNames();
+           while (e.hasMoreElements()) {
+              String param = (String) e.nextElement();
+              if (param.startsWith("param.")) {
+                 final String name = param.substring(6);
+                 final String value = request.getParameter(param);
+                 final String[] keyParam = split(name, ".");
+                 if (keyParam.length == 1) {
+                    // this is a global parameter
+                    Server.log("global parameter: " + keyParam[0] + " = " + value);
+                    for (int i = 0; i < keySeq.length; i++)
+                       // we add the global parameter only if there is no
+                       // local parameter with the same name
+                       if (!keySeq[i].params.containsKey(keyParam[0]))
+                          keySeq[i].params.put(keyParam[0], value);
+                 } else
+                 if (keyParam.length == 2) {
+                    // this is a local parameter
+                    Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value);
+                    for (int i = 0; i < keySeq.length; i++) {
+                       if (keySeq[i].name.equals(keyParam[0]))
+                          keySeq[i].params.put(keyParam[1], value);
+                    }
+                 } else { 
+                    sendError(response, out, "bad parameters", usage[6]); return;
+                 }
+              } else
+              if (param.startsWith("prop.")) {
+                 final String name = param.substring(5);
+                 final String value = request.getParameter(param);
+                 Server.log("property: " + name + " = " + value);
+                 props.setProperty(name, value);
+              }
+           }
+                                       
+           String contentType = props.getProperty(OutputKeys.MEDIA_TYPE);
+           if (contentType == null && keySeq.length > 0) 
+              contentType = server.getContentType(keySeq[keySeq.length - 1].name);
+           else if (contentType == null)
+              contentType = "text/xml";
+           response.setContentType(contentType);
+           Server.log("content type: " + contentType);
 
-               response.setContentType("text/html");
-               response.setHeader("Cache-Control", "no-cache");
-               response.setHeader("Pragma", "no-cache");
-               response.setHeader("Expires", "0");
-               out = response.getOutputStream();
-               out.println("<html><body><h1>Uwobo servlet</h1><p>done</p></body></html>");
-               out.close();
-       }
+           try {
+              out = response.getOutputStream(); 
+              server.apply(infile, out, keySeq, props);
+              out.close();
+           } catch (TransformerConfigurationException tce) {
+               sendError(response, out, exc_out("stylesheet error", tce), "");
+            } catch (Exception ee) {
+              sendError(response, out, exc_out(null, ee), "");
+            }
+           return;
+        }
+        if (cmd.equals("/remove")) {
+           final String key = request.getParameter("keys");
+           final String [] data = split(key, ",");
+           html_open(response, out);
+           for (int i = 0; i < data.length; i++) { 
+              if (data[i] == null) {
+                 out.println(msg_out("removing all keys"));
+                 server.removeAll();
+              } else {
+                 out.println(msg_out("removing key "+data[i]));
+                 server.remove(data[i]);
+              }
+           }
+           html_close(out); return;
+        }
+        if (cmd.equals("/list")) {
+           html_open(response, out);
+           out.println("<p>stylesheet list:</p><ul>");
+           
+           Iterator i = server.list().iterator();
+            while (i.hasNext())
+              out.println("<li>"+i.next()+"</li>");
+           out.println("</ul>"); html_close(out); return;
+        }
+        if (cmd.equals("/reload")) {
+           final String key = request.getParameter("keys");
+           final String [] data = split(key, ",");
+           html_open(response, out);
+           for (int i = 0; i < data.length; i++) {
+              try {
+                 if (data[i] == null) {
+                    out.println(msg_out("reloading all keys"));
+                    server.reloadAll();
+                 } else {
+                    out.println(msg_out("reloading key "+data[i]));
+                    server.reload(data[i]);
+                 }
+              } catch (TransformerConfigurationException tce) {
+                  out.println(exc_out("stylesheet error", tce));
+               } catch (Exception e) {
+                 out.println(exc_out(null, e));
+               }
+           }
+           html_close(out); return;
+        }
+         if (cmd.equals("/update")) {
+           final String key = request.getParameter("keys");
+            final String [] data = split(key, ",");
+           html_open(response, out);
+           for (int i = 0; i < data.length; i++) {
+              try {
+                 if (data[i] == null) {
+                    out.println(msg_out("updating all keys")); 
+                    server.updateAll();
+                 } else {
+                    out.println(msg_out("updating key "+data[i]));
+                    server.update(data[i]);
+                 }
+              } catch (TransformerConfigurationException tce) {
+                  out.println(exc_out("stylesheet error", tce));
+               } catch (Exception e) {
+                 out.println(exc_out(null, e));
+               }
+           }
+           html_close(out); return;
+        }
+        if (cmd.equals("/help")) {
+           html_open(response, out);
+           out.println("<h2>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
+           out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
+           out.println("<p>usage:</p>"+help+"</body></html>");
+           html_close(out); return;
+        }
+        sendError(response, out, "unknown command", help); return;
+      } catch (Exception e) {
+        sendError(response, out, exc_out(null, e),""); // FG: non dovrebbe servire mai
+        return;
+      }
+   }
 
-       private void sendError(HttpServletResponse response, int code, String msg, Exception e) throws IOException {
-               String err;
-               if (e != null)
-                       err = "<p>"+e.getMessage()+"</P>";
-               else
-                       err = "";
-               response.setContentType("text/html");
-               response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>"+err+"</body></html>");
-       }
-       
-       private void sendError(HttpServletResponse response, int code, String msg, String usage) throws IOException {
-               response.setContentType("text/html");
-               response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>usage: "+usage+"</body></html>");
-       }
+   private void sendError(HttpServletResponse resp, ServletOutputStream out, 
+                         String msg, String usage)
+   throws IOException 
+   {
+      html_open(resp, out);
+      out.println("<p>"+msg+"</p>usage: "+usage);
+      html_close(out);
+   }
        
-       public String getServletInfo() {
-               return "The UWOBO servlet";
-       }
+   public String getServletInfo()
+   {
+      return "The UWOBO servlet";
+   }
 }