X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fgenerator%2FFreemarkerGenerator.java;h=a786a71224fcf16761e1dac8c5f28de22ad7188a;hb=a9ea831fc6b187596b6efcfc79818bf86e557ca9;hp=8b5ec61d95a387b5b5da65a5d422cbabfddbb2e0;hpb=165de4ed6c5babe44fb5f11ac93cde442cde55e4;p=mir.git diff --git a/source/mir/generator/FreemarkerGenerator.java b/source/mir/generator/FreemarkerGenerator.java index 8b5ec61d..a786a712 100755 --- a/source/mir/generator/FreemarkerGenerator.java +++ b/source/mir/generator/FreemarkerGenerator.java @@ -1,383 +1,393 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * 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 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 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 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; -import mir.log.LoggerWrapper; -import mir.util.GeneratorFormatAdapters; -import mir.util.RewindableIterator; - - -public class FreemarkerGenerator implements Generator { - private Template template; - - public FreemarkerGenerator(Template aTemplate) { - template = aTemplate; - } - - 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"); - - try { - template.process((TemplateModelRoot) makeMapAdapter(aValues), (PrintWriter) anOutputWriter); - } - catch (Throwable t) { - t.printStackTrace(); - aLogger.error("Exception occurred: "+t.getMessage()); - t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE)); - throw new GeneratorFailure( t ); - } - } - - private static TemplateScalarModel makeStringAdapter(String aString) { - return new SimpleScalar(aString); - } - - private static TemplateHashModel makeMapAdapter(Map aMap) { - return new MapAdapter(aMap); - } - - private static TemplateListModel makeIteratorAdapter(Iterator anIterator) { - return new IteratorAdapter(anIterator); - } - - private static TemplateMethodModel makeFunctionAdapter(Generator.GeneratorFunction aFunction) { - 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 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 makeMapAdapter((Map) anObject); - else if (anObject instanceof Iterator) - 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 - return makeBeanAdapter(anObject); - } - - private static class MapAdapter implements TemplateModelRoot { - private Map map; - private Map valuesCache; - - private MapAdapter(Map aMap) { - map = aMap; - valuesCache = new HashMap(); - } - - public void put(String aKey, TemplateModel aModel) { - valuesCache.put(aKey, aModel); - } - - public void remove(String aKey) { - } - - public boolean isEmpty() { - return map.isEmpty(); - } - - public TemplateModel get(String aKey) throws TemplateModelException { - try { - if (!valuesCache.containsKey(aKey)) { - Object value = map.get(aKey); - - if (value == null && !map.containsKey(aKey)) { - throw new TemplateModelException("MapAdapter: no key "+aKey+" available"); - } - - valuesCache.put(aKey, makeAdapter(value)); - } - - return (TemplateModel) valuesCache.get(aKey); - } - catch (TemplateModelException e) { - throw e; - } - catch (Throwable t) { - throw new TemplateModelException(t.getMessage()); - } - } - } - - private static class IteratorAdapter implements TemplateListModel { - private Iterator iterator; - private List valuesCache; - private int position; - - private IteratorAdapter(Iterator anIterator) { - iterator = anIterator; - - valuesCache = new Vector(); - position=0; - - - if (iterator instanceof RewindableIterator) { - ((RewindableIterator) iterator).rewind(); - } - } - - public boolean isEmpty() { - return valuesCache.isEmpty() && !iterator.hasNext(); - } - - private void getUntil(int anIndex) throws TemplateModelException { - while (valuesCache.size()<=anIndex && iterator.hasNext()) - { - valuesCache.add(makeAdapter(iterator.next())); - } - }; - - public TemplateModel get(int anIndex) throws TemplateModelException { - TemplateModel result; - - getUntil(anIndex); - - if (anIndex=valuesCache.size() && i=valuesCache.size() && i