代码之家  ›  专栏  ›  技术社区  ›  braveterry

ApacheVelocity:是否有一种标准的方法来验证命令行中模板的正确性?

  •  6
  • braveterry  · 技术社区  · 15 年前

    我们的网站使用 Apache Velocity 模板语言。我们的内容管理系统已经检查了任何生成的XML文档的格式是否正确。在将文件推送到活动站点之前,我们被要求检查文档以捕获Velocity语法错误。

    是否有一种标准的方法来验证命令行中速度模板的正确性?

    我准备阅读模板路径,初始化速度引擎,解析模板,并捕获任何错误。 as shown on this page 但是,如果有一个现成的工具,它获取一个文件和一个配置,并指出任何错误,那么我宁愿使用它。


    更新

    我最后做的是:

    package velocitysample;
    
    import java.io.IOException;
    import java.io.StringWriter;
    import org.apache.log4j.Logger;
    import org.apache.log4j.BasicConfigurator;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.Template;
    import org.apache.velocity.app.Velocity;
    import org.apache.velocity.exception.ResourceNotFoundException;
    import org.apache.velocity.exception.ParseErrorException;
    import org.apache.velocity.exception.MethodInvocationException;
    
    public class Main
    {
       /** Define a static logger variable so that it references the Logger
        *  instance named "MyApp".
        */
        private static Logger logger = Logger.getLogger(Main.class);
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
    
            /* Set up a simple log4j configuration that logs on the console. */
            BasicConfigurator.configure();
    
    
            /* Check to see that a template path was passed on the command line. */
            if (args.length != 1)
            {
                logger.fatal("You must pass the path to a template as a " +
                        "command line argument.");
                return;
            }
    
            /* Pull the template filename from the command line argument. */
            String fileName = args[0];
    
            try
            {
                Velocity.setProperty("resource.loader", "file");
                Velocity.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
                Velocity.setProperty("file.resource.loader.path", "/templates/");
                Velocity.setProperty("file.resource.loader.cache", "false");
                Velocity.setProperty("file.resource.loader.modificationCheckInterval", "0");
    
                Velocity.init();
            }
            catch (Exception ex)
            {
                logger.fatal("Error initializing the Veolcity engine.", ex);
                return;
            }
    
            boolean error = false;
    
            /* Create an empty Velocity context */
            VelocityContext context = new VelocityContext();
    
            Template template = null;
    
            try
            {
               template = Velocity.getTemplate(fileName);
            }
            catch( ResourceNotFoundException rnfe )
            {
               logger.error("Couldn't find the template to parse at this path: " +
                       fileName + ".", rnfe);
               error = true;
            }
            catch( ParseErrorException peex )
            {
                logger.error("Error parsing the template located at this path: " +
                        fileName + ".", peex);
                error = true;
            }
            catch( MethodInvocationException mie )
            {
                logger.error("Something invoked in the template (" + fileName +
                        ") threw an Exception while parsing.", mie);
                error = true;
            }
            catch( Exception e )
            {
                logger.error("An unexpected exception was thrown when attempting " +
                        "to parse the template: " + fileName + ".", e);
                error = true;
            }
    
            if (error)
            {
                return;
            }
    
            StringWriter sw = new StringWriter();
            try
            {
                template.merge(context, sw);
            } 
            catch (ResourceNotFoundException rnfe)
            {
                logger.error("Couldn't find the template to merge at this path: " +
                       fileName + ".", rnfe);
                error = true;
            } 
            catch (ParseErrorException peex)
            {
                logger.error("Error parsing the template at this path during merge: " +
                        fileName + ".", peex);
                error = true;
            } 
            catch (MethodInvocationException mie)
            {
                logger.error("Something invoked in the template (" + fileName +
                        ") threw an Exception while merging.", mie);
                error = true;
            } 
            catch (IOException ioe)
            {
                logger.error("Error reading the template located at this path from " +
                        "disk: " + fileName + ".", ioe);
                error = true;
            }
            catch( Exception e )
            {
                logger.error("An unexpected exception was thrown when attempting " +
                        "to merge the template: " + fileName + ".", e);
                error = true;
            }
    
            if (!error)
            {
                logger.info("No syntax errors detected.");
            }
        }
    }
    
    3 回复  |  直到 12 年前
        1
  •  4
  •   ZZ Coder    15 年前

    有一个与Velocity一起分发的工具,叫做template tool,它可以转储所有引用,并且可以用来验证模板的语法。

    但是,必须正确设置上下文才能验证任何模板。所以最好的验证是用自己的上下文编写自己的工具。

        2
  •  1
  •   Will Glass    15 年前

    要捕获速度语法错误,您的方法可能是最好的。

    但是,它将忽略无效的宏参数和不存在的引用。在Velocity 1.6中,您可以设置一个严格的模式,该模式将对错误的宏参数(例如错误的数字)或错误的引用(例如$abc.badmethod())引发异常。这假设您正在填充测试工具中使用的模板的上下文,与在生产中使用模板时相同。

        3
  •  1
  •   Claes    12 年前

    2010年12月,有人发布了一个验证速度的工具。我试过了,效果很好。它使用的是速度1.6.4,但如果需要的话,也许可以换成其他版本。

    http://code.google.com/p/velocity-validator/