fix of human readable size and media info
[mir.git] / source / mir / generator / VelocityGenerator.java
index a6c0a77..340aa4b 100755 (executable)
  */
 package mir.generator;
 
+import java.io.File;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.AbstractList;
+import java.util.ArrayList;
 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 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;
@@ -105,37 +106,6 @@ public class VelocityGenerator implements Generator {
 
   /**
    *
-   * <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
    */
@@ -166,7 +136,7 @@ public class VelocityGenerator implements Generator {
    * @param aFunction
    * @return
    */
-  private static Object makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
+  private static Object makeFunctionAdapter(Generator.Function aFunction) {
     return new FunctionAdapter(aFunction);
   }
 
@@ -201,8 +171,7 @@ public class VelocityGenerator implements Generator {
     if (anObject instanceof VelocityAdapter) {
       return ((VelocityAdapter) anObject).getOriginal();
     }
-    else
-      return anObject;
+               return anObject;
   }
 
   /**
@@ -217,8 +186,8 @@ public class VelocityGenerator implements Generator {
     if (anObject instanceof Context)
       return anObject;
 
-    else if (anObject instanceof Generator.GeneratorFunction)
-      return makeFunctionAdapter((Generator.GeneratorFunction) anObject);
+    else if (anObject instanceof Generator.Function)
+      return makeFunctionAdapter((Generator.Function) anObject);
     else if (anObject instanceof Integer)
       return anObject;
     else if (anObject instanceof Boolean)
@@ -249,18 +218,18 @@ public class VelocityGenerator implements Generator {
    * @version 1.0
    */
   public static class FunctionAdapter implements VelocityAdapter {
-    private GeneratorFunction function;
+    private Function function;
 
     public Object getOriginal() {
       return function;
     }
 
-    private FunctionAdapter(GeneratorFunction aFunction) {
+    private FunctionAdapter(Function aFunction) {
       function = aFunction;
     }
 
     public Object call(Object aParameters[]) throws GeneratorExc {
-      List parameters = new Vector();
+      List parameters = new ArrayList();
 
       for (int i = 0; i<aParameters.length; i++) {
         parameters.add(unmakeAdapter(aParameters[i]));
@@ -270,7 +239,7 @@ public class VelocityGenerator implements Generator {
     }
 
     public Object call() throws GeneratorExc {
-      return makeAdapter(function.perform(new Vector()));
+      return makeAdapter(function.perform(new ArrayList()));
     }
 
     public Object call(Object anObject) throws GeneratorExc {
@@ -364,14 +333,11 @@ public class VelocityGenerator implements Generator {
   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;
-
+      valuesCache = new ArrayList();
 
       if (iterator instanceof RewindableIterator) {
         ((RewindableIterator) iterator).rewind();
@@ -383,7 +349,7 @@ public class VelocityGenerator implements Generator {
       {
         valuesCache.add(makeAdapter(iterator.next()));
       }
-    };
+    }
 
     public Object getOriginal() {
       return iterator;
@@ -400,8 +366,7 @@ public class VelocityGenerator implements Generator {
 
         return result;
       }
-      else
-        throw new RuntimeException( "Iterator out of bounds" );
+                       throw new RuntimeException( "Iterator out of bounds" );
     }
 
     public int size() {
@@ -423,13 +388,11 @@ public class VelocityGenerator implements Generator {
   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;
+      valuesCache = new ArrayList();
     }
 
     private void getUntil(int anIndex) {
@@ -437,7 +400,7 @@ public class VelocityGenerator implements Generator {
       {
         valuesCache.add(makeAdapter(list.get(valuesCache.size())));
       }
-    };
+    }
 
     public Object getOriginal() {
       return list;
@@ -454,8 +417,7 @@ public class VelocityGenerator implements Generator {
 
         return result;
       }
-      else
-        throw new RuntimeException( "Iterator out of bounds" );
+                       throw new RuntimeException( "Iterator out of bounds" );
     }
 
     public int size() {
@@ -533,8 +495,7 @@ public class VelocityGenerator implements Generator {
    * @version 1.0
    */
   private static class VelocityLoggerWrapper implements LogSystem {
-    private LoggerWrapper logger;
-
+   
     public VelocityLoggerWrapper(LoggerWrapper aLogger) {
       logger = aLogger;
     }
@@ -569,46 +530,22 @@ public class VelocityGenerator implements Generator {
    * @author not attributable
    * @version 1.0
    */
-  public static class VelocityGeneratorLibrary implements GeneratorLibrary {
+  public static class VelocityGeneratorLibrary implements Library {
     private VelocityEngine engine;
 
-    public VelocityGeneratorLibrary(String aTemplateRoot) throws GeneratorExc, GeneratorFailure {
+    public VelocityGeneratorLibrary(File 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.path", aTemplateRoot.getAbsolutePath());
           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");
+          logger.error("Error while constructing library: " + t.toString());
 
+          throw new GeneratorFailure(t);
         }
 
         try {
@@ -621,8 +558,6 @@ public class VelocityGenerator implements Generator {
         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);
       }
@@ -642,15 +577,15 @@ public class VelocityGenerator implements Generator {
    * @author not attributable
    * @version 1.0
    */
-  public static class VelocityGeneratorLibraryFactory implements GeneratorLibraryFactory {
-    private String basePath;
+  public static class VelocityGeneratorLibraryFactory implements LibraryFactory {
+    private File basePath;
 
-    public VelocityGeneratorLibraryFactory(String aBasePath) {
+    public VelocityGeneratorLibraryFactory(File aBasePath) {
       basePath = aBasePath;
     }
 
-    public GeneratorLibrary makeLibrary(String anInitializationString) throws GeneratorExc, GeneratorFailure {
-      return new VelocityGeneratorLibrary(basePath+anInitializationString);
-    };
+    public Library makeLibrary(String anInitializationString) throws GeneratorExc, GeneratorFailure {
+      return new VelocityGeneratorLibrary(new File(basePath, anInitializationString));
+    }
   }
 }