/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
*
* This file is part of Mir.
*
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two. You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries. If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so. If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
*/
-
package mir.generator;
-import freemarker.template.*;
-import org.apache.struts.util.MessageResources;
-import java.util.*;
-import java.io.*;
-import mir.entity.*;
-import mir.util.*;
-import mir.misc.*;
+import java.io.PrintWriter;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+
+import mir.log.LoggerWrapper;
+import mir.util.GeneratorFormatAdapters;
+import mir.util.RewindableIterator;
+
+import org.apache.commons.beanutils.MethodUtils;
+import org.apache.commons.beanutils.PropertyUtils;
+
+import freemarker.template.FileTemplateCache;
+import freemarker.template.SimpleScalar;
+import freemarker.template.Template;
+import freemarker.template.TemplateHashModel;
+import freemarker.template.TemplateListModel;
+import freemarker.template.TemplateMethodModel;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+import freemarker.template.TemplateModelRoot;
+import freemarker.template.TemplateScalarModel;
+
public class FreemarkerGenerator implements Generator {
private Template template;
template = aTemplate;
}
- public void generate(Object anOutputWriter, Map aValues, PrintWriter aLogger) throws GeneratorExc, GeneratorFailure {
+ public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
if (!(anOutputWriter instanceof PrintWriter))
throw new GeneratorExc("Writer for a FreemarkerGenerator must be a PrintWriter");
template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter);
}
catch (Throwable t) {
- aLogger.println("Exception occurred: "+t.getMessage());
- t.printStackTrace(aLogger);
+ t.printStackTrace();
+ aLogger.error("Exception occurred: "+t.getMessage());
+ t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
throw new GeneratorFailure( t );
}
}
return new FunctionAdapter(aFunction);
}
+ private static TemplateHashModel makeBeanAdapter(Object anObject) {
+ return new BeanAdapter(anObject);
+ }
+
public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
if (anObject == null)
return null;
+
if (anObject instanceof TemplateModel)
return (TemplateModel) anObject;
else if (anObject instanceof Generator.GeneratorFunction)
return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
- else if (anObject instanceof MessageResources)
- return new MessageMethodModel((MessageResources) anObject);
else if (anObject instanceof Integer)
return makeStringAdapter(((Integer) anObject).toString());
+ else if (anObject instanceof Boolean) {
+ if (((Boolean) anObject).booleanValue())
+ return makeStringAdapter("1");
+ else
+ return makeStringAdapter("0");
+ }
else if (anObject instanceof String)
return makeStringAdapter((String) anObject);
else if (anObject instanceof Map)
return makeIteratorAdapter((Iterator) anObject);
else if (anObject instanceof List)
return makeIteratorAdapter(((List) anObject).iterator());
+ else if (anObject instanceof Number)
+ return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));
+ else if (anObject instanceof Date)
+ return makeAdapter(new GeneratorFormatAdapters.DateFormatAdapter((Date) anObject));
else
- throw new TemplateModelException("Unadaptable class: " + anObject.getClass().getName());
+ return makeBeanAdapter(anObject);
}
private static class MapAdapter implements TemplateModelRoot {
- Map map;
- Map valuesCache;
+ private Map map;
+ private Map valuesCache;
private MapAdapter(Map aMap) {
map = aMap;
}
public void remove(String aKey) {
- // ML: kinda tricky...
}
public boolean isEmpty() {
}
private static class IteratorAdapter implements TemplateListModel {
- Iterator iterator;
- List valuesCache;
- int position;
+ private Iterator iterator;
+ private List valuesCache;
+ private int position;
private IteratorAdapter(Iterator anIterator) {
iterator = anIterator;
}
private static class FunctionAdapter implements TemplateMethodModel {
- Generator.GeneratorFunction function;
+ private Generator.GeneratorFunction function;
public FunctionAdapter(Generator.GeneratorFunction aFunction) {
function = aFunction;
}
+ private static class BeanAdapter implements TemplateHashModel {
+ private Object object;
+
+ public BeanAdapter(Object anObject) {
+ object = anObject;
+ }
+
+ public void put(String aKey, TemplateModel aModel) throws TemplateModelException {
+ throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.put not supported");
+ }
+
+ public void remove(String aKey) throws TemplateModelException {
+ throw new TemplateModelException("FreemarkerGenerator$BeanAdapter.remove not supported");
+ }
+
+ public boolean isEmpty() {
+ return false;
+ }
+
+ public TemplateModel get(String aKey) throws TemplateModelException {
+ try {
+ if (PropertyUtils.isReadable(object, aKey))
+ return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
+ else
+ return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));
+ }
+ catch (Throwable t) {
+ throw new TemplateModelException(t.getMessage());
+ }
+ }
+ }
+
public static class FreemarkerGeneratorLibrary implements GeneratorLibrary {
private FileTemplateCache templateCache;
public FreemarkerGeneratorLibrary(String aTemplateRoot) {
templateCache = new FileTemplateCache( aTemplateRoot+"/" );
- templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
+ templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND);
}
public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {