• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Balasubramanian Krishnaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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

 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
maybe like this

 
Balasubramanian Krishnaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic