• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Placeholders in property files

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your responses. I'll try this out.
 
Would you like to try a free sample? Today we are featuring tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic