public static String replaceVariablesInString ( final String str , TestContext context , boolean enableQuoting ) { StringBuffer newStr = new StringBuffer ( ) ; boolean isVarComplete ; StringBuffer variableNameBuf = new StringBuffer ( ) ; int startIndex = 0 ; int curIndex ; int searchIndex ; while ( ( searchIndex = str . indexOf ( Citrus . VARIABLE_PREFIX , startIndex ) ) != - 1 ) { int control = 0 ; isVarComplete = false ; curIndex = searchIndex + Citrus . VARIABLE_PREFIX . length ( ) ; while ( curIndex < str . length ( ) && ! isVarComplete ) { if ( str . indexOf ( Citrus . VARIABLE_PREFIX , curIndex ) == curIndex ) { control ++ ; } if ( ( ! Character . isJavaIdentifierPart ( str . charAt ( curIndex ) ) && ( str . charAt ( curIndex ) == Citrus . VARIABLE_SUFFIX . charAt ( 0 ) ) ) || ( curIndex + 1 == str . length ( ) ) ) { if ( control == 0 ) { isVarComplete = true ; } else { control -- ; } } if ( ! isVarComplete ) { variableNameBuf . append ( str . charAt ( curIndex ) ) ; } ++ curIndex ; } final String value = context . getVariable ( variableNameBuf . toString ( ) ) ; if ( value == null ) { throw new NoSuchVariableException ( "Variable: " + variableNameBuf . toString ( ) + " could not be found" ) ; } newStr . append ( str . substring ( startIndex , searchIndex ) ) ; if ( enableQuoting ) { newStr . append ( "'" + value + "'" ) ; } else { newStr . append ( value ) ; } startIndex = curIndex ; variableNameBuf = new StringBuffer ( ) ; isVarComplete = false ; } newStr . append ( str . substring ( startIndex ) ) ; return newStr . toString ( ) ; }
Replace all variable expression in a string with its respective value . Variable values are enclosed with quotes if enabled .