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 freemarker.template.*;
35 import org.apache.struts.util.MessageResources;
42 public class FreemarkerGenerator implements Generator {
43 private Template template;
45 public FreemarkerGenerator(Template aTemplate) {
49 public void generate(Object anOutputWriter, Map aValues, PrintWriter aLogger) throws GeneratorExc, GeneratorFailure {
50 if (!(anOutputWriter instanceof PrintWriter))
51 throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
54 template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
57 aLogger.println("Exception occurred: "+t.getMessage());
58 t.printStackTrace(aLogger);
59 throw new GeneratorFailure( t );
63 private static TemplateScalarModel makeStringAdapter(String aString) {
64 return new SimpleScalar(aString);
67 private static TemplateHashModel makeMapAdapter(Map aMap) {
68 return new MapAdapter(aMap);
71 private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
72 return new IteratorAdapter(anIterator);
75 private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
76 return new FunctionAdapter(aFunction);
79 public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
82 if (anObject instanceof TemplateModel)
83 return (TemplateModel) anObject;
84 else if (anObject instanceof Generator.GeneratorFunction)
85 return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
86 else if (anObject instanceof MessageResources)
87 return new MessageMethodModel((MessageResources) anObject);
88 else if (anObject instanceof Integer)
89 return makeStringAdapter(((Integer) anObject).toString());
90 else if (anObject instanceof String)
91 return makeStringAdapter((String) anObject);
92 else if (anObject instanceof Map)
93 return makeMapAdapter((Map) anObject);
94 else if (anObject instanceof Iterator)
95 return makeIteratorAdapter((Iterator) anObject);
96 else if (anObject instanceof List)
97 return makeIteratorAdapter(((List) anObject).iterator());
99 throw new TemplateModelException("Unadaptable class: " + anObject.getClass().getName());
102 private static class MapAdapter implements TemplateModelRoot {
106 private MapAdapter(Map aMap) {
108 valuesCache = new HashMap();
111 public void put(String aKey, TemplateModel aModel) {
112 valuesCache.put(aKey, aModel);
115 public void remove(String aKey) {
116 // ML: kinda tricky...
119 public boolean isEmpty() {
120 return map.isEmpty();
123 public TemplateModel get(String aKey) throws TemplateModelException {
125 if (!valuesCache.containsKey(aKey)) {
126 Object value = map.get(aKey);
128 if (value == null && !map.containsKey(aKey)) {
129 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
132 valuesCache.put(aKey, makeAdapter(value));
135 return (TemplateModel) valuesCache.get(aKey);
137 catch (TemplateModelException e) {
140 catch (Throwable t) {
141 throw new TemplateModelException(t.getMessage());
146 private static class IteratorAdapter implements TemplateListModel {
151 private IteratorAdapter(Iterator anIterator) {
152 iterator = anIterator;
154 valuesCache = new Vector();
158 if (iterator instanceof RewindableIterator) {
159 ((RewindableIterator) iterator).rewind();
163 public boolean isEmpty() {
164 return valuesCache.isEmpty() && !iterator.hasNext();
167 private void getUntil(int anIndex) throws TemplateModelException {
168 while (valuesCache.size()<=anIndex && iterator.hasNext())
170 valuesCache.add(makeAdapter(iterator.next()));
174 public TemplateModel get(int anIndex) throws TemplateModelException {
175 TemplateModel result;
179 if (anIndex<valuesCache.size())
181 result = (TemplateModel) valuesCache.get(anIndex);
186 throw new TemplateModelException( "Iterator out of bounds" );
189 public boolean hasNext() {
190 return position<valuesCache.size() || iterator.hasNext();
193 public boolean isRewound() {
197 public TemplateModel next() throws TemplateModelException {
198 TemplateModel result;
201 result = get(position);
205 throw new TemplateModelException( "Iterator out of bounds" );
210 public void rewind() {
215 private static class ListAdapter implements TemplateListModel {
220 private ListAdapter(List aList) {
222 valuesCache = new Vector();
226 public boolean isEmpty() {
227 return list.isEmpty();
230 public TemplateModel get(int i) throws TemplateModelException {
232 if (i>=valuesCache.size() && i<list.size()) {
233 for(int j=valuesCache.size(); j<=i; j++) {
234 valuesCache.add(makeAdapter(list.get(j)));
238 if (i<valuesCache.size())
239 return (TemplateModel) valuesCache.get(i);
241 throw new TemplateModelException( "Iterator out of bounds" );
244 public boolean hasNext() {
245 return position<list.size();
248 public boolean isRewound() {
252 public TemplateModel next() throws TemplateModelException {
253 TemplateModel result;
256 result = get(position);
260 throw new TemplateModelException( "Iterator out of bounds" );
266 public void rewind() {
271 private static class FunctionAdapter implements TemplateMethodModel {
272 Generator.GeneratorFunction function;
274 public FunctionAdapter(Generator.GeneratorFunction aFunction) {
275 function = aFunction;
278 public TemplateModel exec(List anArguments) throws TemplateModelException {
280 return makeAdapter(function.perform(anArguments));
282 catch (Throwable t) {
283 throw new TemplateModelException(t.getMessage());
287 public boolean isEmpty() {
293 public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
294 private FileTemplateCache templateCache;
296 public FreemarkerGeneratorLibrary(String aTemplateRoot) {
297 templateCache = new FileTemplateCache( aTemplateRoot+"/" );
298 templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
301 public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
302 Template template = (Template) templateCache.getItem(anIdentifier, "template");
304 if (template==null) {
305 throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+anIdentifier);
308 return new FreemarkerGenerator(template);
312 public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory {
313 private String basePath;
315 public FreemarkerGeneratorLibraryFactory(String aBasePath) {
316 basePath = aBasePath;
319 public GeneratorLibrary makeLibrary(String anInitializationString) {
320 return new FreemarkerGeneratorLibrary(basePath+anInitializationString);