7

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

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
ufk
  • 30,912
  • 70
  • 235
  • 386
  • 1
    Other suggestions on http://stackoverflow.com/questions/872272/java-properties-file-how-to-reference-already-defined-property-later-dir-defa – Kennet Jan 15 '12 at 12:22

5 Answers5

7

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.

craigmj
  • 4,827
  • 2
  • 18
  • 22
4

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.
The Nail
  • 8,355
  • 2
  • 35
  • 48
2

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.

Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
1

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");
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

I've done something like this and it isn't that difficult, you simply subclass the Properties class and implement your own getProperty method, check for a pattern and replace it if necessary.

//this makes the pattern ${sometext}
static public final String JAVA_CONFIG_VARIABLE = "\\$\\{(.*)\\}";

@Override
public String getProperty(String key)
{
    String val = super.getProperty(key);
    if( val != null && val.indexOf("${") != -1 )
    {
        //we have at least one replacable parm, let's replace it
        val = replaceParms(val);
    }
    return val;
 }

 public final String replaceParms(String in)
 {
     if(in == null) return null;
     //this could be precompiled as Pattern is supposed to be thread safe
     Pattern pattern = Pattern.compile(JAVA_CONFIG_VARIABLE);
     Matcher matcher = pattern.matcher(in);
     StringBuffer buf = new StringBuffer();
     while (matcher.find())
     {
         String replaceStr = matcher.group(1);
         String prop = getProperty(replaceStr);   
         //if it isn't in our properties file, check if it is a system property
         if (prop == null )
             prop = System.getProperty(replaceStr);
         if( prop == null )
         {
            System.out.printf("Failed to find property '%s' for '%s'\n", replaceStr, in);
         }
         else
         {
           matcher.appendReplacement(buf, prop);
         } 
     }
     matcher.appendTail(buf);
     String result = buf.toString();
     return result;
 }
Jere
  • 581
  • 2
  • 3