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 actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
50 openAction = MirConfig.getProp("Producer.OpenAction");
51 productionHost = MirConfig.getProp("Producer.ProductionHost");
52 videoHost = MirConfig.getProp("Producer.VideoHost");
53 audioHost = MirConfig.getProp("Producer.AudioHost");
54 imageHost = MirConfig.getProp("Producer.Image.Host");
55 producerDocRoot = MirConfig.getProp("Producer.DocRoot");
56 producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
61 * empty private constructor, to avoid instantiation
63 private HTMLTemplateProcessor () { }
66 // process-methods to merge different datastructures
67 // with freemarker templates
71 * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
72 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
75 * @param templateFilename
78 * @exception HTMLParseException
81 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
82 throws HTMLParseException {
83 if (anEntity == null) throw new HTMLParseException("entity is empty!");
84 else process(templateFilename, anEntity, out);
89 * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
90 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
93 * @param templateFilename
96 * @exception HTMLParseException
98 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
99 throws HTMLParseException {
100 process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out);
104 * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
105 * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
106 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
109 * @param templateFilename
111 * @param additionalModelName
112 * @param additionalModel
114 * @exception HTMLParseException
116 public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
117 TemplateModelRoot additionalModel, PrintWriter out)
118 throws HTMLParseException {
120 SimpleHash modelRoot = new SimpleHash();
122 if (entList == null) {
123 process(null,templateFilename, modelRoot, out);
126 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
128 // Quickhack um mal ein Popup mit reinzunhemen ..
129 if (additionalModelName != null && additionalModel != null)
130 modelRoot.put(additionalModelName, additionalModel);
132 process(res,templateFilename, modelRoot, out);
133 } catch (StorageObjectException e) {
134 throw new HTMLParseException(e.toString());
140 * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
141 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
144 * @param templateFilename
145 * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
147 * @exception HTMLParseException
149 public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
150 throws HTMLParseException {
151 process(res,templateFilename, makeSimpleHash(mergeData), out);
155 * Gibt Template <code>templateFilename</code> an den PrintWriter
158 * @param templateFilename
161 * @exception HTMLParseException
163 public static void process(String templateFilename, PrintWriter out)
164 throws HTMLParseException {
165 process(null,templateFilename, (TemplateModelRoot)null, out);
170 * Mischt die freemarker-Struktur <code>tmr</code> mit
171 * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
174 * @param templateFilename
177 * @exception HTMLParseException
179 public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
180 throws HTMLParseException {
181 if (out==null) throw new HTMLParseException("no outputstream");
182 Template tmpl = getTemplateFor(templateFilename);
183 if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
184 if (tmr==null) tmr = new SimpleHash();
186 /** @todo what is this for? (rk) */
189 session=res.encodeURL("");
192 // put standard configuration into tempalteRootmodel
193 SimpleHash configHash = new SimpleHash();
194 configHash.put("docroot", new SimpleScalar(producerDocRoot));
195 configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
196 configHash.put("productionhost", new SimpleScalar(productionHost));
197 configHash.put("openaction", new SimpleScalar(openAction));
198 configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
201 tmr.put("docRoot", new SimpleScalar(docRoot));
202 tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
203 tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
204 tmr.put("openAction", new SimpleScalar(openAction));
205 tmr.put("productionHost", new SimpleScalar(productionHost));
206 tmr.put("videoHost", new SimpleScalar(videoHost));
207 tmr.put("audioHost", new SimpleScalar(audioHost));
208 tmr.put("imageHost", new SimpleScalar(imageHost));
210 tmr.put("config", configHash);
211 tmpl.process(tmr, out);
217 * Converts Entity-List to SimpleList of SimpleHashes.
218 * @param aList ist eine Liste von Entity
219 * @return eine freemarker.template.SimpleList von SimpleHashes.
221 public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
223 SimpleList simpleList = new SimpleList();
225 for(int i=0;i<aList.size();i++) {
226 simpleList.add(makeSimpleHash(aList.elementAt(i)));
233 * Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
234 * sind die einzelnen Entities ueber ihre id zu erreichen.
235 * @param aList ist die EntityList
236 * @return SimpleHash mit den entsprechenden freemarker Daten
239 public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
241 SimpleHash simpleHash = new SimpleHash();
242 Entity currentEntity;
245 for (int i=0;i<aList.size();i++) {
246 currentEntity = (Entity)aList.elementAt(i);
247 simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
254 * Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
255 * @param entity ist die Entity
256 * @return SimpleHash mit den entsprechenden freemarker Daten
259 public static SimpleHash makeSimpleHash(Entity entity) {
261 return makeSimpleHash(entity.getValues());
267 * Konvertiert ein Hashtable mit den keys und values als String
268 * in ein freemarker.template.SimpleHash-Modell
269 * @param mergeData der HashMap mit den String / String Daten
270 * @return SimpleHash mit den entsprechenden freemarker Daten
273 public static SimpleHash makeSimpleHash(HashMap mergeData)
275 SimpleHash modelRoot = new SimpleHash();
277 if (mergeData != null) {
278 Set set = mergeData.keySet();
279 Iterator it = set.iterator();
280 for (int i=0; i<set.size();i++) {
281 aField = (String)it.next();
282 modelRoot.put(aField, (String)mergeData.get(aField));
290 * Converts EntityList in SimpleHash and adds additional information
291 * to the returned SimpleHash
294 * @return SimpleHash returns SimpleHash with the converted EntityList plus
295 * additional Data about the list.
296 * @exception StorageObjectException
299 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
300 SimpleHash modelRoot = new SimpleHash();
302 modelRoot.put("contentlist", makeSimpleList(entList));
303 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
304 if (entList.getWhere()!=null) {
305 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
306 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
308 if(entList.getOrder()!=null) {
309 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
310 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
312 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
313 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
315 if (entList.hasNextBatch())
316 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
317 if (entList.hasPrevBatch())
318 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
324 * Private methods to get template from a templateFilename
325 * @param templateFilename
327 * @exception HTMLParseException
329 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
331 if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
333 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
334 throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
338 public static void stopAutoUpdate(){
339 templateCache.stopAutoUpdate();