fa121c7597a518ea7c834ddca32c34a3c2d48126
[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.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Vector;
38
39 import mir.log.LoggerWrapper;
40 import mir.util.*;
41
42 import org.apache.commons.beanutils.*;
43
44 import freemarker.template.FileTemplateCache;
45 import freemarker.template.SimpleScalar;
46 import freemarker.template.Template;
47 import freemarker.template.TemplateHashModel;
48 import freemarker.template.TemplateListModel;
49 import freemarker.template.TemplateMethodModel;
50 import freemarker.template.TemplateModel;
51 import freemarker.template.TemplateModelException;
52 import freemarker.template.TemplateModelRoot;
53 import freemarker.template.TemplateScalarModel;
54
55
56 public class FreemarkerGenerator implements Generator {
57   private Template template;
58
59   public FreemarkerGenerator(Template aTemplate) {
60     template = aTemplate;
61   }
62
63   public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
64     if (!(anOutputWriter instanceof PrintWriter))
65       throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
66
67     try {
68       template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
69     }
70     catch (Throwable t) {
71       t.printStackTrace();
72       aLogger.error("Exception occurred: "+t.getMessage());
73       t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
74       throw new GeneratorFailure( t );
75     }
76   }
77
78   private static TemplateScalarModel makeStringAdapter(String aString) {
79     return new SimpleScalar(aString);
80   }
81
82   private static TemplateHashModel makeMapAdapter(Map aMap)  {
83     return new MapAdapter(aMap);
84   }
85
86   private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
87     return new IteratorAdapter(anIterator);
88   }
89
90   private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
91     return new FunctionAdapter(aFunction);
92   }
93
94   private static TemplateHashModel makeBeanAdapter(Object anObject)  {
95     return new BeanAdapter(anObject);
96   }
97
98   public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
99     if (anObject == null)
100       return null;
101
102     if (anObject instanceof TemplateModel)
103       return (TemplateModel) anObject;
104     else if (anObject instanceof Generator.GeneratorFunction)
105       return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
106     else if (anObject instanceof Integer)
107       return makeStringAdapter(((Integer) anObject).toString());
108     else if (anObject instanceof Boolean) {
109       if (((Boolean) anObject).booleanValue())
110         return makeStringAdapter("1");
111       else
112         return makeStringAdapter("0");
113     }
114     else if (anObject instanceof String)
115       return makeStringAdapter((String) anObject);
116     else if (anObject instanceof Map)
117       return makeMapAdapter((Map) anObject);
118     else if (anObject instanceof Iterator)
119       return makeIteratorAdapter((Iterator) anObject);
120     else if (anObject instanceof List)
121       return makeIteratorAdapter(((List) anObject).iterator());
122     else if (anObject instanceof Number)
123       return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));
124     else
125       return makeBeanAdapter(anObject);
126   }
127
128   private static class MapAdapter implements TemplateModelRoot {
129     private Map map;
130     private Map valuesCache;
131
132     private MapAdapter(Map aMap) {
133       map = aMap;
134       valuesCache = new HashMap();
135     }
136
137     public void put(String aKey, TemplateModel aModel) {
138       valuesCache.put(aKey, aModel);
139     }
140
141     public void remove(String aKey) {
142       // ML: kinda tricky...
143     }
144
145     public boolean isEmpty() {
146       return map.isEmpty();
147     }
148
149     public TemplateModel get(String aKey) throws TemplateModelException {
150       try {
151         if (!valuesCache.containsKey(aKey)) {
152           Object value = map.get(aKey);
153
154           if (value == null && !map.containsKey(aKey)) {
155             throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
156           }
157
158           valuesCache.put(aKey, makeAdapter(value));
159         }
160
161         return (TemplateModel) valuesCache.get(aKey);
162       }
163       catch (TemplateModelException e) {
164         throw e;
165       }
166       catch (Throwable t) {
167         throw new TemplateModelException(t.getMessage());
168       }
169     }
170   }
171
172   private static class IteratorAdapter implements TemplateListModel {
173     private Iterator iterator;
174     private List valuesCache;
175     private int position;
176
177     private IteratorAdapter(Iterator anIterator) {
178       iterator = anIterator;
179
180       valuesCache = new Vector();
181       position=0;
182
183
184       if (iterator instanceof RewindableIterator) {
185         ((RewindableIterator) iterator).rewind();
186       }
187     }
188
189     public boolean isEmpty() {
190       return valuesCache.isEmpty() && !iterator.hasNext();
191     }
192
193     private void getUntil(int anIndex) throws TemplateModelException {
194       while (valuesCache.size()<=anIndex && iterator.hasNext())
195       {
196         valuesCache.add(makeAdapter(iterator.next()));
197       }
198     };
199
200     public TemplateModel get(int anIndex) throws TemplateModelException {
201       TemplateModel result;
202
203       getUntil(anIndex);
204
205       if (anIndex<valuesCache.size())
206       {
207         result = (TemplateModel) valuesCache.get(anIndex);
208
209         return result;
210       }
211       else
212         throw new TemplateModelException( "Iterator out of bounds" );
213     }
214
215     public boolean hasNext() {
216       return position<valuesCache.size() || iterator.hasNext();
217     }
218
219     public boolean isRewound() {
220       return position==0;
221     }
222
223     public TemplateModel next() throws TemplateModelException {
224       TemplateModel result;
225
226       if (hasNext()) {
227         result = get(position);
228         position++;
229       }
230       else
231         throw new TemplateModelException( "Iterator out of bounds" );
232
233       return result;
234     }
235
236     public void rewind() {
237       position=0;
238     }
239   }
240
241   private static class ListAdapter implements TemplateListModel {
242     List list;
243     List valuesCache;
244     int position;
245
246     private ListAdapter(List aList) {
247       list = aList;
248       valuesCache = new Vector();
249       position=0;
250     }
251
252     public boolean isEmpty() {
253       return list.isEmpty();
254     }
255
256     public TemplateModel get(int i) throws TemplateModelException {
257
258       if (i>=valuesCache.size() && i<list.size()) {
259         for(int j=valuesCache.size(); j<=i; j++) {
260           valuesCache.add(makeAdapter(list.get(j)));
261         }
262       }
263
264       if (i<valuesCache.size())
265         return (TemplateModel) valuesCache.get(i);
266       else
267         throw new TemplateModelException( "Iterator out of bounds" );
268     }
269
270     public boolean hasNext() {
271       return position<list.size();
272     }
273
274     public boolean isRewound() {
275       return position==0;
276     }
277
278     public TemplateModel next() throws TemplateModelException {
279       TemplateModel result;
280
281       if (hasNext()) {
282         result = get(position);
283         position++;
284       }
285       else {
286         throw new TemplateModelException( "Iterator out of bounds" );
287       }
288
289       return result;
290     }
291
292     public void rewind() {
293       position = 0;
294     }
295   }
296
297   private static class FunctionAdapter implements TemplateMethodModel {
298     private Generator.GeneratorFunction function;
299
300     public FunctionAdapter(Generator.GeneratorFunction aFunction) {
301       function = aFunction;
302     }
303
304     public TemplateModel exec(List anArguments) throws TemplateModelException {
305       try {
306         return makeAdapter(function.perform(anArguments));
307       }
308       catch (Throwable t) {
309         throw new TemplateModelException(t.getMessage());
310       }
311     }
312
313     public boolean isEmpty() {
314       return false;
315     }
316
317   }
318
319   private static class BeanAdapter implements TemplateHashModel {
320     private Object object;
321
322     public BeanAdapter(Object anObject) {
323       object = anObject;
324     }
325
326     public void put(String aKey, TemplateModel aModel)  throws TemplateModelException  {
327       throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
328     }
329
330     public void remove(String aKey) throws TemplateModelException  {
331       throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
332     }
333
334     public boolean isEmpty() {
335       return false;
336     }
337
338     public TemplateModel get(String aKey) throws TemplateModelException {
339       try {
340         if (PropertyUtils.isReadable(object, aKey))
341           return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
342         else
343           return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
344       }
345       catch (Throwable t) {
346         throw new TemplateModelException(t.getMessage());
347       }
348     }
349   }
350
351   public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
352     private FileTemplateCache templateCache;
353
354     public FreemarkerGeneratorLibrary(String aTemplateRoot) {
355       templateCache = new FileTemplateCache( aTemplateRoot+"/" );
356       templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
357     }
358
359     public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
360       Template template = (Template) templateCache.getItem(anIdentifier, "template");
361
362       if (template==null) {
363         throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+anIdentifier);
364       }
365
366       return new FreemarkerGenerator(template);
367     }
368   }
369
370   public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory {
371     private String basePath;
372
373     public FreemarkerGeneratorLibraryFactory(String aBasePath) {
374       basePath = aBasePath;
375     }
376
377     public GeneratorLibrary makeLibrary(String anInitializationString) {
378       return new FreemarkerGeneratorLibrary(basePath+anInitializationString);
379     };
380   }
381 }