buffer.append( getResourceAsString( resource ) );
}
}
List<String> outputNames = new ArrayList<String>();
JavascriptAction jscriptAction = (JavascriptAction) getActionDefinition();
IActionOutput[] actionOutputs = jscriptAction.getOutputs();
if ( actionOutputs.length == 1 ) {
String outputName = actionOutputs[0].getName();
outputNames.add( outputName );
} else {
if ( oldStyleOutputs ) {
int i = 1;
while ( true ) {
if ( jscriptAction.getInput( "output" + i ) != ActionInputConstant.NULL_INPUT ) { //$NON-NLS-1$
outputNames.add( jscriptAction.getInput( "output" + i ).getStringValue() ); //$NON-NLS-1$
} else {
break;
}
i++;
}
} else {
for ( IActionOutput element : actionOutputs ) {
outputNames.add( element.getName() );
}
}
}
boolean success = false;
try {
String script = jscriptAction.getScript().getStringValue();
if ( script == null ) {
error( Messages.getInstance().getErrorString( "JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName() ) ); //$NON-NLS-1$
} else {
buffer.append( script );
script = buffer.toString();
if ( ComponentBase.debug ) {
debug( "script=" + script ); //$NON-NLS-1$
}
try {
ScriptableObject scriptable = new RhinoScriptable();
// initialize the standard javascript objects
Scriptable scope = cx.initStandardObjects( scriptable );
Object resultObject = executeScript( scriptable, scope, script, cx );
if ( oldStyleOutputs ) {
if ( resultObject instanceof org.mozilla.javascript.NativeArray ) {
// we need to convert this to an ArrayList
NativeArray jsArray = (NativeArray) resultObject;
int length = (int) jsArray.getLength();
for ( int i = 0; i < length; i++ ) {
Object value = jsArray.get( i, scriptable );
if ( i < outputNames.size() ) {
jscriptAction.getOutput( outputNames.get( i ).toString() )
.setValue( convertWrappedJavaObject( value ) );
} else {
break;
}
}
} else {
jscriptAction.getOutput( outputNames.get( 0 ).toString() ).setValue(
convertWrappedJavaObject( resultObject ) );
}
} else {
if ( ( outputNames.size() == 1 ) && ( resultObject != null ) ) {
jscriptAction.getOutput( outputNames.get( 0 ).toString() ).setValue(
convertWrappedJavaObject( resultObject ) );
} else {
List<String> setOutputs = new ArrayList<String>( outputNames.size() );
Object[] ids = ScriptableObject.getPropertyIds( scope );
for ( Object element : ids ) {
int idx = outputNames.indexOf( element.toString() );
if ( idx >= 0 ) {
jscriptAction.getOutput( outputNames.get( idx ).toString() ).setValue(
convertWrappedJavaObject( ScriptableObject.getProperty( scope, (String) element ) ) );
setOutputs.add( outputNames.get( idx ) );
}
}
// Javascript Component defined an output, but
// didn't set it to anything.
// So, set it to null.
if ( setOutputs.size() != outputNames.size() ) {
for ( int i = 0; i < outputNames.size(); i++ ) {
if ( setOutputs.indexOf( outputNames.get( i ) ) < 0 ) {
// An output that wasn't set in the
// javascript component
jscriptAction.getOutput( outputNames.get( i ).toString() ).setValue( null );
}
}
}
}