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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mir.generator;
34 import java.io.PrintWriter;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Vector;
41 import org.apache.struts.util.MessageResources;
42 import org.apache.commons.beanutils.PropertyUtils;
45 import freemarker.template.FileTemplateCache;
46 import freemarker.template.SimpleScalar;
47 import freemarker.template.Template;
48 import freemarker.template.TemplateHashModel;
49 import freemarker.template.TemplateListModel;
50 import freemarker.template.TemplateMethodModel;
51 import freemarker.template.TemplateModel;
52 import freemarker.template.TemplateModelException;
53 import freemarker.template.TemplateModelRoot;
54 import freemarker.template.TemplateScalarModel;
56 import mir.util.RewindableIterator;
60 public class FreemarkerGenerator implements Generator {
61 private Template template;
63 public FreemarkerGenerator(Template aTemplate) {
67 public void generate(Object anOutputWriter, Map aValues, PrintWriter 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.println("Exception occurred: "+t.getMessage());
77 t.printStackTrace(aLogger);
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.GeneratorFunction 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.GeneratorFunction)
109 return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
110 else if (anObject instanceof Integer)
111 return makeStringAdapter(((Integer) 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());
127 return makeBeanAdapter(anObject);
128 // throw new TemplateModelException("Unadaptable class: " + anObject.getClass().getName());
131 private static class MapAdapter implements TemplateModelRoot {
133 private Map valuesCache;
135 private MapAdapter(Map aMap) {
137 valuesCache = new HashMap();
140 public void put(String aKey, TemplateModel aModel) {
141 valuesCache.put(aKey, aModel);
144 public void remove(String aKey) {
145 // ML: kinda tricky...
148 public boolean isEmpty() {
149 return map.isEmpty();
152 public TemplateModel get(String aKey) throws TemplateModelException {
154 if (!valuesCache.containsKey(aKey)) {
155 Object value = map.get(aKey);
157 if (value == null && !map.containsKey(aKey)) {
158 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
161 valuesCache.put(aKey, makeAdapter(value));
164 return (TemplateModel) valuesCache.get(aKey);
166 catch (TemplateModelException e) {
169 catch (Throwable t) {
170 throw new TemplateModelException(t.getMessage());
175 private static class IteratorAdapter implements TemplateListModel {
176 private Iterator iterator;
177 private List valuesCache;
178 private int position;
180 private IteratorAdapter(Iterator anIterator) {
181 iterator = anIterator;
183 valuesCache = new Vector();
187 if (iterator instanceof RewindableIterator) {
188 ((RewindableIterator) iterator).rewind();
192 public boolean isEmpty() {
193 return valuesCache.isEmpty() && !iterator.hasNext();
196 private void getUntil(int anIndex) throws TemplateModelException {
197 while (valuesCache.size()<=anIndex && iterator.hasNext())
199 valuesCache.add(makeAdapter(iterator.next()));
203 public TemplateModel get(int anIndex) throws TemplateModelException {
204 TemplateModel result;
208 if (anIndex<valuesCache.size())
210 result = (TemplateModel) valuesCache.get(anIndex);
215 throw new TemplateModelException( "Iterator out of bounds" );
218 public boolean hasNext() {
219 return position<valuesCache.size() || iterator.hasNext();
222 public boolean isRewound() {
226 public TemplateModel next() throws TemplateModelException {
227 TemplateModel result;
230 result = get(position);
234 throw new TemplateModelException( "Iterator out of bounds" );
239 public void rewind() {
244 private static class ListAdapter implements TemplateListModel {
249 private ListAdapter(List aList) {
251 valuesCache = new Vector();
255 public boolean isEmpty() {
256 return list.isEmpty();
259 public TemplateModel get(int i) throws TemplateModelException {
261 if (i>=valuesCache.size() && i<list.size()) {
262 for(int j=valuesCache.size(); j<=i; j++) {
263 valuesCache.add(makeAdapter(list.get(j)));
267 if (i<valuesCache.size())
268 return (TemplateModel) valuesCache.get(i);
270 throw new TemplateModelException( "Iterator out of bounds" );
273 public boolean hasNext() {
274 return position<list.size();
277 public boolean isRewound() {
281 public TemplateModel next() throws TemplateModelException {
282 TemplateModel result;
285 result = get(position);
289 throw new TemplateModelException( "Iterator out of bounds" );
295 public void rewind() {
300 private static class FunctionAdapter implements TemplateMethodModel {
301 private Generator.GeneratorFunction function;
303 public FunctionAdapter(Generator.GeneratorFunction aFunction) {
304 function = aFunction;
307 public TemplateModel exec(List anArguments) throws TemplateModelException {
309 return makeAdapter(function.perform(anArguments));
311 catch (Throwable t) {
312 throw new TemplateModelException(t.getMessage());
316 public boolean isEmpty() {
322 private static class BeanAdapter implements TemplateHashModel {
323 private Object object;
325 public BeanAdapter(Object anObject) {
329 public void put(String aKey, TemplateModel aModel) throws TemplateModelException {
330 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
333 public void remove(String aKey) throws TemplateModelException {
334 throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
337 public boolean isEmpty() {
341 public TemplateModel get(String aKey) throws TemplateModelException {
343 return makeAdapter(PropertyUtils.getSimpleProperty(object, 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);