• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to reference already defined property as the value for another key in the properties file.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I need to transfer the logic of creating a final string from code to a properties file. This will help if there are any changes to the logic in future with the change of only the properties file and no code change.

I want to refer a property key in another property. Something like

Key1 = value1 (value1 is not constant but a regex and hence Key1 is calculated at runtime)
Key2 = value2 (value2 is not constant but a regex and hence Key2 is calculated at runtime)
Result = Key1 + Key2

I am ready to use Regex, Bean Shell etc.

Please advice.

Thanks
Bala

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ant and spring framework do soemthing like this, where they use the "${propertyKey}" notation. where enclosing something in "${" and "}" means to try to find a property in the properties file having the key of the thing between the ${ and }

I guess you could create a subclass of the java.util.Properties and override the getProperty() method to parse out things between some delimiter, like ${ and } and recursively try to look for a property of that key name and substitute its value.
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe like this

 
Balasubramanian Krishnaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Travis. This is the appropriate solution.
Though not very relevant to Java Ranch's "General Java" forum, I was successful experimenting with Bean Shell and come up with a solution (just in case).

Prperties File

Key1 = regex
Key2 = regex
Result = Key1 + \"_\" + Key2

Code Snippet

Properties properties = new Properties();
try {
properties.load(new FileInputStream("C:/Test/test.properties"));
String returnValue;
String finalValue = properties.getProperty("Result");
Interpreter i = new Interpreter();
try {
i.set("Key1", someValue)
i.set("Key2", someValue)
returnValue = (String)i.eval(finalValue);
}
catch (EvalError e) {
}
}
catch (IOException e) {
}

Thanks
Bala

 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic