Class BSHExpression
- java.lang.Object
-
- org.pentaho.reporting.engine.classic.core.function.AbstractExpression
-
- org.pentaho.reporting.engine.classic.core.modules.misc.beanshell.BSHExpression
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Expression
public class BSHExpression extends AbstractExpression
An expression that uses the BeanShell scripting framework to perform a scripted calculation. The expression itself is contained in a function calledObject getValue()
and this function is defined in theexpression
property. You have to overwrite the functiongetValue()
to begin and to end your expression, but you are free to add your own functions to the script. By default, base Java core and extension packages are imported for you. They are:java.lang
java.io
java.util
java.net
java.awt
java.awt.event
javax.swing
javax.swing.event
// you may import packages and classes or use the fully qualified name of the class import org.pentaho.reporting.engine.classic.core.*; String userdefinedFunction (String parameter, Date date) { return parameter + " - the current date is " + date); } // use simple java code to perform the expression. You may use all classes // available in your classpath as if you write "real" java code in your favourite // IDE. // See the www.beanshell.org site for more information ... // // A return value of type "Object" is alway implied ... getValue () { return userdefinedFunction ("Hello World :) ", new Date()); } - Author:
- Thomas Morgner
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
BSHHEADERFILE
The headerfile with the default initialisations.
-
Constructor Summary
Constructors Constructor Description BSHExpression()
default constructor, create a new BeanShellExpression.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected bsh.Interpreter
createInterpreter()
This method tries to create a new and fully initialized BeanShell interpreter.String
getExpression()
Sets the beanshell script as string.Expression
getInstance()
Return a new instance of this expression.Object
getValue()
Evaluates the defined expression.protected void
initializeInterpreter(bsh.Interpreter interpreter)
Initializes the bean-shell interpreter by executing the code in the BSHExpressionHeader.txt file.void
setExpression(String expression)
Sets the beanshell script that should be executed.-
Methods inherited from class org.pentaho.reporting.engine.classic.core.function.AbstractExpression
clone, getDataRow, getDependencyLevel, getName, getReportConfiguration, getResourceBundleFactory, getRuntime, isActive, isDeepTraversing, isPreserve, setActive, setDependencyLevel, setName, setPreserve, setRuntime
-
-
-
-
Field Detail
-
BSHHEADERFILE
public static final String BSHHEADERFILE
The headerfile with the default initialisations.- See Also:
- Constant Field Values
-
-
Method Detail
-
createInterpreter
protected bsh.Interpreter createInterpreter()
This method tries to create a new and fully initialized BeanShell interpreter.- Returns:
- the interpreter or null, if there was no way to create the interpreter.
-
initializeInterpreter
protected void initializeInterpreter(bsh.Interpreter interpreter) throws bsh.EvalError, IOException
Initializes the bean-shell interpreter by executing the code in the BSHExpressionHeader.txt file.- Parameters:
interpreter
- the interpreter that should be initialized.- Throws:
bsh.EvalError
- if an BeanShell-Error occured.IOException
- if the beanshell file could not be read.
-
getValue
public Object getValue()
Evaluates the defined expression. If an exception or an evaluation error occures, the evaluation returns null and the error is logged. The current datarow and a copy of the expressions properties are set to script-internal variables. Changes to the properties will not alter the expressions original properties and will be lost when the evaluation is finished. Expressions do not maintain a state and no assumptions about the order of evaluation can be made.- Returns:
- the evaluated value or null.
-
getInstance
public Expression getInstance()
Return a new instance of this expression. The copy is initialized and uses the same parameters as the original, but does not share any objects.- Specified by:
getInstance
in interfaceExpression
- Overrides:
getInstance
in classAbstractExpression
- Returns:
- a copy of this function.
-
getExpression
public String getExpression()
Sets the beanshell script as string.- Returns:
- the script.
-
setExpression
public void setExpression(String expression)
Sets the beanshell script that should be executed. The script should define a getValue() method which returns a single object.- Parameters:
expression
- the script.
-
-