Apache Velocity:是否有一种标准方法可以从命令行验证模板的正确性?

2023-11-24

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

是否有一种标准方法可以从命令行验证 Velocity 模板的正确性?

我准备读取模板路径、初始化 Velocity Engine、解析模板并捕获任何错误如本页所示,但是如果有一个现成的工具可以获取文件和配置,并吐出任何错误,那么我宁愿使用它。


Update

这就是我最终所做的:

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 的工具,它会转储所有引用,并可用于验证模板的语法。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apache Velocity:是否有一种标准方法可以从命令行验证模板的正确性? 的相关文章

随机推荐

  • 为什么在传递 std::ofstream 作为参数时要使用“使用已删除”函数? [复制]

    这个问题在这里已经有答案了 我有一个会员是std ofstream fBinaryFile and a void setFile std ofstream pBinaryFile fBinaryFile pBinaryFile output
  • 如何从 Pandas 的 OLS 摘要中提取特定值?

    是否有可能从 pandas 的线性回归摘要中获取其他值 目前我只知道一种获取 beta 和截距的方法 我需要得到 R 平方 这是手册的摘录 In 244 model ols y rets AAPL x rets ix GOOG In 245
  • Ruby,如何访问 do-end 循环之外的局部变量

    我有一个循环 在远程计算机上执行一系列命令 ssh exec cd vmfs volumes 4c6d95d2 b1923d5d 4dd7 f4ce46baaadc ghettoVCB ghettoVCB sh f vms to backu
  • 从表中删除重复行

    我的数据库中有一个表 其中包含我想要删除的重复记录 我不想为此创建一个包含不同条目的新表 我想要的是从现有表中删除重复的条目而不创建任何新表 有什么办法可以做到这一点吗 id action L1 name L1 data L2 name L
  • 如何在 PHP 中重复数组?

    arr array 1st 1st 以上 arr has 2项目 我想重复 arr这样它就充满了4 items PHP 中有单次调用吗 数组填充函数应该有帮助 array array fill int start index int num
  • IEquatable 和仅仅重写 Object.Equals() 有什么区别?

    我想要我的Food类能够在它等于另一个实例时进行测试Food 稍后我将针对列表使用它 并且我想使用它List Contains 方法 我应该实施IEquatable
  • 使用 Javascript 语法高亮代码 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 您可以推荐哪些 Javas
  • 如何在前台运行 GcmListenerService

    我的 GCM 服务有时会出现问题 当我的智能手机的 RAM 自动清除时 该服务会关闭 请阅读this如果您需要更多详细信息 据我了解 如果我将服务设置为在前台运行 它应该有助于系统用 RAM 删除它 方法为Service class onS
  • HTML5 与 HTML4 - h1 标签呈现有额外的空间 - 如何删除?

    我选取了一个 DTD 为 HTML4 Transitional 的页面 并将文档类型更改为h1 和其下方的 div 之间会出现额外的空间 我没有对标记或 CSS 进行任何其他更改 JSFiddle 示例 http jsfiddle net
  • 是否有不需要空终止字符串的 strtol 等效项?

    是否有类似于 strtol 的标准 C 函数 它将采用 char 和非空终止字符串的长度 我知道我可以将字符串复制到空终止区域 但出于效率原因 这是不可取的 标准库中没有这样的函数 您要么必须使用临时缓冲区方法 要么从头开始编写自己的函数
  • Python导入csv到列表[重复]

    这个问题在这里已经有答案了 我有一个包含大约 2000 条记录的 CSV 文件 每条记录都有一个字符串和一个类别 This is the first line Line1 This is the second line Line2 This
  • iOS 版 Google 地图,swift - 如何显示标记之间的整个折线?

    我正在尝试在谷歌地图视图中拟合一条折线 折线是通过谷歌地图方向API中的overview polyline获取的 想知道如何将编码的折线转换为可以使用的东西 我需要使折线适合地图视图 我发现要做的就是适应边界以显示所有标记 但不显示整个折线
  • 将代码与两个 subversion 存储库同步

    首先介绍一下背景 我正在使用来自远程 SVN 存储库的 基本 代码 不受我的控制 代码还没有标记 所以我总是需要跟上主干 由于多种原因 最重要的是我们对代码的本地扩展具有 利基 性质 并且旨在解决使用代码的项目的特定问题 我无法使用远程存储
  • Kafka I/O 错误 java.io.EOFException: null

    我正在使用 Kafka 0 8 2 0 Scala 2 10 在我的日志文件中 我间歇性地看到以下消息 这似乎是一个连接问题 但我正在本地主机中运行这两个问题 这是无害的警告消息还是我应该采取措施来避免它 2015 10 30 14 12
  • 从图表系列获取主题颜色信息

    我有一个使用一种颜色的图表系列 它可以是msoThemeColorAccent lt gt 或任何其他 用于标记线 另一种颜色用于标记填充 可以是msoThemeColorAccent lt gt 打火机 x 或任何其他 并且没有线条 我想
  • find 命令查找文件并将它们连接起来

    我正在尝试查找所有类型的文件 gz and cat他们到total gz我想我已经很接近这一点了 这是我用来列出所有的命令 gzfiles find home downloaded maxdepth 3 type d name exec b
  • Toast:在 Android 中集成 Google Plus 时发生内部错误

    我正在将 Google Plus 集成到我的 Android 应用程序中 我已经在 Google API 控制台中创建了该项目 我创建了 OAuth 客户端 ID 并仔细检查了包名称和密钥库 SHA1 但两者都是正确的 但我仍然得到Inte
  • Quartz 中每 50 秒执行一次 Cron 表达式

    我每 50 秒使用 Quartz 和 cron 表达式运行我的作业 Cron Expression 0 50 发生的情况是我的工作以秒为单位运行 50 60 50 60 而且不是每 50 秒一次 并且不在第二个 0 处运行 从 0 开始每
  • 获取 JSON 对象的大小

    我有一个由 AJAX 请求返回的 JSON 对象 但我遇到了一些问题 length因为它不断返回undefined 只是想知道我是否正确使用它 console log data length console log data phones
  • Apache Velocity:是否有一种标准方法可以从命令行验证模板的正确性?

    我们的网站使用阿帕奇速度模板语言 我们的内容管理系统已经检查所有生成的 XML 文档的格式是否良好 我们被要求在将文件推送到实时站点之前检查文档以捕获 Velocity 语法错误 是否有一种标准方法可以从命令行验证 Velocity 模板的