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;
32 import java.io.PrintWriter;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Vector;
39 import mir.log.LoggerWrapper;
42 import org.apache.commons.beanutils.*;
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;
56 public class FreemarkerGenerator implements Generator {
57 private Template template;
59 public FreemarkerGenerator(Template aTemplate) {
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");
68 template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
72 aLogger.error("Exception occurred: "+t.getMessage());
73 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
74 throw new GeneratorFailure( t );
78 private static TemplateScalarModel makeStringAdapter(String aString) {
79 return new SimpleScalar(aString);
82 private static TemplateHashModel makeMapAdapter(Map aMap) {
83 return new MapAdapter(aMap);
86 private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
87 return new IteratorAdapter(anIterator);
90 private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
91 return new FunctionAdapter(aFunction);
94 private static TemplateHashModel makeBeanAdapter(Object anObject) {
95 return new BeanAdapter(anObject);
98 public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
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");
112 return makeStringAdapter("0");
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));
125 return makeBeanAdapter(anObject);
128 private static class MapAdapter implements TemplateModelRoot {
130 private Map valuesCache;
132 private MapAdapter(Map aMap) {
134 valuesCache = new HashMap();
137 public void put(String aKey, TemplateModel aModel) {
138 valuesCache.put(aKey, aModel);
141 public void remove(String aKey) {
142 // ML: kinda tricky...
145 public boolean isEmpty() {
146 return map.isEmpty();
149 public TemplateModel get(String aKey) throws TemplateModelException {
151 if (!valuesCache.containsKey(aKey)) {
152 Object value = map.get(aKey);
154 if (value == null && !map.containsKey(aKey)) {
155 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
158 valuesCache.put(aKey, makeAdapter(value));
161 return (TemplateModel) valuesCache.get(aKey);
163 catch (TemplateModelException e) {
166 catch (Throwable t) {
167 throw new TemplateModelException(t.getMessage());
172 private static class IteratorAdapter implements TemplateListModel {
173 private Iterator iterator;
174 private List valuesCache;
175 private int position;
177 private IteratorAdapter(Iterator anIterator) {
178 iterator = anIterator;
180 valuesCache = new Vector();
184 if (iterator instanceof RewindableIterator) {
185 ((RewindableIterator) iterator).rewind();
189 public boolean isEmpty() {
190 return valuesCache.isEmpty() && !iterator.hasNext();
193 private void getUntil(int anIndex) throws TemplateModelException {
194 while (valuesCache.size()<=anIndex && iterator.hasNext())
196 valuesCache.add(makeAdapter(iterator.next()));
200 public TemplateModel get(int anIndex) throws TemplateModelException {
201 TemplateModel result;
205 if (anIndex<valuesCache.size())
207 result = (TemplateModel) valuesCache.get(anIndex);
212 throw new TemplateModelException( "Iterator out of bounds" );
215 public boolean hasNext() {
216 return position<valuesCache.size() || iterator.hasNext();
219 public boolean isRewound() {
223 public TemplateModel next() throws TemplateModelException {
224 TemplateModel result;
227 result = get(position);
231 throw new TemplateModelException( "Iterator out of bounds" );
236 public void rewind() {
241 private static class ListAdapter implements TemplateListModel {
246 private ListAdapter(List aList) {
248 valuesCache = new Vector();
252 public boolean isEmpty() {
253 return list.isEmpty();
256 public TemplateModel get(int i) throws TemplateModelException {
258 if (i>=valuesCache.size() && i<list.size()) {
259 for(int j=valuesCache.size(); j<=i; j++) {
260 valuesCache.add(makeAdapter(list.get(j)));
264 if (i<valuesCache.size())
265 return (TemplateModel) valuesCache.get(i);
267 throw new TemplateModelException( "Iterator out of bounds" );
270 public boolean hasNext() {
271 return position<list.size();
274 public boolean isRewound() {
278 public TemplateModel next() throws TemplateModelException {
279 TemplateModel result;
282 result = get(position);
286 throw new TemplateModelException( "Iterator out of bounds" );
292 public void rewind() {
297 private static class FunctionAdapter implements TemplateMethodModel {
298 private Generator.GeneratorFunction function;
300 public FunctionAdapter(Generator.GeneratorFunction aFunction) {
301 function = aFunction;
304 public TemplateModel exec(List anArguments) throws TemplateModelException {
306 return makeAdapter(function.perform(anArguments));
308 catch (Throwable t) {
309 throw new TemplateModelException(t.getMessage());
313 public boolean isEmpty() {
319 private static class BeanAdapter implements TemplateHashModel {
320 private Object object;
322 public BeanAdapter(Object anObject) {
326 public void put(String aKey, TemplateModel aModel) throws TemplateModelException {
327 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
330 public void remove(String aKey) throws TemplateModelException {
331 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
334 public boolean isEmpty() {
338 public TemplateModel get(String aKey) throws TemplateModelException {
340 if (PropertyUtils.isReadable(object, aKey))
341 return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
343 return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
345 catch (Throwable t) {
346 throw new TemplateModelException(t.getMessage());
351 public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
352 private FileTemplateCache templateCache;
354 public FreemarkerGeneratorLibrary(String aTemplateRoot) {
355 templateCache = new FileTemplateCache( aTemplateRoot+"/" );
356 templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
359 public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
360 Template template = (Template) templateCache.getItem(anIdentifier, "template");
362 if (template==null) {
363 throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+anIdentifier);
366 return new FreemarkerGenerator(template);
370 public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory {
371 private String basePath;
373 public FreemarkerGeneratorLibraryFactory(String aBasePath) {
374 basePath = aBasePath;
377 public GeneratorLibrary makeLibrary(String anInitializationString) {
378 return new FreemarkerGeneratorLibrary(basePath+anInitializationString);