X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fmisc%2FHTMLTemplateProcessor.java;h=8a797057951f1a7bc371e9b95873411e99962f4f;hb=546961c7d44afc07790196556aaba1103b0ab4e0;hp=70ec150daf5a71033f654563a918698c071294ca;hpb=5bf2a1fa11c1529a731ab6b4521b8254c1fa100f;p=mir.git diff --git a/source/mir/misc/HTMLTemplateProcessor.java b/source/mir/misc/HTMLTemplateProcessor.java index 70ec150d..8a797057 100755 --- a/source/mir/misc/HTMLTemplateProcessor.java +++ b/source/mir/misc/HTMLTemplateProcessor.java @@ -12,6 +12,7 @@ import java.net.*; import freemarker.template.*; import mir.entity.*; import mir.storage.*; +import javax.servlet.http.*; /** @@ -19,325 +20,345 @@ import mir.storage.*; */ public final class HTMLTemplateProcessor { - public static String templateDir; - private static FileTemplateCache templateCache; - private static Logfile theLog; - private static String docRoot; - private static String actionRoot; - private static String productionHost; - private static String audioHost; - private static String videoHost; - private static String imageHost; - private static String openAction; - protected static String producerDocRoot = Configuration.getProperty("Producer.DocRoot"); - protected static String producerStorageRoot = Configuration.getProperty("Producer.StorageRoot"); - - - // - // Initialisierung - - static { - templateDir = Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Dir"); - templateCache = new FileTemplateCache(templateDir); - templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND); - //templateCache.startAutoUpdate(); - theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Logfile")); - docRoot = Configuration.getProperty("HTMLTemplateProcessor.DocRoot"); - actionRoot = Configuration.getProperty("HTMLTemplateProcessor.ActionRoot"); - openAction = Configuration.getProperty("Producer.OpenAction"); - productionHost = Configuration.getProperty("Producer.ProductionHost"); - videoHost = Configuration.getProperty("Producer.VideoHost"); - audioHost = Configuration.getProperty("Producer.AudioHost"); - imageHost = Configuration.getProperty("Producer.Image.Host"); - producerDocRoot = Configuration.getProperty("Producer.DocRoot"); - producerStorageRoot = Configuration.getProperty("Producer.StorageRoot"); - - - } + public static String templateDir; + private static FileTemplateCache templateCache; + private static Logfile theLog; + private static String docRoot; + private static String actionRoot; + private static String productionHost; + private static String audioHost; + private static String videoHost; + private static String imageHost; + private static String openAction; + protected static String producerDocRoot = MirConfig.getProp("Producer.DocRoot"); + protected static String producerStorageRoot = MirConfig.getProp("Producer.StorageRoot"); + + + // + // init + + static { + + templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir"); + templateCache = new FileTemplateCache(templateDir); + templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND); + // gone in freemarker 1.7.1 + // templateCache.startAutoUpdate(); + theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile")); + docRoot = MirConfig.getProp("RootUri"); + //the quick hack is back in effect as it was more broken than ever before + // -mh + // sorry: nadir back in town, i have to debug the mirbase.jar in the + // nadir evironment. from my point of coding, this needs an urgent + // fixxx. + // yeah, from my point too - tob. + //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName"); + //actionRoot = docRoot + "/servlet/NadirAktuell"; + + actionRoot = docRoot + "/servlet/Mir"; + + openAction = MirConfig.getProp("Producer.OpenAction"); + productionHost = MirConfig.getProp("Producer.ProductionHost"); + videoHost = MirConfig.getProp("Producer.VideoHost"); + audioHost = MirConfig.getProp("Producer.AudioHost"); + imageHost = MirConfig.getProp("Producer.Image.Host"); + producerDocRoot = MirConfig.getProp("Producer.DocRoot"); + producerStorageRoot = MirConfig.getProp("Producer.StorageRoot"); + } /** - * Privater Konstruktor, um versehentliche Instantiierung zu verhindern - */ - private HTMLTemplateProcessor () { - } - - - // - // process-Methoden zum Mischen verschiedener Datenstrukturen mit HTML-Templates - - - /** - * Wandelt anEntity in freemarker-Struktur um, mischt die Daten mit - * Template templateFilename und gibt das Ergebnis an den PrintWriter - * out - * - * @param templateFilename - * @param anEntity - * @param out - * @exception HTMLParseException - */ - - public static void process(String templateFilename, Entity anEntity, PrintWriter out) - throws HTMLParseException { - if (anEntity == null) throw new HTMLParseException("Entity leer!"); - else process(templateFilename, anEntity, out); - } - - - /** - * Wandelt Liste mit Entities entList in freemarker-Struktur um, mischt die Daten mit - * Template templateFilename und gibt das Ergebnis an den PrintWriter - * out - * - * @param templateFilename - * @param entList - * @param out - * @exception HTMLParseException - */ - public static void process(String templateFilename, EntityList entList, PrintWriter out) - throws HTMLParseException { - process( templateFilename, entList, (String)null, (TemplateModelRoot)null, out); - } - - /** - * Wandelt Entitylist in freemarker-Struktur um, fügt additionalModel - * unter dem Namen additionalModelName ein und mischt die Daten mit - * Template templateFilename und gibt das Ergebnis an den PrintWriter - * out - * - * @param templateFilename - * @param entList - * @param additionalModelName - * @param additionalModel - * @param out - * @exception HTMLParseException - */ - public static void process(String templateFilename, EntityList entList, String additionalModelName, - TemplateModelRoot additionalModel, PrintWriter out) - throws HTMLParseException { - - SimpleHash modelRoot = new SimpleHash(); - - if (entList == null) { - //theLog.printInfo("Keine Daten! Suche erfolglos."); - process(templateFilename, modelRoot, out); - } else { - try { - modelRoot = makeSimpleHashWithEntitylistInfos(entList); - // - // Hilfskruecke um mal ein Popup mit reinzunhemen .. - if (additionalModelName != null && additionalModel != null) - modelRoot.put(additionalModelName, additionalModel); - - process(templateFilename, modelRoot, out); - } catch (StorageObjectException e) { - throw new HTMLParseException(e.toString()); - } - } - } - - /** - * Wandelt HashMap mergeData in freemarker-Struktur und mischt diese mit - * Template templateFilename und gibt das Ergebnis an den PrintWriter - * out - * - * @param templateFilename - * @param mergeData - * @param out - * @exception HTMLParseException - */ - public static void process(String templateFilename, HashMap mergeData, PrintWriter out) - throws HTMLParseException { - process(templateFilename, makeSimpleHash(mergeData), out); - } - - /** - * Gibt Template templateFilename an den PrintWriter - * out - * - * @param templateFilename - * @param mergeData - * @param out - * @exception HTMLParseException - */ - public static void process(String templateFilename, PrintWriter out) - throws HTMLParseException { - process(templateFilename, (TemplateModelRoot)null, out); - } - - - /** - * Mischt die freemarker-Struktur tmr mit - * Template templateFilename und gibt das Ergebnis an den PrintWriter - * out - * - * @param templateFilename - * @param mergeData - * @param out - * @exception HTMLParseException - */ - public static void process(String templateFilename, TemplateModelRoot tmr, PrintWriter out) - throws HTMLParseException { - if (out==null) throw new HTMLParseException("KEIN OUTPUTSTREAM"); - Template tmpl = getTemplateFor(templateFilename); - if (tmpl == null) throw new HTMLParseException("KEIN TEMPLATE: " + templateFilename); - if (tmr==null) tmr = new SimpleHash(); - - //some utilities - SimpleHash utilityHash = new SimpleHash(); - utilityHash.put("htmlEscape",new freemarker.template.utility.HtmlEscape()); + * empty private constructor, to avoid instantiation + */ + private HTMLTemplateProcessor () { } + + + // process-methods to merge different datastructures + // with freemarker templates + + + /** + * Wandelt anEntity in freemarker-Struktur um, mischt die Daten mit + * Template templateFilename und gibt das Ergebnis an den PrintWriter + * out + * + * @param templateFilename + * @param anEntity + * @param out + * @exception HTMLParseException + */ + + public static void process(String templateFilename, Entity anEntity, PrintWriter out) + throws HTMLParseException { + if (anEntity == null) throw new HTMLParseException("entity is empty!"); + else process(templateFilename, anEntity, out); + } + + + /** + * Wandelt Liste mit Entities entList in freemarker-Struktur um, mischt die Daten mit + * Template templateFilename und gibt das Ergebnis an den PrintWriter + * out + * + * @param templateFilename + * @param entList + * @param out + * @exception HTMLParseException + */ + public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out) + throws HTMLParseException { + process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out); + } + + /** + * Wandelt Entitylist in freemarker-Struktur um, fügt additionalModel + * unter dem Namen additionalModelName ein und mischt die Daten mit + * Template templateFilename und gibt das Ergebnis an den PrintWriter + * out + * + * @param templateFilename + * @param entList + * @param additionalModelName + * @param additionalModel + * @param out + * @exception HTMLParseException + */ + public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName, + TemplateModelRoot additionalModel, PrintWriter out) + throws HTMLParseException { + + SimpleHash modelRoot = new SimpleHash(); + + if (entList == null) { + process(null,templateFilename, modelRoot, out); + } else { + try { + modelRoot = makeSimpleHashWithEntitylistInfos(entList); + + // Quickhack um mal ein Popup mit reinzunhemen .. + if (additionalModelName != null && additionalModel != null) + modelRoot.put(additionalModelName, additionalModel); + + process(res,templateFilename, modelRoot, out); + } catch (StorageObjectException e) { + throw new HTMLParseException(e.toString()); + } + } + } + + /** + * Wandelt HashMap mergeData in freemarker-Struktur und mischt diese mit + * Template templateFilename und gibt das Ergebnis an den PrintWriter + * out + * + * @param templateFilename + * @param mergeData - a HashMap with mergeData to be converted in SimpleHash + * @param out + * @exception HTMLParseException + */ + public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out) + throws HTMLParseException { + process(res,templateFilename, makeSimpleHash(mergeData), out); + } + + /** + * Gibt Template templateFilename an den PrintWriter + * out + * + * @param templateFilename + * @param mergeData + * @param out + * @exception HTMLParseException + */ + public static void process(String templateFilename, PrintWriter out) + throws HTMLParseException { + process(null,templateFilename, (TemplateModelRoot)null, out); + } + + + /** + * Mischt die freemarker-Struktur tmr mit + * Template templateFilename und gibt das Ergebnis an den PrintWriter + * out + * + * @param templateFilename + * @param mergeData + * @param out + * @exception HTMLParseException + */ + public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out) + throws HTMLParseException { + if (out==null) throw new HTMLParseException("no outputstream"); + Template tmpl = getTemplateFor(templateFilename); + if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename); + if (tmr==null) tmr = new SimpleHash(); + + /** @todo what is this for? (rk) */ + String session=""; + if (res!=null) { + session=res.encodeURL(""); + } // put standard configuration into tempalteRootmodel - SimpleHash configHash = new SimpleHash(); - configHash.put("docroot", new SimpleScalar(producerDocRoot)); - configHash.put("storageroot", new SimpleScalar(producerStorageRoot)); - configHash.put("productionhost", new SimpleScalar(productionHost)); - configHash.put("openaction", new SimpleScalar(openAction)); + SimpleHash configHash = new SimpleHash(); + configHash.put("docroot", new SimpleScalar(producerDocRoot)); + configHash.put("storageroot", new SimpleScalar(producerStorageRoot)); + configHash.put("productionhost", new SimpleScalar(productionHost)); + configHash.put("openaction", new SimpleScalar(openAction)); + configHash.put("actionRootLogin",new SimpleScalar(actionRoot)); tmr.put("docRoot", new SimpleScalar(docRoot)); - tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); - tmr.put("actionRoot", new SimpleScalar(actionRoot)); - tmr.put("openAction", new SimpleScalar(openAction)); - tmr.put("productionHost", new SimpleScalar(productionHost)); - tmr.put("videoHost", new SimpleScalar(videoHost)); - tmr.put("audioHost", new SimpleScalar(audioHost)); - tmr.put("imageHost", new SimpleScalar(imageHost)); + tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); + tmr.put("actionRoot", new SimpleScalar(actionRoot+session)); + tmr.put("openAction", new SimpleScalar(openAction)); + tmr.put("productionHost", new SimpleScalar(productionHost)); + tmr.put("videoHost", new SimpleScalar(videoHost)); + tmr.put("audioHost", new SimpleScalar(audioHost)); + tmr.put("imageHost", new SimpleScalar(imageHost)); tmr.put("config", configHash); - tmr.put("utility",utilityHash); - tmpl.process(tmr, out); + tmpl.process(tmr, out); + + } + + + /** + * Converts Entity-List to SimpleList of SimpleHashes. + * @param aList ist eine Liste von Entity + * @return eine freemarker.template.SimpleList von SimpleHashes. + */ + public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException + { + SimpleList simpleList = new SimpleList(); + if (aList != null) { + for(int i=0;i"); - return null; - } - } - - /** - * Konvertiert ein Hashtable mit den keys und values als String - * in ein freemarker.template.SimpleHash-Modell - * @param mergeData der HashMap mit den String / String Daten - * @return SimpleHash mit den entsprechenden freemarker Daten - * - */ - public static SimpleHash makeSimpleHash(HashMap mergeData) - { - SimpleHash modelRoot = new SimpleHash(); - String aField; - if (mergeData != null) { - Set set = mergeData.keySet(); - Iterator it = set.iterator(); - for (int i=0; i