| Author |
Placeholders in property files
|
Russle Smith
Greenhorn
Joined: Mar 20, 2009
Posts: 2
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
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! ]
|
[Jess in Action][AskingGoodQuestions]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
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
Joined: Mar 20, 2009
Posts: 2
|
|
|
Thanks for your responses. I'll try this out.
|
 |
 |
|
|
subject: Placeholders in property files
|
|
|