Apache Velocity:验证上下文中的所有变量是否都在Velocity模板中存在

huangapple go评论68阅读模式
英文:

Apache Velocity: Verify if all variables in a context are present in velocity template

问题

在Apache Velocity 1.7中,是否可以检查指定在VelocityContext中的所有键是否对模板有效?例如,假设我有一个名为card.vm的模板,如下所示:

card {
    type: CREDIT
    company: VISA
    name: "${firstName} ${lastName}"
}

如果我执行以下代码,由于cardNumber未在card.vm模板中出现,它应该会引发错误:

VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
context.put("lastName", "man");
context.put("cardNumber", "1234");
StringWriter writer = new StringWriter();
t.merge(context, writer);

(注意:我已经将双引号改为了中文引号“”以避免混淆)

英文:

Is it possible in Apache Velocity 1.7 to check if all keys specified in a VelocityContext are valid for a template? For example, let's say I have a template, card.vm, that looks like this:

card {
    type: CREDIT
    company: VISA
    name: "${firstName} ${lastName}"
}

If I then execute the following code, it should throw an error since cardNumber is not present in the card.vm template:

VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
context.put("lastName", "man");
context.put("cardNumber", "1234");
StringWriter writer = new StringWriter();
t.merge(context, writer);

答案1

得分: 0

如克里斯托弗在速度邮件列表中提到的,针对你之前的问题,你可以随时创建VelocityContext的子类来自定义行为。

例如,这将跟踪哪些键已放入上下文并跟踪上下文中使用了哪些键。然后在合并模板后,您将调用context.checkForUnreferencedValues()。

根据哪些辅助对象向您的上下文添加键以及您是否在重复使用,您可能需要定制代码。

    static class ReferenceCountingVelocityContext extends VelocityContext {
        private Set<String> keysToCheck = new HashSet<>();
        private Set<String> keysUsed = new HashSet<>();

        @Override
        public Object put(String key, Object value) {
            keysToCheck.add(key);
            return super.put(key, value);
        }

        @Override
        public Object get(String key) {
            keysUsed.add(key);
            return super.get(key);
        }
        public void checkForUnreferencedValues() throws UnusedVelocityContextKeyException {
            if (!keysToCheck.containsAll(keysUsed)) {
                throw new UnusedVelocityContextKeyException();
            }
        }
    }
英文:

As Christopher mentioned on the velocity mailing list for your earlier question, you can always create a subclass of VelocityContext to customize behavior like this.

For instance, this will track what keys have been put into the context and track what keys were used in the context. You would then call context.checkForUnreferencedValues() after you merge the template.

You will likely need to customize the code depending on what helper objects are adding keys to your context and whether you are reusing it.

    static class ReferenceCountingVelocityContext extends VelocityContext {
        private Set<String> keysToCheck = new HashSet<>();
        private Set<String> keysUsed = new HashSet<>();

        @Override
        public Object put(String key, Object value) {
            keysToCheck.add(key);
            return super.put(key, value);
        }

        @Override
        public Object get(String key) {
            keysUsed.add(key);
            return super.get(key);
        }
        public void checkForUnreferencedValues() throws UnusedVelocityContextKeyException {
            if (!keysToCheck.containsAll(keysUsed)) {
                throw new UnusedVelocityContextKeyException();
            }
        }
    }

huangapple
  • 本文由 发表于 2023年6月16日 14:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定