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 = Configuration.getProperty("Producer.DocRoot");
34 protected static String producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
41 templateDir = Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Dir");
42 templateCache = new FileTemplateCache(templateDir);
43 templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
44 //templateCache.startAutoUpdate();
45 theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Logfile"));
46 docRoot = Configuration.getProperty("HTMLTemplateProcessor.DocRoot");
47 actionRoot = Configuration.getProperty("HTMLTemplateProcessor.ActionRoot");
48 openAction = Configuration.getProperty("Producer.OpenAction");
49 productionHost = Configuration.getProperty("Producer.ProductionHost");
50 videoHost = Configuration.getProperty("Producer.VideoHost");
51 audioHost = Configuration.getProperty("Producer.AudioHost");
52 imageHost = Configuration.getProperty("Producer.Image.Host");
53 producerDocRoot = Configuration.getProperty("Producer.DocRoot");
54 producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
60 * Privater Konstruktor, um versehentliche Instantiierung zu verhindern
62 private HTMLTemplateProcessor () {
67 // process-Methoden zum Mischen verschiedener Datenstrukturen mit HTML-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 leer!");
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 //theLog.printInfo("Keine Daten! Suche erfolglos.");
124 process(null,templateFilename, modelRoot, out);
127 modelRoot = makeSimpleHashWithEntitylistInfos(entList);
129 // Hilfskruecke 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
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("KEIN OUTPUTSTREAM");
183 Template tmpl = getTemplateFor(templateFilename);
184 if (tmpl == null) throw new HTMLParseException("KEIN TEMPLATE: " + templateFilename);
185 if (tmr==null) tmr = new SimpleHash();
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 * Wandelt eine Entity-Liste in eine SimpleList von SimpleHashes um.
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) {
260 if (entity != null) {
261 return makeSimpleHash(entity.getValues());
264 //theLog.printWarning("Entity ist <null>");
270 * Konvertiert ein Hashtable mit den keys und values als String
271 * in ein freemarker.template.SimpleHash-Modell
272 * @param mergeData der HashMap mit den String / String Daten
273 * @return SimpleHash mit den entsprechenden freemarker Daten
276 public static SimpleHash makeSimpleHash(HashMap mergeData)
278 SimpleHash modelRoot = new SimpleHash();
280 if (mergeData != null) {
281 Set set = mergeData.keySet();
282 Iterator it = set.iterator();
283 for (int i=0; i<set.size();i++) {
284 aField = (String)it.next();
285 modelRoot.put(aField, (String)mergeData.get(aField));
293 * Wandelt EntityListe in SimpleHash um, und versieht die Liste mit StandardInfos,
294 * die aus EntityList kommen.
297 * @return SimpleHash mit Entity-Daten und ZusatzInfos.
298 * @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())));
310 if(entList.getOrder()!=null) {
311 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
312 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
314 modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
315 modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
317 if (entList.hasNextBatch())
318 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
319 if (entList.hasPrevBatch())
320 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
326 * Private Methode, um für templateFilename das Template zu bekommen.
327 * @param templateFilename
329 * @exception HTMLParseException
331 private static Template getTemplateFor(String templateFilename) throws HTMLParseException
333 if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
335 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
336 throw new HTMLParseException("Templatedatei: "+ templateFilename + " nicht gefunden!");
341 public static void stopAutoUpdate(){
342 templateCache.stopAutoUpdate();