Re: [tools] Using java.text.MessageFormat in templates

Christopher Schultz <[email protected]> Fri, 21 Feb 2014 13:17:40 -0500
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
All,

On 2/21/14, 12:48 PM, Christopher Schultz wrote:
> All,
> 
> I have a template where I'd like to take some text in a variable and do
> parametric-replacement like new
> MessageFormat("format").format(replacements) might do.
> 
> I'm already using Velocity Tools/Struts to great effect with pre-defined
> messages in properties files, but this data needs to come from a
> database and I'd rather not pre-process the data in Java code unless
> absolutely necessary.
> 
> I've browsed through the tools available from Velocity Tools and I don't
> really see anything that would meet my needs. I even looked at ClassTool
> to see if I could instantiate an object through brute-force and it looks
> like that is not possible.
> 
> Is there a stock tool I can use, or will I have to write my own?

Of course, as soon as I post, I find ResourceTool.render, which appears
to do exactly what I want.

I can see that while the Javadoc says the method signature is
render(Object resource, Object[] args), Velocity treats it as a varargs
call and so you need to do:

   $text.render("format", arg, arg, arg)

 instead of

   $text.render("format", [arg, arg, arg])

Use of the latter yields unfortunate results ;)

Also, it looks like the default key for ResourceTool is "text", as I can
see this during startup:

INFO:  Velocity  [debug] Configuring factory with:
FactoryConfiguration from 8 sources including 4 data with 3 toolboxes:
 Toolbox 'application' with 1 properties [scope -auto-> application; ]
and 17 tools:
  Tool 'alternator' => org.apache.velocity.tools.generic.AlternatorTool
  Tool 'class' => org.apache.velocity.tools.generic.ClassTool
[...]
  Tool 'text' => org.apache.velocity.tools.generic.ResourceTool

...but it conflicts with Velocity Struts, which re-defined the key:

 Toolbox 'request' with 2 properties [scope -auto-> request; xhtml
-auto-> true;
 ] and 16 tools:
  Tool 'context' => org.apache.velocity.tools.view.ViewContextTool
  [...]
  Tool 'text' => org.apache.velocity.tools.struts.MessageTool

Since the "Struts" key is at a closer scope (request instead of
application), it masks the definition at the application level. I can
clearly re-name the ResourceTool's key, but I was wondering if it was
intentional to have a tool key naming conflict there?

Thanks,
-chris
signature.asc (application/pgp-signature, 924 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTB5hEAAoJEBzwKT+lPKRYehUP/AwBbYyDDCbGehg0hIRBSdzv
UC8PsoKDMhyWOsYI+QRdhmdX1DBdkfZv8kEqU5JEjhFS6KLxhK5at7/nuMgHtq8X
SXliO3odd5QJL/pG21vtYQ3Zn1sCCmjNlMqxQO5KjbsYuDL3TOpDNXkJ6b6m3Bi/
3VtbjbQFZjvZyzkdx42s8nbOjcRq7uQ/poVoHMjim4GetKVLBSCB6zVsPIdecwWV
76Ag66QUzjiGpfCsE3oPSyQXZw+NZS9kda7lOb7gcNKmiOi1AhC3aqnTqlv4gFqU
r0DxamMmMos4xYjH6cXnNmHve3QFKamtKXx3edhLwym0TK6rwAnzGdLo4ryDXF62
vDpdigLxlqcFF7HUebftBmZWG5JaGQJAG8gtYCx4OdhmYMmCqznl6l8bN7tz8Epo
fYvB+v6AoB8tv7kJr5awA0C0yCWCecxs7q1hfIos1MG7CoQQ2kSZvOb5QnZS6TOr
+CV6EhCjZ+NeUzjo+wcR/Rj6gX7HDVSiHc+BSgOZ7qiCXGFcGmQiK0sQI8h6VOOf
O0z95CQDYyWFOWEF30OILUX2V1mO7G+wi/FRxX4y/30bq2S2SnJhu7XvbzR8JSU+
9EIa0SeEnobUr5YA22nFgV9Hf+1O7u4wPMhBkXUk3ndsFjXwLZcsaW699/2kCdQD
bV2CbsrnZKCkd7AroINn
=P8bj
-----END PGP SIGNATURE-----