]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java
This commit was manufactured by cvs2svn to create branch 'init'.
[helm.git] / helm / uwobo / src / it / unibo / cs / helm / uwobo / Server.java
diff --git a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java
deleted file mode 100644 (file)
index 293b2b9..0000000
+++ /dev/null
@@ -1,423 +0,0 @@
-/* Copyright (C) 2001, HELM Team
- *
- * This file is part of UWOBO, developed at the Computer Science
- * Department, University of Bologna, Italy.
- * 
- * UWOBO is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * UWOBO is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with UWOBO; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA  02111-1307, USA.
- *
- * For details, see the UWOBO World-Wide-Web page,
- * http://cs.unibo.it/helm/uwobo
- */
-
-package it.unibo.cs.helm.uwobo;
-
-import java.io.*;
-import java.net.URL;
-import java.util.*;
-import javax.xml.transform.*;
-import javax.xml.transform.sax.*;
-import javax.xml.transform.stream.*;
-import org.apache.xalan.serialize.*;
-import org.apache.xalan.xslt.*;
-import org.apache.xalan.templates.*;
-import org.apache.xalan.transformer.*;
-import org.apache.xerces.parsers.*;
-import org.xml.sax.*;
-import org.xml.sax.ext.*;
-import org.xml.sax.helpers.*;
-
-/**
-// This file is part of UWOBO, a small and simple XSLT server.
-// 
-// UWOBO is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// UWOBO is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with UWOBO; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-// 
-// For details, send a mail to <luca.padovani@cs.unibo.it>
-* @author Luca Padovani, Riccardo Solmi
-*/
-
-public class Server {
-       private static class Style implements Serializable {
-               public String fileName;
-               public Templates stylesheet;
-       };
-
-       public static final String SERVERNAME = "uwobo-XSLT-server";
-       public static final String PACKAGE;
-       public static final String VERSION;
-       public static final String DATE;
-       public static final String TIME;
-       public static final String SERIALIZATION_DIR;
-
-       static {
-               Properties props = new Properties();
-               try {
-                       InputStream in = Server.class.getResourceAsStream("properties.txt");
-                       props.load(in);
-                       in.close();
-               } catch (IOException ioe) {
-                       System.err.println("Could not load the version information.");
-               }
-
-               PACKAGE = props.getProperty("PACKAGE");
-               VERSION = props.getProperty("VERSION");
-               DATE = props.getProperty("DATE");
-               TIME = props.getProperty("TIME");
-                SERIALIZATION_DIR = props.getProperty("SERIALIZATION_DIR");
-       }
-
-        Server() {
-           if(!SERIALIZATION_DIR.equals("")) {
-              log("Looking for serialized stylesheets");
-              File [] serialized = new File(SERIALIZATION_DIR).listFiles();
-              if (serialized == null) {
-                 log("Serialized stylesheets directory \"" + SERIALIZATION_DIR +
-                  "\" not found");
-              } else {
-                 for (int i = 0; i < serialized.length ; i++) {
-                    File filename = serialized[i];
-                    String key = filename.getName();
-                    log("Found serialized stylesheet " + key);
-                    log("Reloading serialized stylesheet \"" + filename + "\"... ");
-                    FileInputStream istream;
-                    try {
-                       istream = new FileInputStream(filename);
-                       ObjectInputStream p = new ObjectInputStream(istream);
-                       Style style = (Style)p.readObject();
-                       istream.close();
-                       hashMap.put(key, style);
-                    } catch (Exception e) {log(e.toString());};
-                 }
-                 log("Serialized stylesheets loaded!");
-              }
-           } else
-              log("Stylesheet serialization is off. Set the property SERIALIZATION_DIR to a non-empty value to turn it on.");
-          
-        }
-
-       private final HashMap hashMap = new HashMap();
-       private static int logCounter = 0;
-       
-       private final String compileStylesheet(Style style, String key) throws TransformerConfigurationException, SAXException, IOException {
-               StreamSource streamsource =
-                       new StreamSource(new URL(style.fileName).openStream());
-               streamsource.setSystemId(style.fileName);
-                Templates templates =
-                   ((SAXTransformerFactory)TransformerFactory.newInstance())
-                      .newTemplates(streamsource);
-                style.stylesheet = templates;
-                File serializationFile = new File(SERIALIZATION_DIR,key);
-                String res = "";
-                if(!SERIALIZATION_DIR.equals("")) {
-                   try {
-                      FileOutputStream ostream =
-                         new FileOutputStream(serializationFile);
-                      ObjectOutputStream p = new ObjectOutputStream(ostream);
-                      p.writeObject(style);
-                      p.flush();
-                      ostream.close();
-                      res =
-                       "Stylesheet serialized in \"" + serializationFile + "\"";
-                     log(res);
-                   } catch (FileNotFoundException e) {
-                      res = "Warning: Stylesheet not " +
-                         "serialized. Error opening " + "file \"" +
-                         serializationFile + "\"";
-                     log(res);
-                      res = htmlOfWarning(res);
-                   }
-                }
-                return res;
-       }
-
-       private String getContentType(Templates templates) {
-               final Properties oprops = templates.getOutputProperties();
-               final String encoding = oprops.getProperty(OutputKeys.ENCODING);  
-               final String media = oprops.getProperty(OutputKeys.MEDIA_TYPE);
-
-               if (media != null) {
-                       if (encoding != null)
-                               return media + "; charset=" + encoding;
-                       return media;
-               } else {
-                       final String method = oprops.getProperty(OutputKeys.METHOD);
-                       if (method.equals("html"))
-                               return "text/html";
-                       else if (method.equals("text"))
-                               return "text/plain";
-                       else 
-                               return "text/xml";
-               }
-       }
-
-       private final ContentHandler applyStylesheet(Templates stylesheet, HashMap params, ContentHandler saxOutput)
-       throws TransformerConfigurationException, SAXException
-       {
-               TransformerHandler th = ((SAXTransformerFactory)TransformerFactory.newInstance()).newTransformerHandler(stylesheet);
-
-               th.setResult(new SAXResult(saxOutput));
-
-               if (params != null) {
-                       final Transformer transformer = th.getTransformer();
-                       Iterator i = params.keySet().iterator();
-                       while (i.hasNext()) {
-                               final String name = (String) i.next();
-                               final String value = (String) params.get(name);
-                               transformer.setParameter(name, value);
-                       }
-               }
-
-               return th;
-       }
-
-       private final void parseFile(String datasheet, ContentHandler saxOutput) throws SAXException, IOException, Exception {
-               final XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
-               reader.setFeature("http://xml.org/sax/features/namespaces", true);
-               reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
-               reader.setContentHandler(saxOutput);
-               if (saxOutput instanceof LexicalHandler)
-                       reader.setProperty("http://xml.org/sax/properties/lexical-handler", (LexicalHandler)saxOutput);
-               reader.parse(datasheet);
-       }
-
-       private final ContentHandler saveFile(OutputStream outputStream, Properties props) throws IOException {
-               final Serializer ser = SerializerFactory.getSerializer(props);
-               ser.setOutputStream(outputStream);
-               return ser.asContentHandler();
-       }
-
-       public static void log(String msg) {
-               System.err.println(SERVERNAME + "[" + logCounter++ + "]: " + msg);
-       }
-
-       public String add(String filename, String key) throws TransformerConfigurationException, SAXException, IOException {
-               log("processing stylesheet \"" + filename + "\" using key " + key + "... ");
-               if (hashMap.containsKey(key)) {
-                   Style style = (Style)hashMap.get(key);
-                   if (style.fileName.equals(filename)) {
-                      String msg =
-                         "Warning: Stylesheet already loaded. Request ignored.";
-                      log(msg);
-                      return htmlOfWarning(msg);
-                   } else {
-                      String res =
-                         "Error: There is already a stylesheet with keyword \""+
-                          key + "\" (aborted)";
-                      log(res);
-                      return htmlOfError(res);
-                   }
-               }
-
-               Style style = new Style();
-               style.fileName = filename;
-               String res = compileStylesheet(style,key);
-                log("done!");
-
-               hashMap.put(key, style);
-                return res;
-       }
-
-       public ArrayList removeAll() throws TransformerConfigurationException, SAXException, IOException {
-                ArrayList res = new ArrayList();
-                String log = "";
-               Iterator i = hashMap.keySet().iterator();
-               while (i.hasNext()) {
-                        String key = (String)i.next();
-                       Style style = (Style)hashMap.get(key);
-                        String msg =
-                         "removing \"" + key + " (" + style.fileName + ")";
-                        res.add(msg);
-                        log(msg);
-                        msg = partialRemove(key,false);
-                        if(!msg.equals(""))
-                           res.add(msg);
-                }
-                hashMap.clear();
-                return res;
-       }
-
-       private String partialRemove(String key, boolean removeFromHashTable) {
-                String res = "";
-               Style style = (Style)hashMap.get(key);
-               if (style != null) {
-                       log("removing \"" + key + " (" + style.fileName + ")");
-                       if (removeFromHashTable)
-                           hashMap.remove(key);
-                        if(!SERIALIZATION_DIR.equals("")) {
-                           File to_delete = new File(SERIALIZATION_DIR,key);
-                           if (!to_delete.delete()) {
-                              res = "Warning: Serialized stylesheet \"" +
-                                 to_delete + "\" to remove not found";
-                              log(res);
-                              res = htmlOfWarning(res);
-                           }
-                        }
-               } else {
-                       res = "Error: stylesheet \"" + key + "\" not loaded";
-                        log(res);
-                        res = htmlOfError(res);
-               }
-                return res;
-       }
-
-       public String remove(String key) {
-           return partialRemove(key,true);
-       }
-
-       public String getContentType(String key) {
-               Style style = (Style)hashMap.get(key);
-               if (style != null) {
-                       return getContentType(style.stylesheet);
-               } else {
-                       log("Error, stylesheet \"" + key + "\" not loaded");
-                       return null;
-               }
-       }
-
-       public List list() {
-               log("listing stylesheets...");
-               ArrayList l = new ArrayList();
-               Iterator i = hashMap.keySet().iterator();
-               while (i.hasNext()) {
-                       String key  = (String)i.next();
-                       Style style = (Style)hashMap.get(key);
-                       l.add("  " + key + " (" + style.fileName + "; " + getContentType(style.stylesheet) + ")");
-                       System.out.println("  " + key + " (" + style.fileName + ")");
-               }
-               log("done!");
-               return l;
-       }
-
-       private ContentHandler applyRec(final Key[] keys, int idx, final ContentHandler saxOutput)
-       throws TransformerConfigurationException, SAXException, IOException
-       {
-               if (idx < 0)
-                       return saxOutput;
-               else {
-                       final Style style = (Style) hashMap.get(keys[idx].name);
-                       if (style == null) {
-                               log("cannot apply unknwon stylesheet \"" + keys[idx].name + "\" (aborted)");
-                               return null;
-                       }
-                       return applyStylesheet(style.stylesheet, keys[idx].params, applyRec(keys, idx - 1, saxOutput));
-               }
-       }
-
-       public void apply(String inFile, OutputStream outputStream, Key[] keys, Properties userProperties)
-       throws IOException, TransformerConfigurationException, SAXException, Exception
-       {
-/*             File outFile = new File(outFilename);
-               if (outFile.exists())
-                       System.out.println("Using cached version\n");
-               else {
-*/                     
-                       final Key[] rkeys = new Key[keys.length];
-                       for (int i = 0; i < keys.length; i++)
-                               rkeys[i] = keys[keys.length - i - 1];
-
-                       Properties outputProperties; 
-                       Properties defaultProperties;
-                       String method;
-
-                       if (keys.length > 0) {
-                               Style style = (Style) hashMap.get(rkeys[0].name);
-                               if (style == null) {
-                                       log("Error, stylesheet \"" + rkeys[0].name + "\" not loaded");
-                                       return;
-                               }
-                               outputProperties = style.stylesheet.getOutputProperties();
-                               method = userProperties.getProperty("method");
-                               if (method == null) method = outputProperties.getProperty("method");
-                               if (method == null) method = "xml";
-                               defaultProperties = org.apache.xalan.templates.OutputProperties.getDefaultMethodProperties(method);
-                       } else {
-                               method = userProperties.getProperty("method");
-                               if (method == null) method = "xml";
-                               outputProperties = org.apache.xalan.templates.OutputProperties.getDefaultMethodProperties(method);
-                               defaultProperties = outputProperties;
-                       }
-
-                       for (Enumeration e = userProperties.propertyNames(); e.hasMoreElements(); ) {
-                               String prop = (String) e.nextElement();
-                               String value = userProperties.getProperty(prop);
-                               if (value.equals("")) {
-                                       String defaultValue = defaultProperties.getProperty(prop);
-                                       if (defaultValue != null)
-                                               outputProperties.setProperty(prop, defaultProperties.getProperty(prop));
-                                       else
-                                               outputProperties.remove(prop);
-                               } else {
-                                       outputProperties.setProperty(prop, value);
-                               }
-                       }
-                       
-                       parseFile(inFile, applyRec(rkeys, rkeys.length - 1, saveFile(outputStream, outputProperties)));
-//             }
-       }
-
-       public ArrayList reloadAll() throws TransformerConfigurationException, SAXException, IOException {
-                ArrayList res = new ArrayList();
-               Iterator i = hashMap.keySet().iterator();
-               while (i.hasNext()) {
-                        String key = (String)i.next();
-                       Style style = (Style)hashMap.get(key);
-                        String msg =
-                         "reloading \"" + key + " (" + style.fileName + ")";
-                        res.add(msg);
-                        log(msg);
-                        msg = reload(key);
-                        if(!msg.equals(""))
-                           res.add(msg);
-                }
-                return res;
-       }
-
-       public String reload(String key) throws TransformerConfigurationException, SAXException, IOException {
-                String res = "";
-               Style style = (Style)hashMap.get(key);
-               if (style != null) {
-                  log("reloading \"" + key + "\"... ");
-                   res= compileStylesheet(style,key);
-                   log("done!");
-               } else {
-                       res = "Error: stylesheet \"" + key + "\" not loaded";
-                        log(res);
-                        res = htmlOfError(res);
-               }
-                return res;
-       }
-
-        private String htmlOfWarning(String message)
-        {
-           return "<b style=\"color: maroon\">" + message+"</b>";
-        }
-
-        private String htmlOfError(String message)
-        {
-           return "<b style=\"color: red\">" + message+"</b>";
-        }
-}