2 * put your module comment here
12 import freemarker.template.*;
15 import javax.servlet.http.*;
19 * Hilfsklasse zum Mergen von Template und Daten
21 public final class HTMLTemplateProcessor {
23 public static String templateDir;
24 private static FileTemplateCache templateCache;
25 private static Logfile theLog;
26 private static String docRoot;
27 private static String actionRoot;
28 private static String productionHost;
29 private static String audioHost;
30 private static String videoHost;
31 private static String imageHost;
32 private static String openAction;
33 protected static String producerDocRoot = MirConfig.getProp("Producer.DocRoot");
34 protected static String producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
42 templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
43 templateCache = new FileTemplateCache(templateDir);
44 templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
45 templateCache.startAutoUpdate();
46 docRoot = MirConfig.getProp("RootUri");
47 theLog=Logfile.getInstance("HTMLTemplateProcessor");
48 //the quick hack is back in effect as it was more broken than ever before
50 // sorry: nadir back in town, i have to debug the mirbase.jar in the
51 // nadir evironment. from my point of coding, this needs an urgent
53 // yeah, from my point too - tob.
54 //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
55 //actionRoot = docRoot + "/servlet/NadirAktuell";
57 actionRoot = docRoot + "/servlet/Mir";
58 openAction = MirConfig.getProp("Producer.OpenAction");
59 productionHost = MirConfig.getProp("Producer.ProductionHost");
60 videoHost = MirConfig.getProp("Producer.VideoHost");
61 audioHost = MirConfig.getProp("Producer.AudioHost");
62 imageHost = MirConfig.getProp("Producer.Image.Host");
63 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
64 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
68 * empty private constructor, to avoid instantiation
70 private HTMLTemplateProcessor () {}
73 // process-methods to merge different datastructures
74 // with freemarker templates
78 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
79 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
82 * @param templateFilename
85 * @exception HTMLParseException
88 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
89 throws HTMLParseException {
90 if (anEntity == null) throw new HTMLParseException("entity is empty!");
91 else process(templateFilename, anEntity, out);
96 * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
97 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
100 * @param templateFilename
103 * @exception HTMLParseException
105 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
106 throws HTMLParseException {
107 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out);
111 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
112 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
113 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
116 * @param templateFilename
118 * @param additionalModelName
119 * @param additionalModel
121 * @exception HTMLParseException
123 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
124 TemplateModelRoot additionalModel, PrintWriter out)
125 throws HTMLParseException {
127 SimpleHash modelRoot = new SimpleHash();
129 if (entList == null) {
130 process(null,templateFilename, modelRoot, out);
133 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
135 // Quickhack um mal ein Popup mit reinzunhemen ..
136 if (additionalModelName != null && additionalModel != null)
137 modelRoot.put(additionalModelName, additionalModel);
139 process(res,templateFilename, modelRoot, out);
140 } catch (StorageObjectException e) {
141 throw new HTMLParseException(e.toString());
147 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
148 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
151 * @param templateFilename
152 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
154 * @exception HTMLParseException
156 public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
157 throws HTMLParseException {
158 process(res,templateFilename, makeSimpleHash(mergeData), out);
162 * Gibt Template <code>templateFilename</code> an den PrintWriter
165 * @param templateFilename
168 * @exception HTMLParseException
170 public static void process(String templateFilename, PrintWriter out)
171 throws HTMLParseException {
172 process(null,templateFilename, (TemplateModelRoot)null, out);
177 * Mischt die freemarker-Struktur <code>tmr</code> mit
178 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
181 * @param templateFilename
184 * @exception HTMLParseException
186 public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
187 throws HTMLParseException {
188 if (out==null) throw new HTMLParseException("no outputstream");
189 Template tmpl = getTemplateFor(templateFilename);
190 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
191 if (tmr==null) tmr = new SimpleHash();
193 /** @todo what is this for? (rk) */
196 session=res.encodeURL("");
199 // put standard configuration into tempalteRootmodel
200 SimpleHash configHash = new SimpleHash();
201 configHash.put("docroot", new SimpleScalar(producerDocRoot));
202 configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
203 configHash.put("productionhost", new SimpleScalar(productionHost));
204 configHash.put("openaction", new SimpleScalar(openAction));
205 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
208 tmr.put("docRoot", new SimpleScalar(docRoot));
209 tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
210 tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
211 tmr.put("openAction", new SimpleScalar(openAction));
212 tmr.put("productionHost", new SimpleScalar(productionHost));
213 tmr.put("videoHost", new SimpleScalar(videoHost));
214 tmr.put("audioHost", new SimpleScalar(audioHost));
215 tmr.put("imageHost", new SimpleScalar(imageHost));
217 tmr.put("config", configHash);
218 tmpl.process(tmr, out);
224 * Converts Entity-List to SimpleList of SimpleHashes.
225 * @param aList ist eine Liste von Entity
226 * @return eine freemarker.template.SimpleList von SimpleHashes.
228 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
230 SimpleList simpleList = new SimpleList();
232 for(int i=0;i<aList.size();i++) {
233 simpleList.add(makeSimpleHash(aList.elementAt(i)));
240 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
241 * sind die einzelnen Entities ueber ihre id zu erreichen.
242 * @param aList ist die EntityList
243 * @return SimpleHash mit den entsprechenden freemarker Daten
246 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
248 SimpleHash simpleHash = new SimpleHash();
249 Entity currentEntity;
252 for (int i=0;i<aList.size();i++) {
253 currentEntity = (Entity)aList.elementAt(i);
254 simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
261 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
262 * @param entity ist die Entity
263 * @return SimpleHash mit den entsprechenden freemarker Daten
266 public static SimpleHash makeSimpleHash(Entity entity) {
268 return makeSimpleHash(entity.getValues());
274 * Konvertiert ein Hashtable mit den keys und values als String
275 * in ein freemarker.template.SimpleHash-Modell
276 * @param mergeData der HashMap mit den String / String Daten
277 * @return SimpleHash mit den entsprechenden freemarker Daten
280 public static SimpleHash makeSimpleHash(HashMap mergeData)
282 SimpleHash modelRoot = new SimpleHash();
284 if (mergeData != null) {
285 Set set = mergeData.keySet();
286 Iterator it = set.iterator();
287 for (int i=0; i<set.size();i++) {
288 aField = (String)it.next();
289 modelRoot.put(aField, (String)mergeData.get(aField));
297 * Converts EntityList in SimpleHash and adds additional information
298 * to the returned SimpleHash
301 * @return SimpleHash returns SimpleHash with the converted EntityList plus
302 * additional Data about the list.
303 * @exception StorageObjectException
306 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
307 SimpleHash modelRoot = new SimpleHash();
309 modelRoot.put("contentlist", makeSimpleList(entList));
310 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
311 if (entList.getWhere()!=null) {
312 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
313 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
315 if(entList.getOrder()!=null) {
316 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
317 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
319 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
320 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
322 if (entList.hasNextBatch())
323 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
324 if (entList.hasPrevBatch())
325 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
331 * Private methods to get template from a templateFilename
332 * @param templateFilename
334 * @exception HTMLParseException
336 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
338 if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
340 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
341 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
345 public static void stopAutoUpdate(){
346 templateCache.stopAutoUpdate();