cl = ClassLoaderFactory.getURLClassLoader( metaData );
sourceUrl = metaData.getSourceURL();
}
catch ( java.net.MalformedURLException mfu )
{
throw new ScriptFrameworkErrorException(
mfu.getMessage(), null,
metaData.getLanguageName(), metaData.getLanguage(),
ScriptFrameworkErrorType.MALFORMED_URL );
}
catch ( com.sun.star.script.framework.provider.NoSuitableClassLoaderException nsc )
{
// Framework error
throw new ScriptFrameworkErrorException(
nsc.getMessage(), null,
metaData.getLanguageName(), metaData.getLanguage(),
ScriptFrameworkErrorType.UNKNOWN );
}
Context ctxt = null;
try
{
String editorURL = sourceUrl.toString();
Object result = null;
String source = null;
ScriptEditorForJavaScript editor =
ScriptEditorForJavaScript.getEditor(
metaData.getSourceURL() );
if (editor != null)
{
editorURL = editor.getURL();
result = editor.execute();
if ( result != null &&
result.getClass().getName().equals( "org.mozilla.javascript.Undefined" ) )
{
// Always return a string
// TODO revisit
return Context.toString( result );
}
}
if (editor != null && editor.isModified() == true)
{
LogUtils.DEBUG("GOT A MODIFIED SOURCE");
source = editor.getText();
}
else
{
metaData.loadSource();
source = metaData.getSource();
}
if ( source == null || source.length() == 0 ) {
throw new ScriptFrameworkErrorException(
"Failed to read source data for script", null,
metaData.getLanguageName(), metaData.getLanguage(),
ScriptFrameworkErrorType.UNKNOWN );
}
/* Set the context ClassLoader on the current thread to
be our custom ClassLoader. This is the suggested method
for setting up a ClassLoader to be used by the Rhino
interpreter
*/
if (cl != null) {
Thread.currentThread().setContextClassLoader(cl);
}
// Initialize a Rhino Context object
ctxt = Context.enter();
ctxt.setLanguageVersion(Context.VERSION_1_8);
ctxt.setOptimizationLevel(9);
/* The ImporterTopLevel ensures that importClass and
importPackage statements work in Javascript scripts
Make the XScriptContext available as a global variable
to the script
*/
ImporterTopLevel scope = new ImporterTopLevel(ctxt);
Scriptable jsCtxt = Context.toObject(
ScriptContext.createContext(
m_xModel, m_xInvocContext, m_xContext,
m_xMultiComponentFactory), scope);
scope.put("XSCRIPTCONTEXT", scope, jsCtxt);
Scriptable jsArgs = Context.toObject(params, scope);
scope.put("ARGUMENTS", scope, jsArgs);
result = ctxt.evaluateString(scope,
source, "<stdin>", 1, null);
result = ctxt.toString(result);
return result;
}
catch (JavaScriptException jse) {
LogUtils.DEBUG( "Caught JavaScriptException exception for JavaScript type = " + jse.getClass() );
String message = jse.getMessage();
int lineNo = jse.lineNumber();
Object wrap = jse.getValue();
LogUtils.DEBUG( "\t message " + message );
LogUtils.DEBUG( "\t wrapped type " + wrap.getClass() );
LogUtils.DEBUG( "\t wrapped toString " + wrap.toString() );
ScriptExceptionRaisedException se = new
ScriptExceptionRaisedException( message );
se.lineNum = lineNo;
se.language = "JavaScript";
se.scriptName = metaData.getLanguageName();
se.exceptionType = wrap.getClass().getName();
se.language = metaData.getLanguage();
LogUtils.DEBUG( "ExceptionRaised exception " );
LogUtils.DEBUG( "\t message " + se.getMessage() );
LogUtils.DEBUG( "\t lineNum " + se.lineNum );
LogUtils.DEBUG( "\t language " + se.language );
LogUtils.DEBUG( "\t scriptName " + se.scriptName );
raiseEditor( se.lineNum );
throw new InvocationTargetException( "JavaScript uncaught exception" + metaData.getLanguageName(), null, se );
}
catch (Exception ex) {
LogUtils.DEBUG("Caught Exception " + ex );
LogUtils.DEBUG("rethrowing as ScriptFramework error" );
throw new ScriptFrameworkErrorException(
ex.getMessage(), null,
metaData.getLanguageName(), metaData.getLanguage(),
ScriptFrameworkErrorType.UNKNOWN );
}
finally {