Velocity最新を使おう

プロジェクトの中で開発中、一度導入したツールのバージョンUPに目が届かないことが多い。

Velocity1.6.2 が、2009年3月にリリースされていた。
1.5 との差を調査するのに、まずはJARに入っている velocity.properties の diff を調べてみた。

(1) runtime.log.logsystem.class → default LogChute to use: 指定クラスが追加されてる。
でも、結局、org.apache.velocity.runtime.log.NullLogSystem 設定して使ってる。

(2) foreach文の変数に、イテレータのHasNext が追加された、velocityHasNext
directive.foreach.iterator.name = velocityHasNext

(3) マクロの深さ → velocimacro.max.depth = 20

(4) STRICT REFERENCE MODE 機能の追加
# -----------------------------------------------------------------------
# STRICT REFERENCE MODE
# -----------------------------------------------------------------------
# if true, will throw a MethodInvocationException for references
# that are not defined in the context, or have not been defined
# with a #set directive. This setting will also throw an exception
# if an attempt is made to call a non-existing property on an object
# or if the object is null. When this property is true then property
# 'directive.set.null.allowed' is also set to true.
# -----------------------------------------------------------------------
runtime.references.strict = false

これを true にすると

例えば、置換対象の変数が NULL であると、$!の記述であっても
MethodInvocationException をスローする。

→ org.apache.velocity.exception.MethodInvocationException

シビアなテンプレートにする場合に便利!

(5) Evaluate 実行を明示的に指定できるようになったデフォルトは
# -----------------------------------------------------------------------
# EVALUATE
# -----------------------------------------------------------------------
# Evaluate VTL dynamically in template. Select a class for the Context
# -----------------------------------------------------------------------
directive.evaluate.context.class = org.apache.velocity.VelocityContext

それから、特筆すべきは、
マクロは、自然に、#parse で指定するファイルの中に書けるようになった。
ことである。
動物的コーディング考察-8で書いていたコードは、
Velocity1.5 での動作であるが、1.6 では、
#parse("macro.vm")
#ListJoin($list,$length)
と書いて、macro.vm に、ListJoin マクロを記述して想定の動作になる。