small bugfix: converting arrays.
[mir.git] / source / mir / generator / FreemarkerGenerator.java
index 11337f6..a786a71 100755 (executable)
@@ -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.
  *
  * 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.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;
@@ -46,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");
 
@@ -54,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 );
     }
   }
@@ -76,6 +95,10 @@ 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;
@@ -83,10 +106,14 @@ public class FreemarkerGenerator implements Generator {
       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,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;
@@ -113,7 +152,6 @@ public class FreemarkerGenerator implements Generator {
     }
 
     public void remove(String aKey) {
-      // ML: kinda tricky...
     }
 
     public boolean isEmpty() {
@@ -144,9 +182,9 @@ public class FreemarkerGenerator implements Generator {
   }
 
   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;
@@ -269,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;
@@ -290,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 {
@@ -308,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);
+    };
+  }
 }