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 private static String defEncoding;
36 private static String generateFO;
37 private static String generatePDF;
38 protected static String producerDocRoot =
39 MirConfig.getProp("Producer.DocRoot");
40 protected static String producerStorageRoot =
41 MirConfig.getProp("Producer.StorageRoot");
47 /** @todo either in the above block or here :) //rk */
48 templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
49 templateCache = new FileTemplateCache(templateDir);
50 templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
51 // gone in freemarker 1.7.1
52 // templateCache.startAutoUpdate();
53 theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile"));
54 docRoot = MirConfig.getProp("RootUri");
55 //the quick hack is back in effect as it was more broken than ever before
57 // sorry: nadir back in town, i have to debug the mirbase.jar in the
58 // nadir evironment. from my point of coding, this needs an urgent
60 // yeah, from my point too - tob.
61 //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
62 //actionRoot = docRoot + "/servlet/NadirAktuell";
64 actionRoot = docRoot + "/servlet/Mir";
66 defEncoding = MirConfig.getProp("Mir.DefaultEncoding");
67 openAction = MirConfig.getProp("Producer.OpenAction");
68 productionHost = MirConfig.getProp("Producer.ProductionHost");
69 videoHost = MirConfig.getProp("Producer.Video.Host");
70 audioHost = MirConfig.getProp("Producer.Audio.Host");
71 imageHost = MirConfig.getProp("Producer.Image.Host");
72 imagePath = MirConfig.getProp("Producer.Image.Path");
73 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
74 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
75 generateFO = MirConfig.getProp("GenerateFO");
76 generatePDF = MirConfig.getProp("GeneratePDF");
80 * empty private constructor, to avoid instantiation
82 private HTMLTemplateProcessor () { }
85 // process-methods to merge different datastructures
86 // with freemarker templates
90 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
91 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
94 * @param templateFilename
97 * @exception HTMLParseException
100 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
101 throws HTMLParseException {
102 if (anEntity == null) throw new HTMLParseException("entity is empty!");
103 else process(templateFilename, anEntity, out);
108 * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
109 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
112 * @param templateFilename
115 * @exception HTMLParseException
117 public static void process(HttpServletResponse res,String templateFilename,
118 EntityList entList, PrintWriter out, Locale locale)
119 throws HTMLParseException {
120 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out, locale);
124 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
125 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
126 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
129 * @param templateFilename
131 * @param additionalModelName
132 * @param additionalModel
134 * @exception HTMLParseException
136 public static void process(HttpServletResponse res,String templateFilename,
137 EntityList entList, String additionalModelName,
138 TemplateModelRoot additionalModel, PrintWriter out,
140 throws HTMLParseException {
142 SimpleHash modelRoot = new SimpleHash();
144 if (entList == null) {
145 process(null,templateFilename, modelRoot, out, locale);
148 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
150 // Quickhack um mal ein Popup mit reinzunhemen ..
151 if (additionalModelName != null && additionalModel != null)
152 modelRoot.put(additionalModelName, additionalModel);
154 process(res,templateFilename, modelRoot, out, locale);
155 } catch (StorageObjectException e) {
156 throw new HTMLParseException(e.toString());
162 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
163 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
166 * @param templateFilename
167 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
169 * @exception HTMLParseException
171 public static void process(HttpServletResponse res,String templateFilename,
172 HashMap mergeData, PrintWriter out, Locale locale)
173 throws HTMLParseException {
174 process(res,templateFilename, makeSimpleHash(mergeData), out, locale);
178 * Gibt Template <code>templateFilename</code> an den PrintWriter
181 * @param templateFilename
184 * @exception HTMLParseException
186 public static void process(String templateFilename, PrintWriter out,
188 throws HTMLParseException {
189 process(null,templateFilename, (TemplateModelRoot)null, out, locale);
194 * Mischt die freemarker-Struktur <code>tmr</code> mit
195 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
198 * @param templateFilename
201 * @exception HTMLParseException
203 public static void process(HttpServletResponse res,String templateFilename,
204 TemplateModelRoot tmr, PrintWriter out, Locale locale)
205 throws HTMLParseException {
206 process(res,templateFilename,tmr,null,out,locale);
211 * Mischt die freemarker-Struktur <code>tmr</code> mit
212 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
215 * @param templateFilename
218 * @exception HTMLParseException
220 public static void process(HttpServletResponse res,String templateFilename,
221 TemplateModelRoot tmr, TemplateModelRoot extra,
222 PrintWriter out, Locale locale)
223 throws HTMLParseException {
224 if (out==null) throw new HTMLParseException("no outputstream");
225 Template tmpl = getTemplateFor(templateFilename);
226 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
227 if (tmr==null) tmr = new SimpleHash();
229 /** @todo what is this for? (rk) */
232 session=res.encodeURL("");
235 // @todo wouldn't it be so much easier to just pass the whole damn config
236 // Hash here from Mirconfig... ??????? than we could access arbitrary config
237 // values in the templates. -mh
238 // put standard configuration into tempalteRootmodel
239 SimpleHash configHash = new SimpleHash();
240 configHash.put("defEncoding", new SimpleScalar(defEncoding));
241 configHash.put("producerDocRoot", new SimpleScalar(producerDocRoot));
242 configHash.put("storageRoot", new SimpleScalar(producerStorageRoot));
243 configHash.put("productionHost", new SimpleScalar(productionHost));
244 configHash.put("openAction", new SimpleScalar(openAction));
245 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
246 configHash.put("docRoot", new SimpleScalar(docRoot));
247 configHash.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
248 configHash.put("actionRoot", new SimpleScalar(actionRoot+session));
249 configHash.put("videoHost", new SimpleScalar(videoHost));
250 configHash.put("audioHost", new SimpleScalar(audioHost));
251 configHash.put("imageHost", new SimpleScalar(imageHost));
252 configHash.put("imagePath", new SimpleScalar(imagePath));
253 configHash.put("mirVersion", new SimpleScalar(MirConfig.getProp("Mir.Version")));
254 // this conform to updated freemarker syntax
255 configHash.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace() );
256 configHash.put("generateFO", new SimpleScalar(generateFO));
257 configHash.put("generatePDF", new SimpleScalar(generatePDF));
259 SimpleHash outPutHash = new SimpleHash();
262 outPutHash.put("extra",extra);
264 while(((SimpleList)extra).hasNext()){
265 theLog.printDebugInfo(((SimpleList)extra).next().toString());
267 }catch(Exception e){}
269 outPutHash.put("data",tmr);
270 outPutHash.put("config", configHash);
272 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
273 outPutHash.put("lang", new MessageMethodModel(locale, messages) );
275 tmpl.process(outPutHash,out);
280 * Converts Entity-List to SimpleList of SimpleHashes.
281 * @param aList ist eine Liste von Entity
282 * @return eine freemarker.template.SimpleList von SimpleHashes.
284 * @deprecated EntityLists comply with TemplateListModel now.
286 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
288 theLog.printWarning("## using deprecated makeSimpleList(entityList) - a waste of resources");
289 SimpleList simpleList = new SimpleList();
291 for(int i=0;i<aList.size();i++) {
292 simpleList.add(aList.elementAt(i));
299 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
300 * sind die einzelnen Entities ueber ihre id zu erreichen.
301 * @param aList ist die EntityList
302 * @return SimpleHash mit den entsprechenden freemarker Daten
305 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
307 SimpleHash simpleHash = new SimpleHash();
308 Entity currentEntity;
311 for (int i=0;i<aList.size();i++) {
312 currentEntity = (Entity)aList.elementAt(i);
313 simpleHash.put(currentEntity.getId(), currentEntity);
320 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
321 * @param entity ist die Entity
322 * @return SimpleHash mit den entsprechenden freemarker Daten
324 * @deprecated This method is deprecated and will be deleted in the next
325 * release. Entity interfaces freemarker.template.TemplateHashModel now
326 * and can be used in the same way as SimpleHash. It is not necessary any
327 * more to make a SimpleHash from an Entity
329 public static SimpleHash makeSimpleHash(Entity entity) {
330 if (entity != null) {
331 theLog.printWarning("## using deprecated makeSimpleHash(entity) - a waste of resources");
332 return makeSimpleHash(entity.getValues());
339 * Konvertiert ein Hashtable mit den keys und values als String
340 * in ein freemarker.template.SimpleHash-Modell
341 * @param mergeData der HashMap mit den String / String Daten
342 * @return SimpleHash mit den entsprechenden freemarker Daten
345 public static SimpleHash makeSimpleHash(HashMap mergeData)
347 SimpleHash modelRoot = new SimpleHash();
349 if (mergeData != null) {
350 Set set = mergeData.keySet();
351 Iterator it = set.iterator();
352 for (int i=0; i<set.size();i++) {
353 aField = (String)it.next();
354 modelRoot.put(aField, (String)mergeData.get(aField));
362 * Converts EntityList in SimpleHash and adds additional information
363 * to the returned SimpleHash
366 * @return SimpleHash returns SimpleHash with the converted EntityList plus
367 * additional Data about the list.
368 * @exception StorageObjectException
371 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
372 SimpleHash modelRoot = new SimpleHash();
374 modelRoot.put("contentlist", entList);
375 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
376 if (entList.getWhere()!=null) {
377 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
378 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
380 if(entList.getOrder()!=null) {
381 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
382 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
384 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
385 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
387 if (entList.hasNextBatch())
388 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
389 if (entList.hasPrevBatch())
390 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
396 * Private methods to get template from a templateFilename
397 * @param templateFilename
399 * @exception HTMLParseException
401 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
403 Template returnTemplate = null;
404 if (templateFilename!=null)
405 returnTemplate = (Template)templateCache.getItem(templateFilename,"template");
408 if (returnTemplate==null) {
409 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
410 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
413 return returnTemplate;
416 public static void stopAutoUpdate(){
417 templateCache.stopAutoUpdate();