File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Placeholders in property files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Placeholders in property files" Watch "Placeholders in property files" New topic
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: 19544
    
  16

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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: 19544
    
  16

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: 19544
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Placeholders in property files
 
Similar Threads
Self Join Query Help
how to create applet pop menu? who can provide a good example?
how to create applet pop menu? who can provide a good example?
cewolf data points not visible
how to create Jcombobox with fixed width?