2 * put your module comment here
12 import freemarker.template.*;
15 import javax.servlet.http.*;
16 import org.apache.struts.util.MessageResources;
20 * Hilfsklasse zum Mergen von Template und Daten
22 public final class HTMLTemplateProcessor {
24 public static String templateDir;
25 private static FileTemplateCache templateCache;
26 private static Logfile theLog;
27 private static String docRoot;
28 private static String actionRoot;
29 private static String productionHost;
30 private static String audioHost;
31 private static String videoHost;
32 private static String imageHost;
33 private static String imagePath;
34 private static String openAction;
35 protected static String producerDocRoot =
36 MirConfig.getProp("Producer.DocRoot");
37 protected static String producerStorageRoot =
38 MirConfig.getProp("Producer.StorageRoot");
44 /** @todo either in the above block or here :) //rk */
45 templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
46 templateCache = new FileTemplateCache(templateDir);
47 templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
48 // gone in freemarker 1.7.1
49 // templateCache.startAutoUpdate();
50 theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile"));
51 docRoot = MirConfig.getProp("RootUri");
52 //the quick hack is back in effect as it was more broken than ever before
54 // sorry: nadir back in town, i have to debug the mirbase.jar in the
55 // nadir evironment. from my point of coding, this needs an urgent
57 // yeah, from my point too - tob.
58 //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
59 //actionRoot = docRoot + "/servlet/NadirAktuell";
61 actionRoot = docRoot + "/servlet/Mir";
63 openAction = MirConfig.getProp("Producer.OpenAction");
64 productionHost = MirConfig.getProp("Producer.ProductionHost");
65 videoHost = MirConfig.getProp("Producer.VideoHost");
66 audioHost = MirConfig.getProp("Producer.AudioHost");
67 imageHost = MirConfig.getProp("Producer.Image.Host");
68 imagePath = MirConfig.getProp("Producer.Image.Path");
69 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
70 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
74 * empty private constructor, to avoid instantiation
76 private HTMLTemplateProcessor () { }
79 // process-methods to merge different datastructures
80 // with freemarker templates
84 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
85 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
88 * @param templateFilename
91 * @exception HTMLParseException
94 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
95 throws HTMLParseException {
96 if (anEntity == null) throw new HTMLParseException("entity is empty!");
97 else process(templateFilename, anEntity, out);
102 * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
103 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
106 * @param templateFilename
109 * @exception HTMLParseException
111 public static void process(HttpServletResponse res,String templateFilename,
112 EntityList entList, PrintWriter out, Locale locale)
113 throws HTMLParseException {
114 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out, locale);
118 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
119 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
120 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
123 * @param templateFilename
125 * @param additionalModelName
126 * @param additionalModel
128 * @exception HTMLParseException
130 public static void process(HttpServletResponse res,String templateFilename,
131 EntityList entList, String additionalModelName,
132 TemplateModelRoot additionalModel, PrintWriter out,
134 throws HTMLParseException {
136 SimpleHash modelRoot = new SimpleHash();
138 if (entList == null) {
139 process(null,templateFilename, modelRoot, out, locale);
142 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
144 // Quickhack um mal ein Popup mit reinzunhemen ..
145 if (additionalModelName != null && additionalModel != null)
146 modelRoot.put(additionalModelName, additionalModel);
148 process(res,templateFilename, modelRoot, out, locale);
149 } catch (StorageObjectException e) {
150 throw new HTMLParseException(e.toString());
156 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
157 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
160 * @param templateFilename
161 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
163 * @exception HTMLParseException
165 public static void process(HttpServletResponse res,String templateFilename,
166 HashMap mergeData, PrintWriter out, Locale locale)
167 throws HTMLParseException {
168 process(res,templateFilename, makeSimpleHash(mergeData), out, locale);
172 * Gibt Template <code>templateFilename</code> an den PrintWriter
175 * @param templateFilename
178 * @exception HTMLParseException
180 public static void process(String templateFilename, PrintWriter out,
182 throws HTMLParseException {
183 process(null,templateFilename, (TemplateModelRoot)null, out, locale);
188 * Mischt die freemarker-Struktur <code>tmr</code> mit
189 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
192 * @param templateFilename
195 * @exception HTMLParseException
197 public static void process(HttpServletResponse res,String templateFilename,
198 TemplateModelRoot tmr, PrintWriter out, Locale locale)
199 throws HTMLParseException {
200 process(res,templateFilename,tmr,null,out,locale);
205 * Mischt die freemarker-Struktur <code>tmr</code> mit
206 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
209 * @param templateFilename
212 * @exception HTMLParseException
214 public static void process(HttpServletResponse res,String templateFilename,
215 TemplateModelRoot tmr, TemplateModelRoot extra,
216 PrintWriter out, Locale locale)
217 throws HTMLParseException {
218 if (out==null) throw new HTMLParseException("no outputstream");
219 Template tmpl = getTemplateFor(templateFilename);
220 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
221 if (tmr==null) tmr = new SimpleHash();
223 /** @todo what is this for? (rk) */
226 session=res.encodeURL("");
229 // put standard configuration into tempalteRootmodel
230 SimpleHash configHash = new SimpleHash();
231 configHash.put("producerDocRoot", new SimpleScalar(producerDocRoot));
232 configHash.put("storageRoot", new SimpleScalar(producerStorageRoot));
233 configHash.put("productionHost", new SimpleScalar(productionHost));
234 configHash.put("openAction", new SimpleScalar(openAction));
235 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
236 configHash.put("docRoot", new SimpleScalar(docRoot));
237 configHash.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
238 configHash.put("actionRoot", new SimpleScalar(actionRoot+session));
239 configHash.put("videoHost", new SimpleScalar(videoHost));
240 configHash.put("audioHost", new SimpleScalar(audioHost));
241 configHash.put("imageHost", new SimpleScalar(imageHost));
242 configHash.put("imagePath", new SimpleScalar(imagePath));
243 configHash.put("mirVersion", new SimpleScalar(MirConfig.getProp("Mir.Version")));
244 // this conform to updated freemarker syntax
245 configHash.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace() );
247 SimpleHash outPutHash = new SimpleHash();
250 outPutHash.put("extra",extra);
252 while(((SimpleList)extra).hasNext()){
253 theLog.printDebugInfo(((SimpleList)extra).next().toString());
255 }catch(Exception e){}
257 outPutHash.put("data",tmr);
258 outPutHash.put("config", configHash);
260 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
261 outPutHash.put("lang", new MessageMethodModel(locale, messages) );
263 tmpl.process(outPutHash,out);
268 * Converts Entity-List to SimpleList of SimpleHashes.
269 * @param aList ist eine Liste von Entity
270 * @return eine freemarker.template.SimpleList von SimpleHashes.
272 * @deprecated EntityLists comply with TemplateListModel now.
274 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
276 theLog.printWarning("## using deprecated makeSimpleList(entityList) - a waste of resources");
277 SimpleList simpleList = new SimpleList();
279 for(int i=0;i<aList.size();i++) {
280 simpleList.add(aList.elementAt(i));
287 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
288 * sind die einzelnen Entities ueber ihre id zu erreichen.
289 * @param aList ist die EntityList
290 * @return SimpleHash mit den entsprechenden freemarker Daten
293 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
295 SimpleHash simpleHash = new SimpleHash();
296 Entity currentEntity;
299 for (int i=0;i<aList.size();i++) {
300 currentEntity = (Entity)aList.elementAt(i);
301 simpleHash.put(currentEntity.getId(), currentEntity);
308 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
309 * @param entity ist die Entity
310 * @return SimpleHash mit den entsprechenden freemarker Daten
312 * @deprecated This method is deprecated and will be deleted in the next
313 * release. Entity interfaces freemarker.template.TemplateHashModel now
314 * and can be used in the same way as SimpleHash. It is not necessary any
315 * more to make a SimpleHash from an Entity
317 public static SimpleHash makeSimpleHash(Entity entity) {
318 if (entity != null) {
319 theLog.printWarning("## using deprecated makeSimpleHash(entity) - a waste of resources");
320 return makeSimpleHash(entity.getValues());
327 * Konvertiert ein Hashtable mit den keys und values als String
328 * in ein freemarker.template.SimpleHash-Modell
329 * @param mergeData der HashMap mit den String / String Daten
330 * @return SimpleHash mit den entsprechenden freemarker Daten
333 public static SimpleHash makeSimpleHash(HashMap mergeData)
335 SimpleHash modelRoot = new SimpleHash();
337 if (mergeData != null) {
338 Set set = mergeData.keySet();
339 Iterator it = set.iterator();
340 for (int i=0; i<set.size();i++) {
341 aField = (String)it.next();
342 modelRoot.put(aField, (String)mergeData.get(aField));
350 * Converts EntityList in SimpleHash and adds additional information
351 * to the returned SimpleHash
354 * @return SimpleHash returns SimpleHash with the converted EntityList plus
355 * additional Data about the list.
356 * @exception StorageObjectException
359 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
360 SimpleHash modelRoot = new SimpleHash();
362 modelRoot.put("contentlist", entList);
363 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
364 if (entList.getWhere()!=null) {
365 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
366 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
368 if(entList.getOrder()!=null) {
369 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
370 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
372 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
373 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
375 if (entList.hasNextBatch())
376 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
377 if (entList.hasPrevBatch())
378 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
384 * Private methods to get template from a templateFilename
385 * @param templateFilename
387 * @exception HTMLParseException
389 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
391 Template returnTemplate = null;
392 if (templateFilename!=null)
393 returnTemplate = (Template)templateCache.getItem(templateFilename,"template");
396 if (returnTemplate==null) {
397 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
398 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
401 return returnTemplate;
404 public static void stopAutoUpdate(){
405 templateCache.stopAutoUpdate();