5 import freemarker.template.*;
9 import org.apache.struts.util.MessageResources;
11 public class FreemarkerGenerator implements Generator {
12 private Template template;
14 public FreemarkerGenerator(Template aTemplate) {
18 public void generate(PrintWriter anOutputWriter, Map aValues, PrintWriter aLogger) throws GeneratorException {
19 aLogger.println("processing...<br/>");
20 template.process((TemplateModelRoot) makeMapAdapter(aValues), anOutputWriter);
21 aLogger.println("processed...<br/>");
24 private static TemplateScalarModel makeStringAdapter(String aString) {
25 return new SimpleScalar(aString);
28 private static TemplateHashModel makeMapAdapter(Map aMap) {
29 return new MapAdapter(aMap);
32 private static TemplateListModel makeIteratorAdapter(Iterator anIterator) {
33 return new IteratorAdapter(anIterator);
36 private static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
39 if (anObject instanceof TemplateModel)
40 return (TemplateModel) anObject;
41 else if (anObject instanceof MessageResources)
42 return new MessageMethodModel((MessageResources) anObject);
43 else if (anObject instanceof String)
44 return makeStringAdapter((String) anObject);
45 else if (anObject instanceof Map)
46 return makeMapAdapter((Map) anObject);
47 else if (anObject instanceof Iterator)
48 return makeIteratorAdapter((Iterator) anObject);
50 throw new TemplateModelException("Unadaptable class: " + anObject.getClass().getName());
53 private static class MapAdapter implements TemplateModelRoot {
57 private MapAdapter(Map aMap) {
59 valuesCache = new HashMap();
62 public void put(String aKey, TemplateModel aModel) {
63 valuesCache.put(aKey, aModel);
66 public void remove(String aKey) {
67 // ML: kinda tricky...
70 public boolean isEmpty() {
74 public TemplateModel get(String aKey) throws TemplateModelException {
75 if (!valuesCache.containsKey(aKey)) {
76 Object value = map.get(aKey);
79 throw new TemplateModelException("MapAdapter: no key "+aKey+" available");
81 valuesCache.put(aKey, makeAdapter(value));
84 return (TemplateModel) valuesCache.get(aKey);
88 private static class IteratorAdapter implements TemplateListModel {
93 private IteratorAdapter(Iterator anIterator) {
94 iterator = anIterator;
96 valuesCache = new Vector();
100 if (iterator instanceof RewindableIterator) {
101 ((RewindableIterator) iterator).rewind();
105 public boolean isEmpty() {
106 return valuesCache.isEmpty() && !iterator.hasNext();
109 private void getUntil(int anIndex) throws TemplateModelException {
110 while (valuesCache.size()<=anIndex && iterator.hasNext())
112 valuesCache.add(makeAdapter(iterator.next()));
116 public TemplateModel get(int anIndex) throws TemplateModelException {
117 TemplateModel result;
121 if (anIndex<valuesCache.size())
123 result = (TemplateModel) valuesCache.get(anIndex);
128 throw new TemplateModelException( "Iterator out of bounds" );
131 public boolean hasNext() {
132 return position<valuesCache.size() || iterator.hasNext();
135 public boolean isRewound() {
139 public TemplateModel next() throws TemplateModelException {
140 TemplateModel result;
143 result = get(position);
147 throw new TemplateModelException( "Iterator out of bounds" );
152 public void rewind() {
157 private static class ListAdapter implements TemplateListModel {
162 private ListAdapter(List aList) {
164 valuesCache = new Vector();
168 public boolean isEmpty() {
169 return list.isEmpty();
172 public TemplateModel get(int i) throws TemplateModelException {
174 if (i>=valuesCache.size() && i<list.size()) {
175 for(int j=valuesCache.size(); j<=i; j++) {
176 valuesCache.add(makeAdapter(list.get(j)));
180 if (i<valuesCache.size())
181 return (TemplateModel) valuesCache.get(i);
183 throw new TemplateModelException( "Iterator out of bounds" );
186 public boolean hasNext() {
187 return position<list.size();
190 public boolean isRewound() {
194 public TemplateModel next() throws TemplateModelException {
195 TemplateModel result;
198 result = get(position);
202 throw new TemplateModelException( "Iterator out of bounds" );
207 public void rewind() {