small bugfix: converting arrays.
[mir.git] / source / mir / generator / FreemarkerGenerator.java
index e92bd74..a786a71 100755 (executable)
@@ -1,12 +1,61 @@
+/*
+ * 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 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.Arrays;
+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;
@@ -15,7 +64,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");
 
@@ -23,8 +72,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 );
     }
   }
@@ -45,17 +95,25 @@ public class FreemarkerGenerator implements Generator {
     return new FunctionAdapter(aFunction);
   }
 
-  private static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
+  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)
@@ -64,13 +122,25 @@ public class FreemarkerGenerator implements Generator {
       return makeIteratorAdapter((Iterator) anObject);
     else if (anObject instanceof List)
       return makeIteratorAdapter(((List) anObject).iterator());
+               else if (anObject instanceof Object[]){
+                       if(((Object[])anObject).length <= 1){
+                               Object[] array = (Object[]) anObject;                           
+                               return makeAdapter(array[0]);
+                       } else {
+                               return makeIteratorAdapter(Arrays.asList((Object[]) 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;
@@ -82,7 +152,6 @@ public class FreemarkerGenerator implements Generator {
     }
 
     public void remove(String aKey) {
-      // ML: kinda tricky...
     }
 
     public boolean isEmpty() {
@@ -91,30 +160,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;
@@ -237,7 +307,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;
@@ -258,12 +328,44 @@ 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 {
@@ -276,4 +378,16 @@ public class FreemarkerGenerator implements Generator {
       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);
+    };
+  }
 }