2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mir.generator;
33 import java.io.PrintWriter;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
41 import mir.log.LoggerWrapper;
42 import mir.util.GeneratorFormatAdapters;
43 import mir.util.RewindableIterator;
45 import org.apache.commons.beanutils.MethodUtils;
46 import org.apache.commons.beanutils.PropertyUtils;
48 import freemarker.template.FileTemplateCache;
49 import freemarker.template.SimpleScalar;
50 import freemarker.template.Template;
51 import freemarker.template.TemplateHashModel;
52 import freemarker.template.TemplateListModel;
53 import freemarker.template.TemplateMethodModel;
54 import freemarker.template.TemplateModel;
55 import freemarker.template.TemplateModelException;
56 import freemarker.template.TemplateModelRoot;
57 import freemarker.template.TemplateScalarModel;
60 public class FreemarkerGenerator implements Generator {
61 private Template template;
63 public FreemarkerGenerator(Template aTemplate) {
67 public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
68 if (!(anOutputWriter instanceof PrintWriter))
69 throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
72 template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
76 aLogger.error("Exception occurred: "+t.getMessage());
77 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
78 throw new GeneratorFailure( t );
82 private static TemplateScalarModel makeStringAdapter(String aString) {
83 return new SimpleScalar(aString);
86 private static TemplateHashModel makeMapAdapter(Map aMap) {
87 return new MapAdapter(aMap);
90 private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
91 return new IteratorAdapter(anIterator);
94 private static TemplateMethodModel makeFunctionAdapter(Generator.Function aFunction) {
95 return new FunctionAdapter(aFunction);
98 private static TemplateHashModel makeBeanAdapter(Object anObject) {
99 return new BeanAdapter(anObject);
102 public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
103 if (anObject == null)
106 if (anObject instanceof TemplateModel)
107 return (TemplateModel) anObject;
108 else if (anObject instanceof Generator.Function)
109 return makeFunctionAdapter((Generator.Function) anObject);
110 else if (anObject instanceof Integer)
111 return makeStringAdapter(anObject.toString());
112 else if (anObject instanceof Boolean) {
113 if (((Boolean) anObject).booleanValue())
114 return makeStringAdapter("1");
116 return makeStringAdapter("0");
118 else if (anObject instanceof String)
119 return makeStringAdapter((String) anObject);
120 else if (anObject instanceof Map)
121 return makeMapAdapter((Map) anObject);
122 else if (anObject instanceof Iterator)
123 return makeIteratorAdapter((Iterator) anObject);
124 else if (anObject instanceof List)
125 return makeIteratorAdapter(((List) anObject).iterator());
126 else if (anObject instanceof Number)
127 return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));
128 else if (anObject instanceof Date)
129 return makeAdapter(new GeneratorFormatAdapters.DateFormatAdapter((Date) anObject));
131 return makeBeanAdapter(anObject);
134 private static class MapAdapter implements TemplateModelRoot {
136 private Map valuesCache;
138 private MapAdapter(Map aMap) {
140 valuesCache = new HashMap();
143 public void put(String aKey, TemplateModel aModel) {
144 valuesCache.put(aKey, aModel);
147 public void remove(String aKey) {
150 public boolean isEmpty() {
151 return map.isEmpty();
154 public TemplateModel get(String aKey) throws TemplateModelException {
156 if (!valuesCache.containsKey(aKey)) {
157 Object value = map.get(aKey);
159 if (value == null && !map.containsKey(aKey)) {
160 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
163 valuesCache.put(aKey, makeAdapter(value));
166 return (TemplateModel) valuesCache.get(aKey);
168 catch (TemplateModelException e) {
171 catch (Throwable t) {
172 throw new TemplateModelException(t.getMessage());
177 private static class IteratorAdapter implements TemplateListModel {
178 private Iterator iterator;
179 private List valuesCache;
180 private int position;
182 private IteratorAdapter(Iterator anIterator) {
183 iterator = anIterator;
185 valuesCache = new ArrayList();
189 if (iterator instanceof RewindableIterator) {
190 ((RewindableIterator) iterator).rewind();
194 public boolean isEmpty() {
195 return valuesCache.isEmpty() && !iterator.hasNext();
198 private void getUntil(int anIndex) throws TemplateModelException {
199 while (valuesCache.size()<=anIndex && iterator.hasNext())
201 valuesCache.add(makeAdapter(iterator.next()));
205 public TemplateModel get(int anIndex) throws TemplateModelException {
206 TemplateModel result;
210 if (anIndex<valuesCache.size())
212 result = (TemplateModel) valuesCache.get(anIndex);
216 throw new TemplateModelException( "Iterator out of bounds" );
219 public boolean hasNext() {
220 return position<valuesCache.size() || iterator.hasNext();
223 public boolean isRewound() {
227 public TemplateModel next() throws TemplateModelException {
228 TemplateModel result;
231 result = get(position);
235 throw new TemplateModelException( "Iterator out of bounds" );
240 public void rewind() {
245 private static class ListAdapter implements TemplateListModel {
250 private ListAdapter(List aList) {
252 valuesCache = new ArrayList();
256 public boolean isEmpty() {
257 return list.isEmpty();
260 public TemplateModel get(int i) throws TemplateModelException {
262 if (i>=valuesCache.size() && i<list.size()) {
263 for(int j=valuesCache.size(); j<=i; j++) {
264 valuesCache.add(makeAdapter(list.get(j)));
268 if (i<valuesCache.size())
269 return (TemplateModel) valuesCache.get(i);
271 throw new TemplateModelException( "Iterator out of bounds" );
274 public boolean hasNext() {
275 return position<list.size();
278 public boolean isRewound() {
282 public TemplateModel next() throws TemplateModelException {
283 TemplateModel result;
286 result = get(position);
290 throw new TemplateModelException( "Iterator out of bounds" );
296 public void rewind() {
301 private static class FunctionAdapter implements TemplateMethodModel {
302 private Generator.Function function;
304 public FunctionAdapter(Generator.Function aFunction) {
305 function = aFunction;
308 public TemplateModel exec(List anArguments) throws TemplateModelException {
310 return makeAdapter(function.perform(anArguments));
312 catch (Throwable t) {
313 throw new TemplateModelException(t.getMessage());
317 public boolean isEmpty() {
323 private static class BeanAdapter implements TemplateHashModel {
324 private Object object;
326 public BeanAdapter(Object anObject) {
330 public void put(String aKey, TemplateModel aModel) throws TemplateModelException {
331 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
334 public void remove(String aKey) throws TemplateModelException {
335 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
338 public boolean isEmpty() {
342 public TemplateModel get(String aKey) throws TemplateModelException {
344 if (PropertyUtils.isReadable(object, aKey))
345 return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
346 return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
348 catch (Throwable t) {
349 throw new TemplateModelException(t.getMessage());
354 public static class FreemarkerGeneratorLibrary implements Library {
355 private FileTemplateCache templateCache;
357 public FreemarkerGeneratorLibrary(File aTemplateRoot) {
358 templateCache = new FileTemplateCache(aTemplateRoot);
359 templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
362 public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
363 Template template = (Template) templateCache.getItem(anIdentifier, "template");
365 if (template==null) {
366 throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template " +
367 templateCache.getDirectory() + File.separatorChar + anIdentifier);
370 return new FreemarkerGenerator(template);
374 public static class FreemarkerGeneratorLibraryFactory implements LibraryFactory {
375 private File basePath;
377 public FreemarkerGeneratorLibraryFactory(File aBasePath) {
378 basePath = aBasePath;
381 public Library makeLibrary(String anInitializationString) {
382 // todo: the initialization string should be parsed
383 return new FreemarkerGeneratorLibrary(new File(basePath, anInitializationString));