/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2005 - 2009 Pentaho Corporation, Object Refinery Limited and Contributors. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.extensions.modules.rhino;
import java.io.Serializable;
import org.pentaho.reporting.engine.classic.core.function.AbstractExpression;
import org.pentaho.reporting.engine.classic.core.states.LegacyDataRowWrapper;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
/**
* An expression that uses the Rhino scripting framework to perform a scripted
* calculation. The expression itself is contained in a function called
* <p/>
* <code>Object getValue()</code>
* <p/>
* and this function is defined in the <code>expression</code> property. You have to
* overwrite the function <code>getValue()</code> to begin and to end your expression, but
* you are free to add your own functions to the script.
* <p/>
*
* @author Thomas Morgner
*/
public class RhinoExpression extends AbstractExpression implements Serializable
{
private String expression;
/**
* default constructor, create a new BeanShellExpression.
*/
public RhinoExpression()
{
}
protected void initializeScope (final Scriptable scope)
{
final LegacyDataRowWrapper dataRowWrapper = new LegacyDataRowWrapper();
final Object wrappedDataRow = Context.javaToJS(dataRowWrapper, scope);
ScriptableObject.putProperty(scope, "dataRow", wrappedDataRow);
}
/**
* 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.
* <p/>
* Expressions do not maintain a state and no assumptions about the order of evaluation
* can be made.
*
* @return the evaluated value or null.
*/
public Object getValue ()
{
if (expression == null)
{
return null;
}
try
{
final Context context = Context.enter();
final Scriptable scope = context.initStandardObjects();
initializeScope(scope);
return context.evaluateString(scope, expression, getName(), 1, null);
}
finally
{
Context.exit();
}
}
public String getExpression ()
{
return expression;
}
public void setExpression (final String expression)
{
this.expression = expression;
}
}