X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fgenerator%2FFreemarkerGenerator.java;h=c0ebb764cf7e6c0a77ea73c63c22a85696c79f5b;hb=29da699109ce8842d02b60abcdb0dfdc4aa4f0db;hp=8dc0a6713418a277fe13c95d5acbc19fe2d6dd03;hpb=855a59e03d05068abbd2d1e4f3256908269e518e;p=mir.git diff --git a/source/mir/generator/FreemarkerGenerator.java b/source/mir/generator/FreemarkerGenerator.java index 8dc0a671..c0ebb764 100755 --- a/source/mir/generator/FreemarkerGenerator.java +++ b/source/mir/generator/FreemarkerGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,26 +18,43 @@ * 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; @@ -46,7 +63,7 @@ public class FreemarkerGenerator implements Generator { 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"); @@ -54,8 +71,9 @@ public class FreemarkerGenerator implements Generator { 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 ); } } @@ -76,17 +94,26 @@ public class FreemarkerGenerator implements Generator { 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) @@ -95,13 +122,17 @@ public class FreemarkerGenerator implements Generator { 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; @@ -113,7 +144,6 @@ public class FreemarkerGenerator implements Generator { } public void remove(String aKey) { - // ML: kinda tricky... } public boolean isEmpty() { @@ -122,30 +152,31 @@ public class FreemarkerGenerator implements Generator { public TemplateModel get(String aKey) throws TemplateModelException { try { - if (!valuesCache.containsKey(aKey)) { - Object value = map.get(aKey); + if (!valuesCache.containsKey(aKey)) { + Object value = map.get(aKey); - if (value == null && !map.containsKey(aKey)) - throw new TemplateModelException("MapAdapter: no key "+aKey+" available"); + if (value == null && !map.containsKey(aKey)) { + throw new TemplateModelException("MapAdapter: no key "+aKey+" available"); + } - valuesCache.put(aKey, makeAdapter(value)); - } + valuesCache.put(aKey, makeAdapter(value)); + } - return (TemplateModel) valuesCache.get(aKey); - } - catch (TemplateModelException e) { - throw e; - } - catch (Throwable t) { - throw new TemplateModelException(t.getMessage()); - } + return (TemplateModel) valuesCache.get(aKey); + } + catch (TemplateModelException e) { + throw e; + } + catch (Throwable t) { + throw new TemplateModelException(t.getMessage()); + } } } 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; @@ -268,7 +299,7 @@ public class FreemarkerGenerator implements Generator { } private static class FunctionAdapter implements TemplateMethodModel { - Generator.GeneratorFunction function; + private Generator.GeneratorFunction function; public FunctionAdapter(Generator.GeneratorFunction aFunction) { function = aFunction; @@ -289,22 +320,66 @@ public class FreemarkerGenerator implements Generator { } + 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; + private FileTemplateCache templateCache; public FreemarkerGeneratorLibrary(String aTemplateRoot) { - templateCache = new FileTemplateCache( aTemplateRoot + "/" ); - templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND); + templateCache = new FileTemplateCache( aTemplateRoot+"/" ); + templateCache.setLoadingPolicy(FileTemplateCache.LOAD_ON_DEMAND); } public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure { Template template = (Template) templateCache.getItem(anIdentifier, "template"); if (template==null) { - throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+anIdentifier); + throw new GeneratorExc("FreemarkerGeneratorLibrary: Can't find template "+templateCache.getDirectory()+"/"+anIdentifier); } return new FreemarkerGenerator(template); } } + + public static class FreemarkerGeneratorLibraryFactory implements GeneratorLibraryFactory { + private String basePath; + + public FreemarkerGeneratorLibraryFactory(String aBasePath) { + basePath = aBasePath; + } + + public GeneratorLibrary makeLibrary(String anInitializationString) { + return new FreemarkerGeneratorLibrary(basePath+anInitializationString); + }; + } }