public static String evaluate ( CharSequence text ) { if ( text == null ) return null ; Matcher m = JS_PATTERN . matcher ( text ) ; StringBuffer ret = new StringBuffer ( ) ; final boolean isNas = scriptEngine . getFactory ( ) . getEngineName ( ) . toLowerCase ( ) . contains ( "nashorn" ) ; while ( m . find ( ) ) { String source = ( isNas ? "load(\"nashorn:mozilla_compat.js\");\nimportPackage(java.lang); " : "\nimportPackage(java.lang); " ) + m . group ( 1 ) ; try { Object obj = scriptEngine . eval ( source , bindings ) ; if ( obj != null ) { //log("Evaled [%s] --> [%s]", source, obj); m . appendReplacement ( ret , obj . toString ( ) ) ; } else { m . appendReplacement ( ret , "" ) ; } } catch ( Exception ex ) { ex . printStackTrace ( System . err ) ; throw new IllegalArgumentException ( "Failed to evaluate expression [" + text + "]" ) ; } } m . appendTail ( ret ) ; return ret . toString ( ) ; }
Evaluates JS expressions defines as configuration values