You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Josh Landin <jl...@gmail.com> on 2005/10/04 23:19:47 UTC

Desired Enhancement to PropertyMessageResources?

Hello,

First and foremost, thanks to all of you for providing your time and effort
to the Struts framework. The Struts product is very much appreciated.

I've been using an enhanced version of PropertyMessageResources (and a
factory counterpart) in my own development and was wondering if the Struts
project would be interested in its incorporation. These classes provide some
features that ease development and performance. I've included the class
javadoc below which describes the enhanced functionality and summarizes its
features in a bulleted list.

--
Josh
 net.paperklip.client.webapp.util
Class EnhancedMessageResources

java.lang.Object
  |
    +--org.apache.struts.util.PropertyMessageResources
        |
        +--*net.paperklip.client.webapp.util.EnhancedMessageResources***

This class supports the loading of java.util.Properties files as resource
bundles just like its parent. The following additional enhancements are also
supported.

   - The config parameter can be a delimited list of property files.
   (delimiters are tab, newline, and/or comma)
   - The config parameter can contain "extends:<resourceKey>" syntax
   allowing for one or more parent bundles.
   - Handles recursively nested property replacements while arguments are
   left intact. (For example: app.msg=Hello {0}, my {app.relationship}.)
   - Expands arguments to their message resource value when { and }
   directives are provided in the argument value.

This class provides support for extending other resource bundles. The
sub-bundles can override any of the entries in the parent while also
providing their own. When a message is being retrieved from the child
bundle, the key/locale pair is first searched-for within the child bundle.
If not found the parent bundles are searched in order until the entry is
found. If not found in any of the related bundles the default null handling
for this bundle is used. Any number of bundles can be extended allowing a
reduced memory footprint and simplified management of resource entries. This
"extends" relationship in conjunction with the support for nested property
replacements allows "parent" bundles to refer to keys in "child" bundles
with OR without providing their own value for the key. Additionally, this
feature allows a parent resource bundle to be considered abstract and as
such, incomplete, without being extended.

Consider the following example of an extended resource bundle as well as
nested property replacement:

*Struts Configuration:*

 <message-resources key="parent" parameter="com.foo.Parent"
    factory="net.paperklip.client.webapp.util.EnhancedPropertyMessageResourcesFactory"/>

 <message-resources key="child"  parameter="extends:parent,com.foo.Child"
    factory="net.paperklip.client.webapp.util.EnhancedPropertyMessageResourcesFactory"/>

*com.foo.Parent:*

 a={b}
 b=Goodbye {c}
 c=World
 d=Foo {e}

*com.foo.Child:*

 b=Hello {c}
 e=Bar

Given the above example, the following can be expected:

   - A request for "a" from the child bundle would yield the string
   "Hello World".
   - A request for "a" from the parent bundle would yield the string
   "Goodbye World".
   - A request for "d" from the child bundle would yield the string "Foo
   Bar".
   - A request for "d" from the parent bundle would yield the string "Foo
   <null>" since "e" is abstract.



 When argument values are encountered and found to be wrapped with braces,
the named resource entry within the brackets is retrieved. One area this
helps is in the validation framework used within struts, allowing the name
of an argument resource entry to be looked-up during the view resolution
(instead of prior).
 Consider a branded application where each resource bundle is keyed by brand
name:

*Validator Configuration:*

<form name="/sys/findPerson">
  <field property="name" depends="minlength">
    <arg position="0" key="{form.person.name
<http://form.person.name>}" resource="false"/>
  </field>
  . . .
</form>

*JSP File:*

<html:messages id="msg" message="true" bundle="${brandName}">
     <c:out value="${msg}"/><br>
</html:messages>

Note that the validation framework is told to not lookup the argument key,
and instead the resource key argument
form.person.name<http://form.person.name>is resolved for a given
brand.

@author jlandin