public static int substituteSystemValues(Properties props){
  int substitutions=0;
  Properties originalProps=new Properties();
  originalProps.putAll(props);
  Enumeration<Object> en=props.keys();
  while (en.hasMoreElements()) {
    String key=(String)en.nextElement();
    String value=props.getProperty(key);
    if (value == null)     continue;
    StringBuffer newValue=new StringBuffer();
    StringBuffer expression=null;
    StringBuffer name=null;
    ParseState state=ParseState.NONE;
    for (int i=0; i < value.length(); i++) {
      char c=value.charAt(i);
switch (state) {
case NONE:
        if (c == '$') {
          state=ParseState.DOLLAR;
          expression=new StringBuffer();
          expression.append(c);
        }
 else         newValue.append(c);
      break;
case DOLLAR:
    expression.append(c);
  if (c == '{') {
    state=ParseState.LBRACKET;
  }
 else {
    state=ParseState.NONE;
    newValue.append(expression.toString());
    expression=null;
  }
break;
case LBRACKET:
expression.append(c);
if (Character.isLetterOrDigit(c)) {
state=ParseState.NAME;
name=new StringBuffer();
name.append(c);
}
 else {
state=ParseState.NONE;
newValue.append(expression.toString());
expression=null;
}
break;
case NAME:
expression.append(c);
if (c == '}') {
String embeddedKey=name.toString();
if (embeddedKey.length() > 0) {
String originalValue=originalProps.getProperty(embeddedKey);
String systemValue=System.getProperty(embeddedKey);
if (originalValue != null) {
expression=new StringBuffer(originalValue);
substitutions++;
}
 else if (systemValue != null) {
expression=new StringBuffer(systemValue);
substitutions++;
}
}
name=null;
state=ParseState.NONE;
newValue.append(expression.toString());
expression=null;
}
 else {
name.append(c);
}
break;
}
}
if (expression != null) {
newValue.append(expression);
}
props.setProperty(key,newValue.toString());
}
return substitutions;
}

public static int substituteSystemValues ( Properties props ) { int substitutions = 0 ; Properties originalProps = new Properties ( ) ; originalProps . putAll ( props ) ; Enumeration < Object > en = props . keys ( ) ; while ( en . hasMoreElements ( ) ) { String key = ( String ) en . nextElement ( ) ; String value = props . getProperty ( key ) ; if ( value == null ) continue ; StringBuffer newValue = new StringBuffer ( ) ; StringBuffer expression = null ; StringBuffer name = null ; ParseState state = ParseState . NONE ; for ( int i = 0 ; i < value . length ( ) ; i ++ ) { char c = value . charAt ( i ) ; switch ( state ) { case NONE : if ( c == '$' ) { state = ParseState . DOLLAR ; expression = new StringBuffer ( ) ; expression . append ( c ) ; } else newValue . append ( c ) ; break ; case DOLLAR : expression . append ( c ) ; if ( c == '{' ) { state = ParseState . LBRACKET ; } else { state = ParseState . NONE ; newValue . append ( expression . toString ( ) ) ; expression = null ; } break ; case LBRACKET : expression . append ( c ) ; if ( Character . isLetterOrDigit ( c ) ) { state = ParseState . NAME ; name = new StringBuffer ( ) ; name . append ( c ) ; } else { state = ParseState . NONE ; newValue . append ( expression . toString ( ) ) ; expression = null ; } break ; case NAME : expression . append ( c ) ; if ( c == '}' ) { String embeddedKey = name . toString ( ) ; if ( embeddedKey . length ( ) > 0 ) { String originalValue = originalProps . getProperty ( embeddedKey ) ; String systemValue = System . getProperty ( embeddedKey ) ; if ( originalValue != null ) { expression = new StringBuffer ( originalValue ) ; substitutions ++ ; } else if ( systemValue != null ) { expression = new StringBuffer ( systemValue ) ; substitutions ++ ; } } name = null ; state = ParseState . NONE ; newValue . append ( expression . toString ( ) ) ; expression = null ; } else { name . append ( c ) ; } break ; } } if ( expression != null ) { newValue . append ( expression ) ; } props . setProperty ( key , newValue . toString ( ) ) ; } return substitutions ; }
scan properties instance values replacing any expression of the form $ { name } where ' name ' is a key in system . properties * or * a local property in the same file , where local property substitutions take priority over system property names . if there is no such value the expression is left as is .
