public static String replaceVariables ( final VariableResolver pResolver , final String pExpression , final String pOpen , final String pClose ) { final char [ ] open = pOpen . toCharArray ( ) ; final char [ ] close = pClose . toCharArray ( ) ; final StringBuilder out = new StringBuilder ( ) ; StringBuilder sb = new StringBuilder ( ) ; char [ ] last = null ; int wo = 0 ; int wc = 0 ; int level = 0 ; for ( char c : pExpression . toCharArray ( ) ) { if ( c == open [ wo ] ) { if ( wc > 0 ) { sb . append ( close , 0 , wc ) ; } wc = 0 ; wo ++ ; if ( open . length == wo ) { // found open if ( last == open ) { out . append ( open ) ; } level ++ ; out . append ( sb ) ; sb = new StringBuilder ( ) ; wo = 0 ; last = open ; } } else if ( c == close [ wc ] ) { if ( wo > 0 ) { sb . append ( open , 0 , wo ) ; } wo = 0 ; wc ++ ; if ( close . length == wc ) { // found close if ( last == open ) { final String variable = pResolver . get ( sb . toString ( ) ) ; if ( variable != null ) { out . append ( variable ) ; } else { out . append ( open ) ; out . append ( sb ) ; out . append ( close ) ; } } else { out . append ( sb ) ; out . append ( close ) ; } sb = new StringBuilder ( ) ; level -- ; wc = 0 ; last = close ; } } else { if ( wo > 0 ) { sb . append ( open , 0 , wo ) ; } if ( wc > 0 ) { sb . append ( close , 0 , wc ) ; } sb . append ( c ) ; wo = wc = 0 ; } } if ( wo > 0 ) { sb . append ( open , 0 , wo ) ; } if ( wc > 0 ) { sb . append ( close , 0 , wc ) ; } if ( level > 0 ) { out . append ( open ) ; } out . append ( sb ) ; return out . toString ( ) ; }
Substitute the variables in the given expression with the values from the resolver