package net.xoetrope.optional.scripts.beanshell;
import bsh.EvalError;
import net.xoetrope.xui.XPage;
import net.xoetrope.xui.XPageManager;
import net.xoetrope.xui.XProject;
import bsh.Interpreter;
import net.xoetrope.optional.scripts.ScriptEngine;
import net.xoetrope.optional.scripts.ScriptException;
/**
* Evaluates a script attribute by passing the scripts
* following the prefix 'script:' to the ScriptEngine.
* The scripts can be an a function that was predifined
* in the page Scripts tag or a script that is just being
* defined.
* eg. <code>script:myFunction();</code> given that myFunction
* was define in the page Scripts tag.
* eg. <code>script:out.println('Hello World');</code> the script
* is just being defined.
* <p>Copyright (c) Xoetrope Ltd., 2002-2007</p>
* <p>License: see license.txt</p>
*/
public class BeanShellScriptEngine implements ScriptEngine
{
private XProject currentProject;
private XPageManager pageMgr;
private Interpreter interpreter;
/**
* Creates a new instance of JavaScriptEngine
*/
public BeanShellScriptEngine()
{
interpreter = new Interpreter(); // Construct an interpreter
}
/**
* Set the owning project
* @param proj the current project
*/
public void setProject( XProject proj )
{
currentProject = proj;
pageMgr = currentProject.getPageManager();
}
/**
* Returns the scripts for this page
*/
public String getPageScript()
{
XPage currentPage = (XPage)pageMgr.getCurrentPage( null );
return (String)currentPage.getAttribute( "source", "scripts" );
}
/**
* Executes a given script and returns the result.
* @param script the script to be executed for the current page
*/
public Object executeScript( String script ) throws ScriptException
{
try {
return interpreter.eval( script );
}
catch ( EvalError ex )
{
ex.printStackTrace();
}
return null;
}
}