Added features:
[mir.git] / source / mir / generator / VelocityGenerator.java
index e827cc5..a6c0a77 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with  any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two.  You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries.  If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-package mir.generator;\r
-\r
-import java.io.PrintWriter;\r
-import java.io.StringWriter;\r
-import java.util.AbstractList;\r
-import java.util.Date;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-\r
-import org.apache.commons.beanutils.MethodUtils;\r
-import org.apache.commons.beanutils.PropertyUtils;\r
-import org.apache.velocity.Template;\r
-import org.apache.velocity.VelocityContext;\r
-import org.apache.velocity.app.VelocityEngine;\r
-import org.apache.velocity.context.Context;\r
-import org.apache.velocity.exception.ParseErrorException;\r
-import org.apache.velocity.exception.ResourceNotFoundException;\r
-import org.apache.velocity.runtime.RuntimeServices;\r
-import org.apache.velocity.runtime.log.LogSystem;\r
-import mir.log.LoggerWrapper;\r
-import mir.util.GeneratorFormatAdapters;\r
-import mir.util.RewindableIterator;\r
-\r
-public class VelocityGenerator implements Generator {\r
-  private String templateIdentifier;\r
-  private VelocityGeneratorLibrary library;\r
-  private static LoggerWrapper logger = new LoggerWrapper("Generator.velocity");\r
-\r
-  /**\r
-   *\r
-   * @param aTemplate\r
-   * @param aLibrary\r
-   */\r
-\r
-  public VelocityGenerator(String aTemplate, VelocityGeneratorLibrary aLibrary) {\r
-    templateIdentifier = aTemplate;\r
-    library = aLibrary;\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param anOutputWriter\r
-   * @param aValues\r
-   * @param aLogger\r
-   * @throws GeneratorExc\r
-   * @throws GeneratorFailure\r
-   */\r
-  public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {\r
-    Template template;\r
-    Context context = makeMapAdapter(aValues);\r
-    StringWriter stringWriter = new StringWriter();\r
-\r
-    try {\r
-      template = library.engine.getTemplate(templateIdentifier);\r
-      if (template == null) {\r
-        throw new GeneratorExc("VelocityGeneratorLibrary: Can't find template " + templateIdentifier);\r
-      }\r
-      template.merge(context, stringWriter);\r
-\r
-      ( (PrintWriter) anOutputWriter).print(stringWriter.toString());\r
-    }\r
-    catch (ResourceNotFoundException t) {\r
-      throw new GeneratorExc("VelocityGeneratorLibrary: Can't find template " + templateIdentifier);\r
-    }\r
-    catch (ParseErrorException t) {\r
-      ( (PrintWriter) anOutputWriter).print(t.toString());\r
-    }\r
-    catch (Throwable t) {\r
-      throw new GeneratorFailure(t);\r
-    }\r
-\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class ContextAdapter implements Context {\r
-    public boolean containsKey(java.lang.Object key) {\r
-      return false;\r
-    }\r
-\r
-    public Object get(java.lang.String key) {\r
-      return null;\r
-    }\r
-\r
-    public Object[] getKeys() {\r
-      return new Object[] {};\r
-    }\r
-\r
-    public Object put(java.lang.String key, java.lang.Object value) {\r
-      return null;\r
-    }\r
-\r
-    public Object remove(java.lang.Object key) {\r
-      return null;\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param aMap\r
-   * @return\r
-   */\r
-  private static Context makeMapAdapter(Map aMap)  {\r
-    return new MapAdapter(aMap);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param anIterator\r
-   * @return\r
-   */\r
-  private static List makeIteratorAdapter(Iterator anIterator) {\r
-    return new IteratorAdapter(anIterator);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param aList\r
-   * @return\r
-   */\r
-  private static List makeListAdapter(List aList) {\r
-    return new ListAdapter(aList);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param aFunction\r
-   * @return\r
-   */\r
-  private static Object makeFunctionAdapter(Generator.GeneratorFunction aFunction) {\r
-    return new FunctionAdapter(aFunction);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param anObject\r
-   * @return\r
-   */\r
-  private static Object makeBeanAdapter(Object anObject)  {\r
-    return new BeanAdapter(anObject);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private interface VelocityAdapter {\r
-    public Object getOriginal();\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param anObject\r
-   * @return\r
-   */\r
-  public static Object unmakeAdapter(Object anObject) {\r
-    if (anObject instanceof VelocityAdapter) {\r
-      return ((VelocityAdapter) anObject).getOriginal();\r
-    }\r
-    else\r
-      return anObject;\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param anObject\r
-   * @return\r
-   */\r
-  public static Object makeAdapter(Object anObject) {\r
-    if (anObject == null)\r
-      return null;\r
-\r
-    if (anObject instanceof Context)\r
-      return anObject;\r
-\r
-    else if (anObject instanceof Generator.GeneratorFunction)\r
-      return makeFunctionAdapter((Generator.GeneratorFunction) anObject);\r
-    else if (anObject instanceof Integer)\r
-      return anObject;\r
-    else if (anObject instanceof Boolean)\r
-      return anObject;\r
-    else if (anObject instanceof String)\r
-      return anObject;\r
-    else if (anObject instanceof Map)\r
-      return makeMapAdapter((Map) anObject);\r
-    else if (anObject instanceof Iterator)\r
-      return makeIteratorAdapter((Iterator) anObject);\r
-    else if (anObject instanceof List)\r
-      return makeListAdapter(((List) anObject));\r
-    else if (anObject instanceof Number)\r
-      return makeAdapter(new GeneratorFormatAdapters.NumberFormatAdapter((Number) anObject));\r
-    else if (anObject instanceof Date)\r
-      return makeAdapter(new GeneratorFormatAdapters.DateFormatAdapter((Date) anObject));\r
-    else\r
-      return makeBeanAdapter(anObject);\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  public static class FunctionAdapter implements VelocityAdapter {\r
-    private GeneratorFunction function;\r
-\r
-    public Object getOriginal() {\r
-      return function;\r
-    }\r
-\r
-    private FunctionAdapter(GeneratorFunction aFunction) {\r
-      function = aFunction;\r
-    }\r
-\r
-    public Object call(Object aParameters[]) throws GeneratorExc {\r
-      List parameters = new Vector();\r
-\r
-      for (int i = 0; i<aParameters.length; i++) {\r
-        parameters.add(unmakeAdapter(aParameters[i]));\r
-      }\r
-\r
-      return makeAdapter(function.perform(parameters));\r
-    }\r
-\r
-    public Object call() throws GeneratorExc {\r
-      return makeAdapter(function.perform(new Vector()));\r
-    }\r
-\r
-    public Object call(Object anObject) throws GeneratorExc {\r
-      return call(new Object[] { anObject });\r
-    }\r
-\r
-    public Object call(Object anObject1, Object anObject2) throws GeneratorExc {\r
-      return call(new Object[] { anObject1, anObject2 });\r
-    }\r
-\r
-    public Object call(Object anObject1, Object anObject2, Object anObject3) throws GeneratorExc {\r
-      return call(new Object[] { anObject1, anObject2, anObject3 });\r
-    }\r
-\r
-    public Object call(Object anObject1, Object anObject2, Object anObject3, Object anObject4) throws GeneratorExc {\r
-      return call(new Object[] { anObject1, anObject2, anObject3, anObject4 });\r
-    }\r
-  }\r
-\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class MapAdapter implements Context, VelocityAdapter  {\r
-    private Map map;\r
-    private Map valuesCache;\r
-\r
-    private MapAdapter(Map aMap) {\r
-      map = aMap;\r
-      valuesCache = new HashMap();\r
-    }\r
-\r
-    public Object getOriginal() {\r
-      return map;\r
-    }\r
-\r
-    public boolean containsKey(Object aKey) {\r
-      return map.containsKey(aKey);\r
-    }\r
-\r
-    public Object get(String aKey) {\r
-      try {\r
-        if (!valuesCache.containsKey(aKey)) {\r
-          Object value = map.get(aKey);\r
-\r
-          if (value == null && !map.containsKey(aKey)) {\r
-            return "no key "+aKey+" available";\r
-          }\r
-          else\r
-            valuesCache.put(aKey, makeAdapter(value));\r
-        }\r
-\r
-        return valuesCache.get(aKey);\r
-      }\r
-      catch (Throwable t) {\r
-        throw new GeneratorFailure(t);\r
-      }\r
-    }\r
-\r
-    public Object[] getKeys() {\r
-      return new Object[] {};\r
-    }\r
-\r
-    public Object put(String aKey, Object aValue) {\r
-      valuesCache.remove(aKey);\r
-      map.put(aKey, unmakeAdapter(aValue));\r
-\r
-      return aValue;\r
-    }\r
-\r
-    public Object remove(java.lang.Object key) {\r
-      return null;\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class IteratorAdapter extends AbstractList implements VelocityAdapter  {\r
-    private Iterator iterator;\r
-    private List valuesCache;\r
-    private int position;\r
-\r
-    private IteratorAdapter(Iterator anIterator) {\r
-      iterator = anIterator;\r
-\r
-      valuesCache = new Vector();\r
-      position=0;\r
-\r
-\r
-      if (iterator instanceof RewindableIterator) {\r
-        ((RewindableIterator) iterator).rewind();\r
-      }\r
-    }\r
-\r
-    private void getUntil(int anIndex) {\r
-      while ((anIndex==-1 || valuesCache.size()<=anIndex) && iterator.hasNext())\r
-      {\r
-        valuesCache.add(makeAdapter(iterator.next()));\r
-      }\r
-    };\r
-\r
-    public Object getOriginal() {\r
-      return iterator;\r
-    }\r
-\r
-    public Object get(int anIndex) {\r
-      Object result;\r
-\r
-      getUntil(anIndex);\r
-\r
-      if (anIndex<valuesCache.size())\r
-      {\r
-        result = valuesCache.get(anIndex);\r
-\r
-        return result;\r
-      }\r
-      else\r
-        throw new RuntimeException( "Iterator out of bounds" );\r
-    }\r
-\r
-    public int size() {\r
-      getUntil(-1);\r
-      return valuesCache.size();\r
-    }\r
-\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class ListAdapter extends AbstractList implements VelocityAdapter  {\r
-    private List list;\r
-    private List valuesCache;\r
-    private int position;\r
-\r
-    private ListAdapter(List aList) {\r
-      list = aList;\r
-\r
-      valuesCache = new Vector();\r
-      position=0;\r
-    }\r
-\r
-    private void getUntil(int anIndex) {\r
-      while ((anIndex==-1 || valuesCache.size()<=anIndex) && valuesCache.size()<list.size())\r
-      {\r
-        valuesCache.add(makeAdapter(list.get(valuesCache.size())));\r
-      }\r
-    };\r
-\r
-    public Object getOriginal() {\r
-      return list;\r
-    }\r
-\r
-    public Object get(int anIndex) {\r
-      Object result;\r
-\r
-      getUntil(anIndex);\r
-\r
-      if (anIndex<valuesCache.size())\r
-      {\r
-        result = valuesCache.get(anIndex);\r
-\r
-        return result;\r
-      }\r
-      else\r
-        throw new RuntimeException( "Iterator out of bounds" );\r
-    }\r
-\r
-    public int size() {\r
-      return list.size();\r
-    }\r
-\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class BeanAdapter implements Context, VelocityAdapter {\r
-    private Object object;\r
-\r
-    public BeanAdapter(Object anObject) {\r
-      object = anObject;\r
-    }\r
-\r
-    public boolean containsKey(Object key) {\r
-      return true;\r
-    }\r
-\r
-    public Object getOriginal() {\r
-      return object;\r
-    }\r
-\r
-    public Object get(String aKey) {\r
-      try {\r
-        if (PropertyUtils.isReadable(object, aKey))\r
-          return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));\r
-        else\r
-          return makeAdapter(MethodUtils.invokeExactMethod(object, "get", aKey));\r
-      }\r
-      catch (Throwable t) {\r
-        throw new GeneratorFailure(t);\r
-      }\r
-    }\r
-\r
-    public Object[] getKeys() {\r
-      return new Object[] {};\r
-    }\r
-\r
-    public Object put(String aKey, Object aValue) {\r
-      try {\r
-        if (PropertyUtils.isWriteable(object, aKey))\r
-          PropertyUtils.setSimpleProperty(object, aKey, unmakeAdapter(aValue));\r
-        else\r
-          MethodUtils.invokeExactMethod(object, "set", new Object[] {aKey, unmakeAdapter(aValue)});\r
-\r
-        return this;\r
-      }\r
-      catch (Throwable t) {\r
-        throw new GeneratorFailure(t);\r
-      }\r
-    }\r
-\r
-    public Object remove(Object aKey) {\r
-      throw new RuntimeException("BeanAdapter.remove not supported");\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  private static class VelocityLoggerWrapper implements LogSystem {\r
-    private LoggerWrapper logger;\r
-\r
-    public VelocityLoggerWrapper(LoggerWrapper aLogger) {\r
-      logger = aLogger;\r
-    }\r
-\r
-    public void init(RuntimeServices aRuntimeServices) {\r
-    }\r
-\r
-    public void logVelocityMessage(int aLevel, String aMessage) {\r
-      switch (aLevel) {\r
-        case DEBUG_ID:\r
-          logger.debug(aMessage);\r
-          break;\r
-        case ERROR_ID:\r
-          logger.error(aMessage);\r
-          break;\r
-        case INFO_ID:\r
-          logger.info(aMessage);\r
-          break;\r
-        default:\r
-          logger.warn(aMessage);\r
-          break;\r
-      }\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  public static class VelocityGeneratorLibrary implements GeneratorLibrary {\r
-    private VelocityEngine engine;\r
-\r
-    public VelocityGeneratorLibrary(String aTemplateRoot) throws GeneratorExc, GeneratorFailure {\r
-      try {\r
-        engine = new VelocityEngine();\r
-        try {\r
-          engine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error(VelocityEngine.RESOURCE_LOADER);\r
-        }\r
-\r
-        try {\r
-          engine.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error("file.resource.loader.class");\r
-        }\r
-\r
-        try {\r
-          engine.setProperty("file.resource.loader.path", aTemplateRoot);\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error("file.resource.loader.path");\r
-\r
-        }\r
-        try {\r
-          engine.setProperty("file.resource.loader.cache", "true");\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error("file.resource.loader.cache");\r
-\r
-        }\r
-        try {\r
-          engine.setProperty("file.resource.loader.modificationCheckInterval", "10");\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error("file.resource.loader.modificationCheckInterval");\r
-\r
-        }\r
-\r
-        try {\r
-          engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new VelocityLoggerWrapper(logger));\r
-        }\r
-        catch (Throwable t) {\r
-          logger.error(VelocityEngine.RUNTIME_LOG_LOGSYSTEM);\r
-\r
-        }\r
-        engine.init();\r
-      }\r
-      catch (Throwable t) {\r
-        t.printStackTrace(logger.asPrintWriter(logger.ERROR_MESSAGE));\r
-\r
-        logger.error("Failed to set up a VelocityGeneratorLibrary: " + t.toString());\r
-        throw new GeneratorFailure(t);\r
-      }\r
-    }\r
-\r
-    public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {\r
-      return new VelocityGenerator(anIdentifier, this);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * <p>Title: </p>\r
-   * <p>Description: </p>\r
-   * <p>Copyright: Copyright (c) 2003</p>\r
-   * <p>Company: </p>\r
-   * @author not attributable\r
-   * @version 1.0\r
-   */\r
-  public static class VelocityGeneratorLibraryFactory implements GeneratorLibraryFactory {\r
-    private String basePath;\r
-\r
-    public VelocityGeneratorLibraryFactory(String aBasePath) {\r
-      basePath = aBasePath;\r
-    }\r
-\r
-    public GeneratorLibrary makeLibrary(String anInitializationString) throws GeneratorExc, GeneratorFailure {\r
-      return new VelocityGeneratorLibrary(basePath+anInitializationString);\r
-    };\r
-  }\r
-}\r
+/*
+ * 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.io.StringWriter;
+import java.util.AbstractList;
+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 org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.log.LogSystem;
+import mir.log.LoggerWrapper;
+import mir.util.GeneratorFormatAdapters;
+import mir.util.RewindableIterator;
+
+public class VelocityGenerator implements Generator {
+  private String templateIdentifier;
+  private VelocityGeneratorLibrary library;
+  private static LoggerWrapper logger = new LoggerWrapper("Generator.velocity");
+
+  /**
+   *
+   * @param aTemplate
+   * @param aLibrary
+   */
+
+  public VelocityGenerator(String aTemplate, VelocityGeneratorLibrary aLibrary) {
+    templateIdentifier = aTemplate;
+    library = aLibrary;
+  }
+
+  /**
+   *
+   * @param anOutputWriter
+   * @param aValues
+   * @param aLogger
+   * @throws GeneratorExc
+   * @throws GeneratorFailure
+   */
+  public void generate(Object anOutputWriter, Map aValues, LoggerWrapper aLogger) throws GeneratorExc, GeneratorFailure {
+    Template template;
+    Context context = makeMapAdapter(aValues);
+    StringWriter stringWriter = new StringWriter();
+
+    try {
+      template = library.engine.getTemplate(templateIdentifier);
+      if (template == null) {
+        throw new GeneratorExc("VelocityGeneratorLibrary: Can't find template " + templateIdentifier);
+      }
+      template.merge(context, stringWriter);
+
+      ( (PrintWriter) anOutputWriter).print(stringWriter.toString());
+    }
+    catch (ResourceNotFoundException t) {
+      throw new GeneratorExc("VelocityGeneratorLibrary: Can't find template " + templateIdentifier);
+    }
+    catch (ParseErrorException t) {
+      ( (PrintWriter) anOutputWriter).print(t.toString());
+    }
+    catch (Throwable t) {
+      throw new GeneratorFailure(t);
+    }
+
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class ContextAdapter implements Context {
+    public boolean containsKey(java.lang.Object key) {
+      return false;
+    }
+
+    public Object get(java.lang.String key) {
+      return null;
+    }
+
+    public Object[] getKeys() {
+      return new Object[] {};
+    }
+
+    public Object put(java.lang.String key, java.lang.Object value) {
+      return null;
+    }
+
+    public Object remove(java.lang.Object key) {
+      return null;
+    }
+  }
+
+  /**
+   *
+   * @param aMap
+   * @return
+   */
+  private static Context makeMapAdapter(Map aMap)  {
+    return new MapAdapter(aMap);
+  }
+
+  /**
+   *
+   * @param anIterator
+   * @return
+   */
+  private static List makeIteratorAdapter(Iterator anIterator) {
+    return new IteratorAdapter(anIterator);
+  }
+
+  /**
+   *
+   * @param aList
+   * @return
+   */
+  private static List makeListAdapter(List aList) {
+    return new ListAdapter(aList);
+  }
+
+  /**
+   *
+   * @param aFunction
+   * @return
+   */
+  private static Object makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
+    return new FunctionAdapter(aFunction);
+  }
+
+  /**
+   *
+   * @param anObject
+   * @return
+   */
+  private static Object makeBeanAdapter(Object anObject)  {
+    return new BeanAdapter(anObject);
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private interface VelocityAdapter {
+    public Object getOriginal();
+  }
+
+  /**
+   *
+   * @param anObject
+   * @return
+   */
+  public static Object unmakeAdapter(Object anObject) {
+    if (anObject instanceof VelocityAdapter) {
+      return ((VelocityAdapter) anObject).getOriginal();
+    }
+    else
+      return anObject;
+  }
+
+  /**
+   *
+   * @param anObject
+   * @return
+   */
+  public static Object makeAdapter(Object anObject) {
+    if (anObject == null)
+      return null;
+
+    if (anObject instanceof Context)
+      return anObject;
+
+    else if (anObject instanceof Generator.GeneratorFunction)
+      return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
+    else if (anObject instanceof Integer)
+      return anObject;
+    else if (anObject instanceof Boolean)
+      return anObject;
+    else if (anObject instanceof String)
+      return 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 makeListAdapter(((List) anObject));
+    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);
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  public static class FunctionAdapter implements VelocityAdapter {
+    private GeneratorFunction function;
+
+    public Object getOriginal() {
+      return function;
+    }
+
+    private FunctionAdapter(GeneratorFunction aFunction) {
+      function = aFunction;
+    }
+
+    public Object call(Object aParameters[]) throws GeneratorExc {
+      List parameters = new Vector();
+
+      for (int i = 0; i<aParameters.length; i++) {
+        parameters.add(unmakeAdapter(aParameters[i]));
+      }
+
+      return makeAdapter(function.perform(parameters));
+    }
+
+    public Object call() throws GeneratorExc {
+      return makeAdapter(function.perform(new Vector()));
+    }
+
+    public Object call(Object anObject) throws GeneratorExc {
+      return call(new Object[] { anObject });
+    }
+
+    public Object call(Object anObject1, Object anObject2) throws GeneratorExc {
+      return call(new Object[] { anObject1, anObject2 });
+    }
+
+    public Object call(Object anObject1, Object anObject2, Object anObject3) throws GeneratorExc {
+      return call(new Object[] { anObject1, anObject2, anObject3 });
+    }
+
+    public Object call(Object anObject1, Object anObject2, Object anObject3, Object anObject4) throws GeneratorExc {
+      return call(new Object[] { anObject1, anObject2, anObject3, anObject4 });
+    }
+  }
+
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class MapAdapter implements Context, VelocityAdapter  {
+    private Map map;
+    private Map valuesCache;
+
+    private MapAdapter(Map aMap) {
+      map = aMap;
+      valuesCache = new HashMap();
+    }
+
+    public Object getOriginal() {
+      return map;
+    }
+
+    public boolean containsKey(Object aKey) {
+      return map.containsKey(aKey);
+    }
+
+    public Object get(String aKey) {
+      try {
+        if (!valuesCache.containsKey(aKey)) {
+          Object value = map.get(aKey);
+
+          if (value == null && !map.containsKey(aKey)) {
+            return "no key "+aKey+" available";
+          }
+          else
+            valuesCache.put(aKey, makeAdapter(value));
+        }
+
+        return valuesCache.get(aKey);
+      }
+      catch (Throwable t) {
+        throw new GeneratorFailure(t);
+      }
+    }
+
+    public Object[] getKeys() {
+      return new Object[] {};
+    }
+
+    public Object put(String aKey, Object aValue) {
+      valuesCache.remove(aKey);
+      map.put(aKey, unmakeAdapter(aValue));
+
+      return aValue;
+    }
+
+    public Object remove(java.lang.Object key) {
+      return null;
+    }
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class IteratorAdapter extends AbstractList implements VelocityAdapter  {
+    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();
+      }
+    }
+
+    private void getUntil(int anIndex) {
+      while ((anIndex==-1 || valuesCache.size()<=anIndex) && iterator.hasNext())
+      {
+        valuesCache.add(makeAdapter(iterator.next()));
+      }
+    };
+
+    public Object getOriginal() {
+      return iterator;
+    }
+
+    public Object get(int anIndex) {
+      Object result;
+
+      getUntil(anIndex);
+
+      if (anIndex<valuesCache.size())
+      {
+        result = valuesCache.get(anIndex);
+
+        return result;
+      }
+      else
+        throw new RuntimeException( "Iterator out of bounds" );
+    }
+
+    public int size() {
+      getUntil(-1);
+      return valuesCache.size();
+    }
+
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class ListAdapter extends AbstractList implements VelocityAdapter  {
+    private List list;
+    private List valuesCache;
+    private int position;
+
+    private ListAdapter(List aList) {
+      list = aList;
+
+      valuesCache = new Vector();
+      position=0;
+    }
+
+    private void getUntil(int anIndex) {
+      while ((anIndex==-1 || valuesCache.size()<=anIndex) && valuesCache.size()<list.size())
+      {
+        valuesCache.add(makeAdapter(list.get(valuesCache.size())));
+      }
+    };
+
+    public Object getOriginal() {
+      return list;
+    }
+
+    public Object get(int anIndex) {
+      Object result;
+
+      getUntil(anIndex);
+
+      if (anIndex<valuesCache.size())
+      {
+        result = valuesCache.get(anIndex);
+
+        return result;
+      }
+      else
+        throw new RuntimeException( "Iterator out of bounds" );
+    }
+
+    public int size() {
+      return list.size();
+    }
+
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class BeanAdapter implements Context, VelocityAdapter {
+    private Object object;
+
+    public BeanAdapter(Object anObject) {
+      object = anObject;
+    }
+
+    public boolean containsKey(Object key) {
+      return true;
+    }
+
+    public Object getOriginal() {
+      return object;
+    }
+
+    public Object get(String aKey) {
+      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 GeneratorFailure(t);
+      }
+    }
+
+    public Object[] getKeys() {
+      return new Object[] {};
+    }
+
+    public Object put(String aKey, Object aValue) {
+      try {
+        if (PropertyUtils.isWriteable(object, aKey))
+          PropertyUtils.setSimpleProperty(object, aKey, unmakeAdapter(aValue));
+        else
+          MethodUtils.invokeExactMethod(object, "set", new Object[] {aKey, unmakeAdapter(aValue)});
+
+        return this;
+      }
+      catch (Throwable t) {
+        throw new GeneratorFailure(t);
+      }
+    }
+
+    public Object remove(Object aKey) {
+      throw new RuntimeException("BeanAdapter.remove not supported");
+    }
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  private static class VelocityLoggerWrapper implements LogSystem {
+    private LoggerWrapper logger;
+
+    public VelocityLoggerWrapper(LoggerWrapper aLogger) {
+      logger = aLogger;
+    }
+
+    public void init(RuntimeServices aRuntimeServices) {
+    }
+
+    public void logVelocityMessage(int aLevel, String aMessage) {
+      switch (aLevel) {
+        case DEBUG_ID:
+          logger.debug(aMessage);
+          break;
+        case ERROR_ID:
+          logger.error(aMessage);
+          break;
+        case INFO_ID:
+          logger.info(aMessage);
+          break;
+        default:
+          logger.warn(aMessage);
+          break;
+      }
+    }
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  public static class VelocityGeneratorLibrary implements GeneratorLibrary {
+    private VelocityEngine engine;
+
+    public VelocityGeneratorLibrary(String aTemplateRoot) throws GeneratorExc, GeneratorFailure {
+      try {
+        engine = new VelocityEngine();
+        try {
+          engine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
+        }
+        catch (Throwable t) {
+          logger.error(VelocityEngine.RESOURCE_LOADER);
+        }
+
+        try {
+          engine.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
+        }
+        catch (Throwable t) {
+          logger.error("file.resource.loader.class");
+        }
+
+        try {
+          engine.setProperty("file.resource.loader.path", aTemplateRoot);
+        }
+        catch (Throwable t) {
+          logger.error("file.resource.loader.path");
+
+        }
+        try {
+          engine.setProperty("file.resource.loader.cache", "true");
+        }
+        catch (Throwable t) {
+          logger.error("file.resource.loader.cache");
+
+        }
+        try {
+          engine.setProperty("file.resource.loader.modificationCheckInterval", "10");
+        }
+        catch (Throwable t) {
+          logger.error("file.resource.loader.modificationCheckInterval");
+
+        }
+
+        try {
+          engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new VelocityLoggerWrapper(logger));
+        }
+        catch (Throwable t) {
+          logger.error(VelocityEngine.RUNTIME_LOG_LOGSYSTEM);
+
+        }
+        engine.init();
+      }
+      catch (Throwable t) {
+        t.printStackTrace(logger.asPrintWriter(logger.ERROR_MESSAGE));
+
+        logger.error("Failed to set up a VelocityGeneratorLibrary: " + t.toString());
+        throw new GeneratorFailure(t);
+      }
+    }
+
+    public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
+      return new VelocityGenerator(anIdentifier, this);
+    }
+  }
+
+  /**
+   *
+   * <p>Title: </p>
+   * <p>Description: </p>
+   * <p>Copyright: Copyright (c) 2003</p>
+   * <p>Company: </p>
+   * @author not attributable
+   * @version 1.0
+   */
+  public static class VelocityGeneratorLibraryFactory implements GeneratorLibraryFactory {
+    private String basePath;
+
+    public VelocityGeneratorLibraryFactory(String aBasePath) {
+      basePath = aBasePath;
+    }
+
+    public GeneratorLibrary makeLibrary(String anInitializationString) throws GeneratorExc, GeneratorFailure {
+      return new VelocityGeneratorLibrary(basePath+anInitializationString);
+    };
+  }
+}