2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mir.generator;
\r
32 import java.io.PrintWriter;
\r
33 import java.util.Date;
\r
34 import java.util.HashMap;
\r
35 import java.util.Iterator;
\r
36 import java.util.List;
\r
37 import java.util.Map;
\r
38 import java.util.Vector;
\r
40 import org.apache.commons.beanutils.MethodUtils;
\r
41 import org.apache.commons.beanutils.PropertyUtils;
\r
42 import freemarker.template.FileTemplateCache;
\r
43 import freemarker.template.SimpleScalar;
\r
44 import freemarker.template.Template;
\r
45 import freemarker.template.TemplateHashModel;
\r
46 import freemarker.template.TemplateListModel;
\r
47 import freemarker.template.TemplateMethodModel;
\r
48 import freemarker.template.TemplateModel;
\r
49 import freemarker.template.TemplateModelException;
\r
50 import freemarker.template.TemplateModelRoot;
\r
51 import freemarker.template.TemplateScalarModel;
\r
52 import mir.log.LoggerWrapper;
\r
53 import mir.util.GeneratorFormatAdapters;
\r
54 import mir.util.RewindableIterator;
\r
57 public class FreemarkerGenerator implements Generator {
\r
58 private Template template;
\r
60 public FreemarkerGenerator(Template aTemplate) {
\r
61 template = aTemplate;
\r
64 public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
\r
65 if (!(anOutputWriter instanceof PrintWriter))
\r
66 throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
\r
69 template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
\r
71 catch (Throwable t) {
\r
72 t.printStackTrace();
\r
73 aLogger.error("Exception occurred: "+t.getMessage());
\r
74 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
75 throw new GeneratorFailure( t );
\r
79 private static TemplateScalarModel makeStringAdapter(String aString) {
\r
80 return new SimpleScalar(aString);
\r
83 private static TemplateHashModel makeMapAdapter(Map aMap) {
\r
84 return new MapAdapter(aMap);
\r
87 private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
\r
88 return new IteratorAdapter(anIterator);
\r
91 private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
\r
92 return new FunctionAdapter(aFunction);
\r
95 private static TemplateHashModel makeBeanAdapter(Object anObject) {
\r
96 return new BeanAdapter(anObject);
\r
99 public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
\r
100 if (anObject == null)
\r
103 if (anObject instanceof TemplateModel)
\r
104 return (TemplateModel) anObject;
\r
105 else if (anObject instanceof Generator.GeneratorFunction)
\r
106 return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
\r
107 else if (anObject instanceof Integer)
\r
108 return makeStringAdapter(((Integer) anObject).toString());
\r
109 else if (anObject instanceof Boolean) {
\r
110 if (((Boolean) anObject).booleanValue())
\r
111 return makeStringAdapter("1");
\r
113 return makeStringAdapter("0");
\r
115 else if (anObject instanceof String)
\r
116 return makeStringAdapter((String) anObject);
\r
117 else if (anObject instanceof Map)
\r
118 return makeMapAdapter((Map) anObject);
\r
119 else if (anObject instanceof Iterator)
\r
120 return makeIteratorAdapter((Iterator) anObject);
\r
121 else if (anObject instanceof List)
\r
122 return makeIteratorAdapter(((List) anObject).iterator());
\r
123 else if (anObject instanceof Number)
\r
124 return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));
\r
125 else if (anObject instanceof Date)
\r
126 return makeAdapter(new GeneratorFormatAdapters.DateFormatAdapter((Date) anObject));
\r
128 return makeBeanAdapter(anObject);
\r
131 private static class MapAdapter implements TemplateModelRoot {
\r
133 private Map valuesCache;
\r
135 private MapAdapter(Map aMap) {
\r
137 valuesCache = new HashMap();
\r
140 public void put(String aKey, TemplateModel aModel) {
\r
141 valuesCache.put(aKey, aModel);
\r
144 public void remove(String aKey) {
\r
147 public boolean isEmpty() {
\r
148 return map.isEmpty();
\r
151 public TemplateModel get(String aKey) throws TemplateModelException {
\r
153 if (!valuesCache.containsKey(aKey)) {
\r
154 Object value = map.get(aKey);
\r
156 if (value == null && !map.containsKey(aKey)) {
\r
157 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
\r
160 valuesCache.put(aKey, makeAdapter(value));
\r
163 return (TemplateModel) valuesCache.get(aKey);
\r
165 catch (TemplateModelException e) {
\r
168 catch (Throwable t) {
\r
169 throw new TemplateModelException(t.getMessage());
\r
174 private static class IteratorAdapter implements TemplateListModel {
\r
175 private Iterator iterator;
\r
176 private List valuesCache;
\r
177 private int position;
\r
179 private IteratorAdapter(Iterator anIterator) {
\r
180 iterator = anIterator;
\r
182 valuesCache = new Vector();
\r
186 if (iterator instanceof RewindableIterator) {
\r
187 ((RewindableIterator) iterator).rewind();
\r
191 public boolean isEmpty() {
\r
192 return valuesCache.isEmpty() && !iterator.hasNext();
\r
195 private void getUntil(int anIndex) throws TemplateModelException {
\r
196 while (valuesCache.size()<=anIndex && iterator.hasNext())
\r
198 valuesCache.add(makeAdapter(iterator.next()));
\r
202 public TemplateModel get(int anIndex) throws TemplateModelException {
\r
203 TemplateModel result;
\r
207 if (anIndex<valuesCache.size())
\r
209 result = (TemplateModel) valuesCache.get(anIndex);
\r
214 throw new TemplateModelException( "Iterator out of bounds" );
\r
217 public boolean hasNext() {
\r
218 return position<valuesCache.size() || iterator.hasNext();
\r
221 public boolean isRewound() {
\r
222 return position==0;
\r
225 public TemplateModel next() throws TemplateModelException {
\r
226 TemplateModel result;
\r
229 result = get(position);
\r
233 throw new TemplateModelException( "Iterator out of bounds" );
\r
238 public void rewind() {
\r
243 private static class ListAdapter implements TemplateListModel {
\r
248 private ListAdapter(List aList) {
\r
250 valuesCache = new Vector();
\r
254 public boolean isEmpty() {
\r
255 return list.isEmpty();
\r
258 public TemplateModel get(int i) throws TemplateModelException {
\r
260 if (i>=valuesCache.size() && i<list.size()) {
\r
261 for(int j=valuesCache.size(); j<=i; j++) {
\r
262 valuesCache.add(makeAdapter(list.get(j)));
\r
266 if (i<valuesCache.size())
\r
267 return (TemplateModel) valuesCache.get(i);
\r
269 throw new TemplateModelException( "Iterator out of bounds" );
\r
272 public boolean hasNext() {
\r
273 return position<list.size();
\r
276 public boolean isRewound() {
\r
277 return position==0;
\r
280 public TemplateModel next() throws TemplateModelException {
\r
281 TemplateModel result;
\r
284 result = get(position);
\r
288 throw new TemplateModelException( "Iterator out of bounds" );
\r
294 public void rewind() {
\r
299 private static class FunctionAdapter implements TemplateMethodModel {
\r
300 private Generator.GeneratorFunction function;
\r
302 public FunctionAdapter(Generator.GeneratorFunction aFunction) {
\r
303 function = aFunction;
\r
306 public TemplateModel exec(List anArguments) throws TemplateModelException {
\r
308 return makeAdapter(function.perform(anArguments));
\r
310 catch (Throwable t) {
\r
311 throw new TemplateModelException(t.getMessage());
\r
315 public boolean isEmpty() {
\r
321 private static class BeanAdapter implements TemplateHashModel {
\r
322 private Object object;
\r
324 public BeanAdapter(Object anObject) {
\r
328 public void put(String aKey, TemplateModel aModel) throws TemplateModelException {
\r
329 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
\r
332 public void remove(String aKey) throws TemplateModelException {
\r
333 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
\r
336 public boolean isEmpty() {
\r
340 public TemplateModel get(String aKey) throws TemplateModelException {
\r
342 if (PropertyUtils.isReadable(object, aKey))
\r
343 return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
\r
345 return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
\r
347 catch (Throwable t) {
\r
348 throw new TemplateModelException(t.getMessage());
\r
353 public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
\r
354 private FileTemplateCache templateCache;
\r
356 public FreemarkerGeneratorLibrary(String aTemplateRoot) {
\r
357 templateCache = new FileTemplateCache( aTemplateRoot+"/" );
\r
358 templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
\r
361 public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
\r
362 Template template = (Template) templateCache.getItem(anIdentifier, "template");
\r
364 if (template==null) {
\r
365 throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+anIdentifier);
\r
368 return new FreemarkerGenerator(template);
\r
372 public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory {
\r
373 private String basePath;
\r
375 public FreemarkerGeneratorLibraryFactory(String aBasePath) {
\r
376 basePath = aBasePath;
\r
379 public GeneratorLibrary makeLibrary(String anInitializationString) {
\r
380 return new FreemarkerGeneratorLibrary(basePath+anInitializationString);
\r