Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using properties within the properties file

I apologize for the title.. i couldn't find a better way to explain the situation.

I use to load properties file using a Property class as described in the URL http://www.exampledepot.com/egs/java.util/Props.html

my question is can I use properties within that properties file ?

example:

test.properties

 url.main="http://mysite.com"
    url.games={url.main}/games
    url.images={url.main}/images
    .
    .
    .

is that possible with some other syntax?

thanks

like image 751
ufk Avatar asked Jan 15 '12 12:01

ufk


4 Answers

Apache Commons Configuration provides for this: http://commons.apache.org/configuration/

Simple example code for loading the configuration file:

Configuration config = new PropertiesConfiguration("config.properties");

You can have 'variable interpolated' properties, as described here http://commons.apache.org/configuration/userguide/howto_basicfeatures.html#Variable_Interpolation

application.name = Killer App
application.version = 1.6.2

application.title = ${application.name} ${application.version}

And it also allows you to include other configuration files while you're at it:

include = colors.properties
include = sizes.properties

Besides a whole range of other features.

like image 121
craigmj Avatar answered Sep 18 '22 12:09

craigmj


Never seen that before. You could make your own preprocessor of course. As long as the referenced property occurs before any references to it, you should be able to implement it using some regular expressions/string replacement. But: I would not recommend this method.

Better resolve the duplication by defining different properties:

  1. change: url.games={url.main}/games into url.games_extension=/games
  2. prepend: url.main to url.games_extension to get the full games url in your application code.
like image 24
The Nail Avatar answered Sep 19 '22 12:09

The Nail


I wrote my own Configuration library that supports variable expansion in properties files, have a look and see if it provides what you need. An article I wrote to introduce the feature is here.

like image 35
Luigi R. Viggiano Avatar answered Sep 18 '22 12:09

Luigi R. Viggiano


There is no direct way to substitute the property value within the property file/object but you may replace property value once you read via getProperty() method. To produce concatenated messages - have a look at MessageFormat class.

String baseValue=prop.getProperty("url.main");
like image 36
KV Prajapati Avatar answered Sep 19 '22 12:09

KV Prajapati