Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Placeholders in property files

 
Russle Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please could you tell me if a class exists that can take two property files and merge them together using Ant build script style placeholders? E.g.:

Dynamic property file:
item1.path={environment.start.path}\{location}\item1
item2.path={environment.start.path}\{location}\item2

Placeholder values property file:
environment.start.path=MyDevServer
location=uk

After the placeholders have been expanded, item1.path would be:
MyDevServer\uk\item1

item2.path would be:
MyDevServer\uk\item2

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why yes, there is such a class. In fact, I've written it myself last month

It's not that hard to write it yourself, using java.util.regex.Pattern. I've used a Properties object in the background, and the getProperty method is the only thing that requires some work.

matcher.find() will find the next occurrance of a template (see the pattern comment), or return false if there are no more occurrances.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know of any library that is all set up to do exactly this for you, but you could accomplish something like it relatively simply using the java.text.MessageFormat class.

[ Edit: Rob was too fast for me! ]
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of something. Instead of creating a new class and keeping an internal Properties object, you can subclass Properties itself. My code more or less remains the same, except replace every call to "propertiesXXX" to "super.XXX".

Also beware that you don't get circular references. For instance, the following will throw a StackOverflowError:
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After noticing that flaw in my own code, of course I fixed it:

The output of that example code I wrote before is now this:

It can't replace {Template 1} again, so it gives up and just shows the template name. It will also be faster if a template would be evaluated more than once; now it only evaluates it once per call to getProperty(String), and uses that same value for each occurrance within the same call.
 
Russle Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses. I'll try this out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic