java - Apache 速度 : Is there a standard way of verifying the correctness of a template from the command line?

标签 java apache velocity

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

是否有从命令行验证 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.");
        }
    }
}

最佳答案

有一个随 Velocity 一起分发的工具,称为 TemplateTool,它可以转储所有引用,并可用于验证模板的语法。

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

关于java - Apache 速度 : Is there a standard way of verifying the correctness of a template from the command line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1910857/

相关文章:

java - Apache Velocity 模板语言 - 忽略无效引用

java - 了解 Groovy 中的 findAll

java - GetText() 工作正常,但 setText() 会使应用程序崩溃

java - android字符串如何放置&字符

apache - Chrome 想要下载 index.php

liferay - 如何获取 Liferay 速度模板中的页面标题

java - Spring MVC 绑定(bind)到类型字段

apache - Hive - 将 select 语句中的指定值插入到表和分区值中

mysql - Debian,mysql : How can I tune these values

liferay - 在速度模板中创建对象