Package org.eclipse.birt.core.script

# Class JavascriptEvalUtil

java.lang.Object
org.eclipse.birt.core.script.JavascriptEvalUtil

public class JavascriptEvalUtil extends Object
Utilities to faciliate the evaluation of Javascript expressions. Handles common evaluation tasks like exception handling, data type conversion and script caching

## Field Summary

Fields
Modifier and Type
Field
Description
`protected static Map`
`compiledScriptCache`
`protected static final int`
`SCRIPT_CACHE_SIZE`

## Constructor Summary

Constructors
Constructor
Description
`JavascriptEvalUtil()`

## Method Summary

Modifier and Type
Method
Description
`static Object`
`convertJavascriptValue(Object inputObj)`
Handles a Rhino script evaluation result, converting Javascript object into equivalent Java objects if necessary.
`static Object[]`
`convertToJavaObjects(Object[] args)`
`static Object`
`convertToJavascriptValue(Object value)`
If caller does not have a scope for evaluation, the caller can use this method to evaluate expression.
`static Object`
`convertToJavascriptValue(Object value, org.mozilla.javascript.Scriptable scope)`
Creates Javascript native wrapper for Java objects, if necessary.
`static String`
`evaluateJsConstants(String js)`
`static Object`
`evaluateRawScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo)`
This method will not convert the data of return value, so it might the Java data type or that of Java Script.
`static Object`
`evaluateScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo)`
Evaluates Javascript expression and return its result, doing the necessary Javascript -> Java data type conversion if necessary
`protected static org.mozilla.javascript.Script`
`getCompiledScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo)`
Gets a compiled script, using and updating the script cache if necessary
`static String`
`transformToJsConstants(String s)`
This method transforms a string to JS string constants.
`static String`
`transformToJsExpression(String s)`
`static BirtException`
`wrapRhinoException(org.mozilla.javascript.RhinoException e, String scriptText, String source, int lineNo)`
Converts Rhino exception (a runtime exception) to BirtException

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### SCRIPT_CACHE_SIZE

protected static final int SCRIPT_CACHE_SIZE

See Also:
  - Constant Field Values

### compiledScriptCache

protected static Map compiledScriptCache

## Constructor Details

### JavascriptEvalUtil

public JavascriptEvalUtil()

## Method Details

### evaluateRawScript

public static Object evaluateRawScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo) throws BirtException
This method will not convert the data of return value, so it might the Java data type or that of Java Script.

Parameters:
  `cx` -
  `scope` -
  `scriptText` -
  `source` -
  `lineNo` -
Returns:
  the evaluated value
Throws:
  `BirtException`

### evaluateScript

public static Object evaluateScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo) throws BirtException
Evaluates Javascript expression and return its result, doing the necessary Javascript -> Java data type conversion if necessary

Parameters:
  `cx` - Javascript context. If null, current thread's context is used
  `scope` - Javascript scope to evaluate script in
  `scriptText` - text of Javascript expression
  `source` - descriptive text of script source (for error reporting)
  `lineNo` - line number of script in it source
Returns:
  Evaluation result.
Throws:
  `BirtException` - If evaluation failed

### getCompiledScript

protected static org.mozilla.javascript.Script getCompiledScript(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, String scriptText, String source, int lineNo)
Gets a compiled script, using and updating the script cache if necessary

### convertToJavascriptValue

public static Object convertToJavascriptValue(Object value, org.mozilla.javascript.Scriptable scope)
Creates Javascript native wrapper for Java objects, if necessary. This method currently only wraps Date/time objects. Rhino engine natively handles wrapping String, Number and Boolean objects.

Parameters:
  `value` - Java object to convert from

### convertToJavascriptValue

public static Object convertToJavascriptValue(Object value)
If caller does not have a scope for evaluation, the caller can use this method to evaluate expression. But if caller has its own scope which can be used, the better way is call the method of convertToJavascriptValue( Object value, Scriptable scope ).

Parameters:
  `value` -
Returns:

### convertJavascriptValue

public static Object convertJavascriptValue(Object inputObj)
Handles a Rhino script evaluation result, converting Javascript object into equivalent Java objects if necessary.

Parameters:
  `inputObj` - Object returned by rhino engine.
Returns:
  If inputObj is a native Javascript object, its equivalent Java object is returned; otherwise inputObj is returned

### wrapRhinoException

public static BirtException wrapRhinoException(org.mozilla.javascript.RhinoException e, String scriptText, String source, int lineNo)
Converts Rhino exception (a runtime exception) to BirtException

Parameters:
  `e` - Rhino exception
  `scriptText` - Javascript code which resulted in the exception (for error reporting purpose)
  `source` - description of the source script. If null, get this info from Rhino exception
  `lineNo` - lineNo of error location

### convertToJavaObjects

public static Object[] convertToJavaObjects(Object[] args)

Parameters:
  `args` -
Returns:

### transformToJsConstants

public static String transformToJsConstants(String s)
This method transforms a string to JS string constants.

Parameters:
  `s` -
Returns:

### transformToJsExpression

public static String transformToJsExpression(String s)

### evaluateJsConstants

public static String evaluateJsConstants(String js)