private static String resolveVariableExpression ( final String value , final int last , final JexlEngine jexl ) { final int lastIndex = value . lastIndexOf ( "$(" , last ) ; if ( lastIndex == - 1 ) { return value ; } // Want to check that everything is well formed, and that // we properly capture $( ...(...)...). int bracketCount = 0 ; int nextClosed = lastIndex + 2 ; for ( ; nextClosed < value . length ( ) ; ++ nextClosed ) { if ( value . charAt ( nextClosed ) == ' ' ) { bracketCount ++ ; } else if ( value . charAt ( nextClosed ) == ' ' ) { bracketCount -- ; if ( bracketCount == - 1 ) { break ; } } } if ( nextClosed == value . length ( ) ) { throw new IllegalArgumentException ( "Expression " + value + " not well formed." ) ; } final String innerExpression = value . substring ( lastIndex + 2 , nextClosed ) ; Object result = null ; try { final Expression e = jexl . createExpression ( innerExpression ) ; result = e . evaluate ( new MapContext ( ) ) ; } catch ( final JexlException e ) { throw new IllegalArgumentException ( "Expression " + value + " not well formed. " + e . getMessage ( ) , e ) ; } if ( result == null ) { // for backward compatibility it is best to return value return value ; } final String newValue = value . substring ( 0 , lastIndex ) + result . toString ( ) + value . substring ( nextClosed + 1 ) ; return resolveVariableExpression ( newValue , lastIndex , jexl ) ; }
Function that looks for expressions to parse . It parses backwards to capture embedded expressions