Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

VelocityEngine.java

00001 package edu.virtualschool.jwaa.xml;
00002 import java.io.File;
00003 import java.io.FileNotFoundException;
00004 import java.io.IOException;
00005 import java.io.StringWriter;
00006 
00007 import org.apache.log4j.Logger;
00008 import org.apache.velocity.VelocityContext;
00009 import org.apache.velocity.app.Velocity;
00010 import org.apache.velocity.exception.MethodInvocationException;
00011 import org.apache.velocity.exception.ParseErrorException;
00012 import org.apache.velocity.exception.ResourceNotFoundException;
00013 import org.apache.velocity.runtime.RuntimeServices;
00014 import org.apache.velocity.runtime.log.LogSystem;
00015 import org.jdom.Element;
00016 
00017 import edu.virtualschool.jwaa.Fault;
00027 public class VelocityEngine 
00028   extends org.apache.velocity.app.VelocityEngine
00029 {
00030   final static Logger logger = Logger.getLogger(VelocityEngine.class.getName());
00031 
00032   VelocityEngine(ApplicationElement app, Element element) throws Fault
00033   {
00034     if (element == null)
00035       throw new ValidationFault("application requires a macros element");
00036     String name = element.getAttributeValue("path");
00037     if (name == null)
00038       throw new ValidationFault("macros element requires a path attribute");
00039     boolean enableDebug = isTrue(element.getAttributeValue("debug"));
00040     File file = new File(app.fileMonitor.file.getParent(), name);
00041     logger.debug("Loading velocity macros from "+file);
00042     VelocityLogger velocityLogger = new VelocityLogger();
00043     try
00044     {
00045       /* Specify file plus classpath resource loaders */
00046       setProperty("resource.loader", "file,class");
00047 
00048       /* File Resource Loader */
00049       setProperty("file.resource.loader.path",
00050         file.getParent());
00051       setProperty("file.resource.loader.description",
00052         "File Loader");
00053       setProperty("file.resource.loader.cache",
00054         enableDebug?"false":"true");
00055       setProperty("file.resource.loader.modificationCheckInterval",
00056         enableDebug?"0":"2");
00057 
00058       /* Classpath Resource Loader */
00059       setProperty("class.resource.loader.description",
00060         "Classpath Resource Loader");
00061       setProperty("class.resource.loader.class", 
00062         "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
00063 
00064       /* Velocimacros */
00065       setProperty("velocimacro.library", 
00066         file.getName());
00067       setProperty("velocimacro.library.autoreload",
00068         enableDebug?"true":"false");
00069 
00070       /* Logging */
00071       setProperty("runtime.log.logsystem",
00072         velocityLogger);
00073       setProperty("runtime.log.logsystem.log4j.category",
00074         logger.getName());
00075       setProperty("runtime.log.logsystem.class",
00076         velocityLogger.getClass().getName());
00077     /*
00078       setProperty("velocimacro.permissions.allow.inline", "true");
00079       setProperty("velocimacro.permissions.allow.inline.local.scope", "false");
00080       setProperty("resource.manager.logwhenfound", "false");
00081       setProperty("velocimacro.context.localscope", "false");
00082       setProperty("runtime.log.invalid.references", "true");
00083     */
00084       init();
00085     }
00086     catch (FileNotFoundException e)
00087     {
00088       throw new FileNotFoundFault(file + " not found");
00089     }
00090     catch (Exception e)
00091     {
00092       throw new Fault("Exception initializing Velocity engine", e);
00093     }
00094   }
00095   String evaluate(String text, VelocityContext context)
00096   throws Fault
00097   {
00098     StringWriter writer = new StringWriter();
00099     try
00100     {
00101       evaluate(context, writer, "", text);
00102     }
00103     catch (ResourceNotFoundException e)
00104     {
00105       throw new Fault("couldn't find resource: "+e.getMessage(), e);
00106     }
00107     catch (ParseErrorException e)
00108     {
00109       throw new Fault("velocity syntax error\n"+ e.getMessage()+
00110         "\n<pre>\n"+
00111         XMLUtil.escape(text)+
00112         "\n</pre>");
00113     }
00114     catch (MethodInvocationException e)
00115     {
00116       throw new Fault("Fault in the java code called by velocity\n\t"+  e.getMessage(), e);
00117     }
00118     catch (IOException e)
00119     {
00120       throw new Fault("IO fault in code called by velocity\n\t"+e.getMessage());
00121     }
00122     catch (Throwable e)
00123     {
00124       throw new Fault("Throwable in velocity", e);
00125     }
00126     return writer.toString();
00127   }
00128   public static class VelocityLogger implements LogSystem
00129   {
00130     public VelocityLogger()
00131     {
00132       try
00133       {
00134         Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this );
00135         Velocity.init();
00136       }
00137       catch (Exception e)
00138       {
00139       }
00140     }
00141     public void init( RuntimeServices rsvc )
00142     {
00143     }
00144     public void logVelocityMessage(int level, String message)
00145     {
00146       logger.debug(level+": "+message);
00147     }
00148   }
00149   static boolean isTrue(String s)
00150   {
00151     if (s == null)
00152       return false;
00153     return (s.equals("true") || s.equals("yes"));
00154   }
00155 }