*/
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;
/**
*
- * <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
*/
* @param aFunction
* @return
*/
- private static Object makeFunctionAdapter(Generator.GeneratorFunction aFunction) {
+ private static Object makeFunctionAdapter(Generator.Function aFunction) {
return new FunctionAdapter(aFunction);
}
if (anObject instanceof VelocityAdapter) {
return ((VelocityAdapter) anObject).getOriginal();
}
- else
- return anObject;
+ return anObject;
}
/**
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)
* @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]));
}
public Object call() throws GeneratorExc {
- return makeAdapter(function.perform(new Vector()));
+ return makeAdapter(function.perform(new ArrayList()));
}
public Object call(Object anObject) throws GeneratorExc {
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();
{
valuesCache.add(makeAdapter(iterator.next()));
}
- };
+ }
public Object getOriginal() {
return iterator;
return result;
}
- else
- throw new RuntimeException( "Iterator out of bounds" );
+ throw new RuntimeException( "Iterator out of bounds" );
}
public int size() {
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) {
{
valuesCache.add(makeAdapter(list.get(valuesCache.size())));
}
- };
+ }
public Object getOriginal() {
return list;
return result;
}
- else
- throw new RuntimeException( "Iterator out of bounds" );
+ throw new RuntimeException( "Iterator out of bounds" );
}
public int size() {
* @version 1.0
*/
private static class VelocityLoggerWrapper implements LogSystem {
- private LoggerWrapper logger;
-
+
public VelocityLoggerWrapper(LoggerWrapper aLogger) {
logger = aLogger;
}
* @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 {
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);
}
* @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));
+ }
}
}