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 theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile"));
47 docRoot = MirConfig.getProp("RootUri");
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 //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
54 //actionRoot = docRoot + "/servlet/NadirAktuell";
55 actionRoot = docRoot + "/servlet/Mir";
56 openAction = MirConfig.getProp("Producer.OpenAction");
57 productionHost = MirConfig.getProp("Producer.ProductionHost");
58 videoHost = MirConfig.getProp("Producer.VideoHost");
59 audioHost = MirConfig.getProp("Producer.AudioHost");
60 imageHost = MirConfig.getProp("Producer.Image.Host");
61 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
62 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
66 * empty private constructor, to avoid instantiation
68 private HTMLTemplateProcessor () { }
71 // process-methods to merge different datastructures
72 // with freemarker templates
76 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
77 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
80 * @param templateFilename
83 * @exception HTMLParseException
86 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
87 throws HTMLParseException {
88 if (anEntity == null) throw new HTMLParseException("entity is empty!");
89 else process(templateFilename, anEntity, out);
94 * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
95 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
98 * @param templateFilename
101 * @exception HTMLParseException
103 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
104 throws HTMLParseException {
105 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out);
109 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
110 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
111 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
114 * @param templateFilename
116 * @param additionalModelName
117 * @param additionalModel
119 * @exception HTMLParseException
121 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
122 TemplateModelRoot additionalModel, PrintWriter out)
123 throws HTMLParseException {
125 SimpleHash modelRoot = new SimpleHash();
127 if (entList == null) {
128 process(null,templateFilename, modelRoot, out);
131 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
133 // Quickhack um mal ein Popup mit reinzunhemen ..
134 if (additionalModelName != null && additionalModel != null)
135 modelRoot.put(additionalModelName, additionalModel);
137 process(res,templateFilename, modelRoot, out);
138 } catch (StorageObjectException e) {
139 throw new HTMLParseException(e.toString());
145 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
146 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
149 * @param templateFilename
150 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
152 * @exception HTMLParseException
154 public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
155 throws HTMLParseException {
156 process(res,templateFilename, makeSimpleHash(mergeData), out);
160 * Gibt Template <code>templateFilename</code> an den PrintWriter
163 * @param templateFilename
166 * @exception HTMLParseException
168 public static void process(String templateFilename, PrintWriter out)
169 throws HTMLParseException {
170 process(null,templateFilename, (TemplateModelRoot)null, out);
175 * Mischt die freemarker-Struktur <code>tmr</code> mit
176 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
179 * @param templateFilename
182 * @exception HTMLParseException
184 public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
185 throws HTMLParseException {
186 if (out==null) throw new HTMLParseException("no outputstream");
187 Template tmpl = getTemplateFor(templateFilename);
188 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
189 if (tmr==null) tmr = new SimpleHash();
191 /** @todo what is this for? (rk) */
194 session=res.encodeURL("");
197 // put standard configuration into tempalteRootmodel
198 SimpleHash configHash = new SimpleHash();
199 configHash.put("docroot", new SimpleScalar(producerDocRoot));
200 configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
201 configHash.put("productionhost", new SimpleScalar(productionHost));
202 configHash.put("openaction", new SimpleScalar(openAction));
203 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
206 tmr.put("docRoot", new SimpleScalar(docRoot));
207 tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
208 tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
209 tmr.put("openAction", new SimpleScalar(openAction));
210 tmr.put("productionHost", new SimpleScalar(productionHost));
211 tmr.put("videoHost", new SimpleScalar(videoHost));
212 tmr.put("audioHost", new SimpleScalar(audioHost));
213 tmr.put("imageHost", new SimpleScalar(imageHost));
215 tmr.put("config", configHash);
216 tmpl.process(tmr, out);
222 * Converts Entity-List to SimpleList of SimpleHashes.
223 * @param aList ist eine Liste von Entity
224 * @return eine freemarker.template.SimpleList von SimpleHashes.
226 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
228 SimpleList simpleList = new SimpleList();
230 for(int i=0;i<aList.size();i++) {
231 simpleList.add(makeSimpleHash(aList.elementAt(i)));
238 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
239 * sind die einzelnen Entities ueber ihre id zu erreichen.
240 * @param aList ist die EntityList
241 * @return SimpleHash mit den entsprechenden freemarker Daten
244 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
246 SimpleHash simpleHash = new SimpleHash();
247 Entity currentEntity;
250 for (int i=0;i<aList.size();i++) {
251 currentEntity = (Entity)aList.elementAt(i);
252 simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
259 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
260 * @param entity ist die Entity
261 * @return SimpleHash mit den entsprechenden freemarker Daten
264 public static SimpleHash makeSimpleHash(Entity entity) {
266 return makeSimpleHash(entity.getValues());
272 * Konvertiert ein Hashtable mit den keys und values als String
273 * in ein freemarker.template.SimpleHash-Modell
274 * @param mergeData der HashMap mit den String / String Daten
275 * @return SimpleHash mit den entsprechenden freemarker Daten
278 public static SimpleHash makeSimpleHash(HashMap mergeData)
280 SimpleHash modelRoot = new SimpleHash();
282 if (mergeData != null) {
283 Set set = mergeData.keySet();
284 Iterator it = set.iterator();
285 for (int i=0; i<set.size();i++) {
286 aField = (String)it.next();
287 modelRoot.put(aField, (String)mergeData.get(aField));
295 * Converts EntityList in SimpleHash and adds additional information
296 * to the returned SimpleHash
299 * @return SimpleHash returns SimpleHash with the converted EntityList plus
300 * additional Data about the list.
301 * @exception StorageObjectException
304 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
305 SimpleHash modelRoot = new SimpleHash();
307 modelRoot.put("contentlist", makeSimpleList(entList));
308 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
309 if (entList.getWhere()!=null) {
310 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
311 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
313 if(entList.getOrder()!=null) {
314 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
315 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
317 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
318 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
320 if (entList.hasNextBatch())
321 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
322 if (entList.hasPrevBatch())
323 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
329 * Private methods to get template from a templateFilename
330 * @param templateFilename
332 * @exception HTMLParseException
334 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
336 if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
338 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
339 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
343 public static void stopAutoUpdate(){
344 templateCache.stopAutoUpdate();