fileedit fixed
[mir.git] / source / mir / generator / FreemarkerGenerator.java
index 11d8610..c0ebb76 100755 (executable)
  * 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.  
+ * 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;
@@ -37,8 +38,10 @@ 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;
@@ -119,9 +122,12 @@ 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
       return makeBeanAdapter(anObject);
-//      throw new TemplateModelException("Unadaptable class: " + anObject.getClass().getName());
   }
 
   private static class MapAdapter implements TemplateModelRoot {
@@ -138,7 +144,6 @@ public class FreemarkerGenerator implements Generator {
     }
 
     public void remove(String aKey) {
-      // ML: kinda tricky...
     }
 
     public boolean isEmpty() {
@@ -336,7 +341,10 @@ public class FreemarkerGenerator implements Generator {
 
     public TemplateModel get(String aKey) throws TemplateModelException {
       try {
-        return makeAdapter(PropertyUtils.getSimpleProperty(object, aKey));
+        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());
@@ -356,7 +364,7 @@ public class FreemarkerGenerator implements Generator {
       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);