c0ebb764cf7e6c0a77ea73c63c22a85696c79f5b
[mir.git] / source / mir / generator / FreemarkerGenerator.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mir.generator;
31
32 import java.io.PrintWriter;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Vector;
39
40 import mir.log.LoggerWrapper;
41 import mir.util.GeneratorFormatAdapters;
42 import mir.util.RewindableIterator;
43
44 import org.apache.commons.beanutils.MethodUtils;
45 import org.apache.commons.beanutils.PropertyUtils;
46
47 import freemarker.template.FileTemplateCache;
48 import freemarker.template.SimpleScalar;
49 import freemarker.template.Template;
50 import freemarker.template.TemplateHashModel;
51 import freemarker.template.TemplateListModel;
52 import freemarker.template.TemplateMethodModel;
53 import freemarker.template.TemplateModel;
54 import freemarker.template.TemplateModelException;
55 import freemarker.template.TemplateModelRoot;
56 import freemarker.template.TemplateScalarModel;
57
58
59 public class FreemarkerGenerator implements Generator {
60   private Template template;
61
62   public FreemarkerGenerator(Template aTemplate) {
63     template = aTemplate;
64   }
65
66   public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
67     if (!(anOutputWriter instanceof PrintWriter))
68       throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
69
70     try {
71       template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
72     }
73     catch (Throwable t) {
74       t.printStackTrace();
75       aLogger.error("Exception occurred: "+t.getMessage());
76       t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
77       throw new GeneratorFailure( t );
78     }
79   }
80
81   private static TemplateScalarModel makeStringAdapter(String aString) {
82     return new SimpleScalar(aString);
83   }
84
85   private static TemplateHashModel makeMapAdapter(Map aMap)  {
86     return new MapAdapter(aMap);
87   }
88
89   private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
90     return new IteratorAdapter(anIterator);
91   }
92
93   private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
94     return new FunctionAdapter(aFunction);
95   }
96
97   private static TemplateHashModel makeBeanAdapter(Object anObject)  {
98     return new BeanAdapter(anObject);
99   }
100
101   public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
102     if (anObject == null)
103       return null;
104
105     if (anObject instanceof TemplateModel)
106       return (TemplateModel) anObject;
107     else if (anObject instanceof Generator.GeneratorFunction)
108       return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
109     else if (anObject instanceof Integer)
110       return makeStringAdapter(((Integer) anObject).toString());
111     else if (anObject instanceof Boolean) {
112       if (((Boolean) anObject).booleanValue())
113         return makeStringAdapter("1");
114       else
115         return makeStringAdapter("0");
116     }
117     else if (anObject instanceof String)
118       return makeStringAdapter((String) anObject);
119     else if (anObject instanceof Map)
120       return makeMapAdapter((Map) anObject);
121     else if (anObject instanceof Iterator)
122       return makeIteratorAdapter((Iterator) anObject);
123     else if (anObject instanceof List)
124       return makeIteratorAdapter(((List) anObject).iterator());
125     else if (anObject instanceof Number)
126       return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));
127     else if (anObject instanceof Date)
128       return makeAdapter(new GeneratorFormatAdapters.DateFormatAdapter((Date) anObject));
129     else
130       return makeBeanAdapter(anObject);
131   }
132
133   private static class MapAdapter implements TemplateModelRoot {
134     private Map map;
135     private Map valuesCache;
136
137     private MapAdapter(Map aMap) {
138       map = aMap;
139       valuesCache = new HashMap();
140     }
141
142     public void put(String aKey, TemplateModel aModel) {
143       valuesCache.put(aKey, aModel);
144     }
145
146     public void remove(String aKey) {
147     }
148
149     public boolean isEmpty() {
150       return map.isEmpty();
151     }
152
153     public TemplateModel get(String aKey) throws TemplateModelException {
154       try {
155         if (!valuesCache.containsKey(aKey)) {
156           Object value = map.get(aKey);
157
158           if (value == null && !map.containsKey(aKey)) {
159             throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
160           }
161
162           valuesCache.put(aKey, makeAdapter(value));
163         }
164
165         return (TemplateModel) valuesCache.get(aKey);
166       }
167       catch (TemplateModelException e) {
168         throw e;
169       }
170       catch (Throwable t) {
171         throw new TemplateModelException(t.getMessage());
172       }
173     }
174   }
175
176   private static class IteratorAdapter implements TemplateListModel {
177     private Iterator iterator;
178     private List valuesCache;
179     private int position;
180
181     private IteratorAdapter(Iterator anIterator) {
182       iterator = anIterator;
183
184       valuesCache = new Vector();
185       position=0;
186
187
188       if (iterator instanceof RewindableIterator) {
189         ((RewindableIterator) iterator).rewind();
190       }
191     }
192
193     public boolean isEmpty() {
194       return valuesCache.isEmpty() && !iterator.hasNext();
195     }
196
197     private void getUntil(int anIndex) throws TemplateModelException {
198       while (valuesCache.size()<=anIndex && iterator.hasNext())
199       {
200         valuesCache.add(makeAdapter(iterator.next()));
201       }
202     };
203
204     public TemplateModel get(int anIndex) throws TemplateModelException {
205       TemplateModel result;
206
207       getUntil(anIndex);
208
209       if (anIndex<valuesCache.size())
210       {
211         result = (TemplateModel) valuesCache.get(anIndex);
212
213         return result;
214       }
215       else
216         throw new TemplateModelException( "Iterator out of bounds" );
217     }
218
219     public boolean hasNext() {
220       return position<valuesCache.size() || iterator.hasNext();
221     }
222
223     public boolean isRewound() {
224       return position==0;
225     }
226
227     public TemplateModel next() throws TemplateModelException {
228       TemplateModel result;
229
230       if (hasNext()) {
231         result = get(position);
232         position++;
233       }
234       else
235         throw new TemplateModelException( "Iterator out of bounds" );
236
237       return result;
238     }
239
240     public void rewind() {
241       position=0;
242     }
243   }
244
245   private static class ListAdapter implements TemplateListModel {
246     List list;
247     List valuesCache;
248     int position;
249
250     private ListAdapter(List aList) {
251       list = aList;
252       valuesCache = new Vector();
253       position=0;
254     }
255
256     public boolean isEmpty() {
257       return list.isEmpty();
258     }
259
260     public TemplateModel get(int i) throws TemplateModelException {
261
262       if (i>=valuesCache.size() && i<list.size()) {
263         for(int j=valuesCache.size(); j<=i; j++) {
264           valuesCache.add(makeAdapter(list.get(j)));
265         }
266       }
267
268       if (i<valuesCache.size())
269         return (TemplateModel) valuesCache.get(i);
270       else
271         throw new TemplateModelException( "Iterator out of bounds" );
272     }
273
274     public boolean hasNext() {
275       return position<list.size();
276     }
277
278     public boolean isRewound() {
279       return position==0;
280     }
281
282     public TemplateModel next() throws TemplateModelException {
283       TemplateModel result;
284
285       if (hasNext()) {
286         result = get(position);
287         position++;
288       }
289       else {
290         throw new TemplateModelException( "Iterator out of bounds" );
291       }
292
293       return result;
294     }
295
296     public void rewind() {
297       position = 0;
298     }
299   }
300
301   private static class FunctionAdapter implements TemplateMethodModel {
302     private Generator.GeneratorFunction function;
303
304     public FunctionAdapter(Generator.GeneratorFunction aFunction) {
305       function = aFunction;
306     }
307
308     public TemplateModel exec(List anArguments) throws TemplateModelException {
309       try {
310         return makeAdapter(function.perform(anArguments));
311       }
312       catch (Throwable t) {
313         throw new TemplateModelException(t.getMessage());
314       }
315     }
316
317     public boolean isEmpty() {
318       return false;
319     }
320
321   }
322
323   private static class BeanAdapter implements TemplateHashModel {
324     private Object object;
325
326     public BeanAdapter(Object anObject) {
327       object = anObject;
328     }
329
330     public void put(String aKey, TemplateModel aModel)  throws TemplateModelException  {
331       throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
332     }
333
334     public void remove(String aKey) throws TemplateModelException  {
335       throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
336     }
337
338     public boolean isEmpty() {
339       return false;
340     }
341
342     public TemplateModel get(String aKey) throws TemplateModelException {
343       try {
344         if (PropertyUtils.isReadable(object, aKey))
345           return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
346         else
347           return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
348       }
349       catch (Throwable t) {
350         throw new TemplateModelException(t.getMessage());
351       }
352     }
353   }
354
355   public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
356     private FileTemplateCache templateCache;
357
358     public FreemarkerGeneratorLibrary(String aTemplateRoot) {
359       templateCache = new FileTemplateCache( aTemplateRoot+"/" );
360       templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
361     }
362
363     public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
364       Template template = (Template) templateCache.getItem(anIdentifier, "template");
365
366       if (template==null) {
367         throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+"/"+anIdentifier);
368       }
369
370       return new FreemarkerGenerator(template);
371     }
372   }
373
374   public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory {
375     private String basePath;
376
377     public FreemarkerGeneratorLibraryFactory(String aBasePath) {
378       basePath = aBasePath;
379     }
380
381     public GeneratorLibrary makeLibrary(String anInitializationString) {
382       return new FreemarkerGeneratorLibrary(basePath+anInitializationString);
383     };
384   }
385 }