wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to reference already defined property as the value for another key in the properties file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to reference already defined property as the value for another key in the properties file." Watch "how to reference already defined property as the value for another key in the properties file." New topic
Author

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

Balasubramanian Krishnaswamy
Greenhorn

Joined: Jun 26, 2007
Posts: 24
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

Joined: Jun 06, 2006
Posts: 161
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.


Error: Keyboard not attached. Press F1 to continue.
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
maybe like this

Balasubramanian Krishnaswamy
Greenhorn

Joined: Jun 26, 2007
Posts: 24
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

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to reference already defined property as the value for another key in the properties file.
 
Similar Threads
Hibernate mapping HashMap to row
Converting property file to object model
I am close to submit - question about suncertify.properties
Properties file issue
How to Handle Properties