X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fuwobo%2Fsrc%2Fit%2Funibo%2Fcs%2Fhelm%2Fuwobo%2FServer.java;h=293b2b91ba1a1533dfd75d3d61ce280eda551b54;hb=10434de696a721d9ea1b4eddc4169d601ee671e5;hp=cd2f2fa30a2a05251d599ae687bab383a7be623c;hpb=b891e0efda6c3407d3dc861af1315a3f1309cd26;p=helm.git diff --git a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java index cd2f2fa30..293b2b91b 100644 --- a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java +++ b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Server.java @@ -1,3 +1,27 @@ +/* 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.*; @@ -37,9 +61,8 @@ import org.xml.sax.helpers.*; */ public class Server { - private static class Style { + private static class Style implements Serializable { public String fileName; - public long lastModified; public Templates stylesheet; }; @@ -48,6 +71,7 @@ public class Server { 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(); @@ -63,17 +87,71 @@ public class Server { 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 Templates compileStylesheet(String xsl_file) throws TransformerConfigurationException, SAXException, IOException { + private final String compileStylesheet(Style style, String key) throws TransformerConfigurationException, SAXException, IOException { StreamSource streamsource = - new StreamSource(new URL(xsl_file).openStream()); - streamsource.setSystemId(xsl_file); - return ((SAXTransformerFactory)TransformerFactory.newInstance()) - .newTemplates(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) { @@ -136,43 +214,78 @@ public class Server { System.err.println(SERVERNAME + "[" + logCounter++ + "]: " + msg); } - public void add(String filename, String key) throws TransformerConfigurationException, SAXException, IOException { + public String add(String filename, String key) throws TransformerConfigurationException, SAXException, IOException { + log("processing stylesheet \"" + filename + "\" using key " + key + "... "); if (hashMap.containsKey(key)) { - log("there is already a stylesheet with keyword \"" + key + "\" (aborted)"); - return; + 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; - style.lastModified = new File(filename).lastModified(); - log("processing stylesheet \"" + filename + "\"... "); - style.stylesheet = compileStylesheet(filename); - log("done!"); + String res = compileStylesheet(style,key); + log("done!"); hashMap.put(key, style); + return res; } - public void removeAll() - { - String key; - Style style; + public ArrayList removeAll() throws TransformerConfigurationException, SAXException, IOException { + ArrayList res = new ArrayList(); + String log = ""; Iterator i = hashMap.keySet().iterator(); while (i.hasNext()) { - key = (String)i.next(); - style = (Style)hashMap.get(key); - log("removing \"" + key + " (" + style.fileName + ")"); - } - hashMap.clear(); + 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; } - public void remove(String key) { + private String partialRemove(String key, boolean removeFromHashTable) { + String res = ""; Style style = (Style)hashMap.get(key); if (style != null) { log("removing \"" + key + " (" + style.fileName + ")"); - hashMap.remove(key); + 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 { - log("error, stylesheet \"" + key + "\" not loaded"); + 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) { @@ -180,7 +293,7 @@ public class Server { if (style != null) { return getContentType(style.stylesheet); } else { - log("error, stylesheet \"" + key + "\" not loaded"); + log("Error, stylesheet \"" + key + "\" not loaded"); return null; } } @@ -213,7 +326,7 @@ 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 IOException, TransformerConfigurationException, SAXException, Exception { @@ -233,7 +346,7 @@ public class Server { if (keys.length > 0) { Style style = (Style) hashMap.get(rkeys[0].name); if (style == null) { - log("error, stylesheet \"" + rkeys[0].name + "\" not loaded"); + log("Error, stylesheet \"" + rkeys[0].name + "\" not loaded"); return; } outputProperties = style.stylesheet.getOutputProperties(); @@ -266,44 +379,45 @@ public class Server { // } } - public void reloadAll() throws TransformerConfigurationException, SAXException, IOException { + public ArrayList reloadAll() throws TransformerConfigurationException, SAXException, IOException { + ArrayList res = new ArrayList(); Iterator i = hashMap.keySet().iterator(); - while (i.hasNext()) - reload((String)i.next()); + 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 void reload(String key) throws TransformerConfigurationException, SAXException, IOException { + public String reload(String key) throws TransformerConfigurationException, SAXException, IOException { + String res = ""; Style style = (Style)hashMap.get(key); - if (style != null) { - log("reloading \"" + key + "\"... "); - style.stylesheet = compileStylesheet(style.fileName); - style.lastModified = new File(style.fileName).lastModified(); - log("done!"); + if (style != null) { + log("reloading \"" + key + "\"... "); + res= compileStylesheet(style,key); + log("done!"); } else { - log("error, stylesheet \"" + key + "\" not loaded"); + res = "Error: stylesheet \"" + key + "\" not loaded"; + log(res); + res = htmlOfError(res); } + return res; } - public void updateAll() throws TransformerConfigurationException, SAXException, IOException { - Iterator i = hashMap.keySet().iterator(); - while (i.hasNext()) - update((String)i.next()); - } + private String htmlOfWarning(String message) + { + return "" + message+""; + } - public void update(String key) throws TransformerConfigurationException, SAXException, IOException { - Style style = (Style)hashMap.get(key); - 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"); - } - } + private String htmlOfError(String message) + { + return "" + message+""; + } }