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 //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
51 actionRoot = docRoot + "/servlet/Mir";
52 openAction = MirConfig.getProp("Producer.OpenAction");
53 productionHost = MirConfig.getProp("Producer.ProductionHost");
54 videoHost = MirConfig.getProp("Producer.VideoHost");
55 audioHost = MirConfig.getProp("Producer.AudioHost");
56 imageHost = MirConfig.getProp("Producer.Image.Host");
57 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
58 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
62 * empty private constructor, to avoid instantiation
64 private HTMLTemplateProcessor () { }
67 // process-methods to merge different datastructures
68 // with freemarker templates
72 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
73 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
76 * @param templateFilename
79 * @exception HTMLParseException
82 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
83 throws HTMLParseException {
84 if (anEntity == null) throw new HTMLParseException("entity is empty!");
85 else process(templateFilename, anEntity, out);
90 * Wandelt Liste mit Entities <code>entList</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
99 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
100 throws HTMLParseException {
101 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out);
105 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
106 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
107 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
110 * @param templateFilename
112 * @param additionalModelName
113 * @param additionalModel
115 * @exception HTMLParseException
117 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
118 TemplateModelRoot additionalModel, PrintWriter out)
119 throws HTMLParseException {
121 SimpleHash modelRoot = new SimpleHash();
123 if (entList == null) {
124 process(null,templateFilename, modelRoot, out);
127 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
129 // Quickhack um mal ein Popup mit reinzunhemen ..
130 if (additionalModelName != null && additionalModel != null)
131 modelRoot.put(additionalModelName, additionalModel);
133 process(res,templateFilename, modelRoot, out);
134 } catch (StorageObjectException e) {
135 throw new HTMLParseException(e.toString());
141 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
142 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
145 * @param templateFilename
146 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
148 * @exception HTMLParseException
150 public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
151 throws HTMLParseException {
152 process(res,templateFilename, makeSimpleHash(mergeData), out);
156 * Gibt Template <code>templateFilename</code> an den PrintWriter
159 * @param templateFilename
162 * @exception HTMLParseException
164 public static void process(String templateFilename, PrintWriter out)
165 throws HTMLParseException {
166 process(null,templateFilename, (TemplateModelRoot)null, out);
171 * Mischt die freemarker-Struktur <code>tmr</code> mit
172 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
175 * @param templateFilename
178 * @exception HTMLParseException
180 public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
181 throws HTMLParseException {
182 if (out==null) throw new HTMLParseException("no outputstream");
183 Template tmpl = getTemplateFor(templateFilename);
184 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
185 if (tmr==null) tmr = new SimpleHash();
187 /** @todo what is this for? (rk) */
190 session=res.encodeURL("");
193 // put standard configuration into tempalteRootmodel
194 SimpleHash configHash = new SimpleHash();
195 configHash.put("docroot", new SimpleScalar(producerDocRoot));
196 configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
197 configHash.put("productionhost", new SimpleScalar(productionHost));
198 configHash.put("openaction", new SimpleScalar(openAction));
199 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
202 tmr.put("docRoot", new SimpleScalar(docRoot));
203 tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
204 tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
205 tmr.put("openAction", new SimpleScalar(openAction));
206 tmr.put("productionHost", new SimpleScalar(productionHost));
207 tmr.put("videoHost", new SimpleScalar(videoHost));
208 tmr.put("audioHost", new SimpleScalar(audioHost));
209 tmr.put("imageHost", new SimpleScalar(imageHost));
211 tmr.put("config", configHash);
212 tmpl.process(tmr, out);
218 * Converts Entity-List to SimpleList of SimpleHashes.
219 * @param aList ist eine Liste von Entity
220 * @return eine freemarker.template.SimpleList von SimpleHashes.
222 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
224 SimpleList simpleList = new SimpleList();
226 for(int i=0;i<aList.size();i++) {
227 simpleList.add(makeSimpleHash(aList.elementAt(i)));
234 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
235 * sind die einzelnen Entities ueber ihre id zu erreichen.
236 * @param aList ist die EntityList
237 * @return SimpleHash mit den entsprechenden freemarker Daten
240 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
242 SimpleHash simpleHash = new SimpleHash();
243 Entity currentEntity;
246 for (int i=0;i<aList.size();i++) {
247 currentEntity = (Entity)aList.elementAt(i);
248 simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
255 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
256 * @param entity ist die Entity
257 * @return SimpleHash mit den entsprechenden freemarker Daten
260 public static SimpleHash makeSimpleHash(Entity entity) {
262 return makeSimpleHash(entity.getValues());
268 * Konvertiert ein Hashtable mit den keys und values als String
269 * in ein freemarker.template.SimpleHash-Modell
270 * @param mergeData der HashMap mit den String / String Daten
271 * @return SimpleHash mit den entsprechenden freemarker Daten
274 public static SimpleHash makeSimpleHash(HashMap mergeData)
276 SimpleHash modelRoot = new SimpleHash();
278 if (mergeData != null) {
279 Set set = mergeData.keySet();
280 Iterator it = set.iterator();
281 for (int i=0; i<set.size();i++) {
282 aField = (String)it.next();
283 modelRoot.put(aField, (String)mergeData.get(aField));
291 * Converts EntityList in SimpleHash and adds additional information
292 * to the returned SimpleHash
295 * @return SimpleHash returns SimpleHash with the converted EntityList plus
296 * additional Data about the list.
297 * @exception StorageObjectException
300 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
301 SimpleHash modelRoot = new SimpleHash();
303 modelRoot.put("contentlist", makeSimpleList(entList));
304 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
305 if (entList.getWhere()!=null) {
306 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
307 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
309 if(entList.getOrder()!=null) {
310 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
311 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
313 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
314 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
316 if (entList.hasNextBatch())
317 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
318 if (entList.hasPrevBatch())
319 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
325 * Private methods to get template from a templateFilename
326 * @param templateFilename
328 * @exception HTMLParseException
330 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
332 if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
334 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
335 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
339 public static void stopAutoUpdate(){
340 templateCache.stopAutoUpdate();