Re: Parsing Properties files

Keats Kirsch <[email protected]> Fri, 15 Jul 2005 16:33:06 -0400
Newsgroups gmane.comp.java.webmacro.user
Message-ID <[email protected]>
I actually had a PropertyDirective years ago that did just what you are 
saying, but I've lost track of the code.  (It never got committed to the 
core because nobody expressed interest.)  There is some code in the core 
that parses expressions from the webmacro.properties file.  I can point 
you to that if it is helpful. 

As I recall, all I did was parse the properties as a template and then 
pipe the results into a properties object via the load() method.

I could probably resurrect this code for you if that would meet your needs.

Actually, I've been toying with a more generic #property directive which 
could set the properties on any object.  E.g.,

#properties $user {
   Name: $UserName
   Address: $UserAddress
   Date: $Today
}

It could have options like:

#properties $user create class="com.xyz.accounts.User" { ...

which would be equivalent to:

#bean $user="com.xyz.accounts.User"
#set $user.Name = $UserName
#set $user.Address = $UserAddress
#set $user.Date = $Today

If you don't specifiy a class, it would just default to HashMap or 
something.

Of course you could the #include with this as well:

#properties $user {
  #include "user.properties"
...

Cheers.

Keats



Lars George wrote:

> Hi,
>
> Has someone of you written a parser for standard Java properties 
> files, ie. so we can use .properties files to read in as templates?
>
> I am interested to get a i18n framework going where the actual content 
> is stored in properties files for easy localization. But I do not want 
> to use the ResourceBundle framework of Java, which is clunky and a bit 
> cumbersome. I rather would like to include those .properties directly 
> into a template and have WebMacro parse it into context variables. For 
> example:
>
> hello.wm:
> ---------
>
> #include "/res/$languagecode/hello.properties"
>
> Hello $msg1!
>
> hello.properties:
> -----------------
> msg1=World
>
>
> This is very simple example, this could be extended to have the 
> properties include figure out what is available and fall back to 
> default file as performed by the ResourceBundle class, eg.
>
> #include-res "res/hello.properties" $languagecode
>
> Assume $languagecode="FR", then it would try to load and parse 
> hello_fr.properties, if that is missing it is using hello.properties.
>
> The advantage is to rely on Webmacro for caching and reloads, while 
> the ResourceBundle cannot reload files ones they are loaded.
>
> A parser could simply use a Properties class to read the file and add 
> it to the context, even as a hashmap, so access would be like this
>
> $hello.msg1
>
> or the include directive could allow for a name that is given to the 
> hashmap.
>
> My main question is what to watch out for writing my own directive. 
> Especially in terms of efficiency and caching.
>
> Any pointers?
>
> Thanks,
> Lars





-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click