org.mozilla.javascript.Wrapper Java Examples

The following examples show how to use org.mozilla.javascript.Wrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScriptablePageVariables.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Support setting parameter value by following methods:
 */
public void put( String name, Scriptable start, Object value )
{
	PageVariable variable = variables.get( name );
	if ( variable != null )
	{
		if ( value instanceof Wrapper )
		{
			value = ( (Wrapper) value ).unwrap( );
		}
		variable.setValue( value );
		return;
	}
	String errorMessage = "Report variable\"" + name + "\" does not exist";
	throw new JavaScriptException( errorMessage, "<unknown>", -1 );
}
 
Example #2
Source File: NativeMap.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean hasInstance(Scriptable value)
{
    if (!(value instanceof Wrapper))
        return false;
    Object instance = ((Wrapper)value).unwrap();
    return Map.class.isInstance(instance);
}
 
Example #3
Source File: ScriptAction.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Convert Action Parameter for Java usage
 * 
 * @param paramName
 *            parameter name
 * @param value
 *            value to convert
 * @return converted value
 */
@SuppressWarnings("synthetic-access")
public Serializable convertActionParamForRepo(String paramName, Serializable value)
{
    ParameterDefinition paramDef = actionDef.getParameterDefintion(paramName);

    if (paramDef != null && paramDef.getType().equals(DataTypeDefinition.QNAME))
    {
        if (value instanceof Wrapper)
        {
            // unwrap a Java object from a JavaScript wrapper
            // recursively call this method to convert the unwrapped value
            return convertActionParamForRepo(paramName, (Serializable) ((Wrapper) value).unwrap());
        }
        else
        {
            if (value instanceof String)
            {
                String stringQName = (String) value;
                if (stringQName.startsWith("{"))
                {
                    return QName.createQName(stringQName);
                   
                }
                else
                {
                    return QName.createQName(stringQName, namespaceService);
                }
            }
            else
            {
                return value;
            }
        }
    }
    else
    {
        return convertValueForRepo(value);
    }
}
 
Example #4
Source File: ReportParameters.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Support setting parameter value by following methods:
 * <li> params["a"] = params["b"]
 * <li> params["a"] = "value"
 */
public void put( String name, Scriptable start, Object value )
{
	DummyParameterAttribute attr = (DummyParameterAttribute) parameters.get( name );
	if ( attr == null )
	{
		attr = new DummyParameterAttribute( );
		parameters.put( name, attr );
	}
	if ( value instanceof ReportParameter )
	{
		ReportParameter scriptableParameter = (ReportParameter) value;
		Object paramValue = scriptableParameter.get( "value", this );
		String displayText = (String) scriptableParameter.get( "displayText",
				this );
		attr.setValue( paramValue );
		attr.setDisplayText( displayText );
		return;
	}

	if ( value instanceof Wrapper )
	{
		value = ( (Wrapper) value ).unwrap( );
	}

	attr.setValue( value );
	
}
 
Example #5
Source File: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String processTemplate(String template, NodeRef templateRef, ScriptableObject args)
{
    Object person = (Object)scope.get("person", scope);
    Object companyhome = (Object)scope.get("companyhome", scope);
    Object userhome = (Object)scope.get("userhome", scope);
    
    // build default model for the template processing
    Map<String, Object> model = this.services.getTemplateService().buildDefaultModel(
        (person.equals(UniqueTag.NOT_FOUND)) ? null : ((ScriptNode)((Wrapper)person).unwrap()).getNodeRef(),
        (companyhome.equals(UniqueTag.NOT_FOUND)) ? null : ((ScriptNode)((Wrapper)companyhome).unwrap()).getNodeRef(),
        (userhome.equals(UniqueTag.NOT_FOUND)) ? null : ((ScriptNode)((Wrapper)userhome).unwrap()).getNodeRef(),
        templateRef,
        null);
    
    // add the current node as either the document/space as appropriate
    if (this.getIsDocument())
    {
        model.put("document", this.nodeRef);
        model.put("space", getPrimaryParentAssoc().getParentRef());
    }
    else
    {
        model.put("space", this.nodeRef);
    }
    
    // add the supplied args to the 'args' root object
    if (args != null)
    {
        // we need to get all the keys to the properties provided
        // and convert them to a Map of QName to Serializable objects
        Object[] propIds = args.getIds();
        Map<String, String> templateArgs = new HashMap<String, String>(propIds.length);
        for (int i = 0; i < propIds.length; i++)
        {
            // work on each key in turn
            Object propId = propIds[i];
            
            // we are only interested in keys that are formed of Strings i.e. QName.toString()
            if (propId instanceof String)
            {
                // get the value out for the specified key - make sure it is Serializable
                Object value = args.get((String) propId, args);
                value = getValueConverter().convertValueForRepo((Serializable)value);
                if (value != null)
                {
                    templateArgs.put((String) propId, value.toString());
                }
            }
        }
        // add the args to the model as the 'args' root object
        model.put("args", templateArgs);
    }
    
    // execute template!
    // TODO: check that script modified nodes are reflected...
    return this.services.getTemplateService().processTemplateString(null, template, model);
}
 
Example #6
Source File: Bug467396Test.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
private Object unwrap(Object obj) {
    return obj instanceof Wrapper ? ((Wrapper) obj).unwrap() : obj;
}
 
Example #7
Source File: Bug467396Test.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Object unwrap(Object obj) {
    return obj instanceof Wrapper ? ((Wrapper) obj).unwrap() : obj;
}
 
Example #8
Source File: JavascriptEvalUtil.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Handles a Rhino script evaluation result, converting Javascript object
 * into equivalent Java objects if necessary.
 * @param inputObj Object returned by rhino engine.
 * @return If inputObj is a native Javascript object, its equivalent Java object 
 *   is returned; otherwise inputObj is returned
 */
public static Object convertJavascriptValue(Object inputObj)
{
	if ( inputObj instanceof Undefined )
	{
		return null;
	}
	if ( inputObj instanceof IdScriptableObject ) 
	{
		// Return type is possibly a Javascript native object
		// Convert to Java object with same value
		String jsClass = ((Scriptable) inputObj).getClassName();
		if ( "Date".equals(jsClass) ) 
		{
			return Context.toType( inputObj, Date.class );
		} 
		else if ( "Boolean".equals(jsClass)) 
		{
			return Boolean.valueOf( Context.toBoolean( inputObj ) );
		} 
		else if ( "Number".equals(jsClass)) 
		{
			return new Double(Context.toNumber(inputObj));
		} 
		else if( "String".equals(jsClass) )
		{
			return inputObj.toString();
		}
		else if ( "Array".equals( jsClass ) )
		{
			Object[] obj = new Object[(int) ( (NativeArray) inputObj ).getLength( )];
			for ( int i = 0; i < obj.length; i++ )
			{
				obj[i] = convertJavascriptValue( ( (NativeArray) inputObj ).get( i,
						null ) );
			}
			return obj;
		}
	}
	else if ( inputObj instanceof Wrapper )
	{
	    return ( (Wrapper) inputObj ).unwrap( );
	}
	else if ( inputObj instanceof Scriptable )
	{
		return ((Scriptable) inputObj).getDefaultValue( null );
	}
	
	return inputObj;
}
 
Example #9
Source File: JavaScriptEngine.java    From commons-bsf with Apache License 2.0 4 votes vote down vote up
/**
     * Return an object from an extension.
     * @param object Object on which to make the call (ignored).
     * @param method The name of the method to call.
     * @param args an array of arguments to be
     * passed to the extension, which may be either
     * Vectors of Nodes, or Strings.
     */
    public Object call(Object object, String method, Object[] args)
        throws BSFException {

        Object retval = null;
        Context cx;

        try {
            cx = Context.enter();

            // REMIND: convert arg list Vectors here?

            Object fun = global.get(method, global);
            // NOTE: Source and line arguments are nonsense in a call().
            //       Any way to make these arguments *sensible?
            if (fun == Scriptable.NOT_FOUND)
                throw new EvaluatorException("function " + method +
                                             " not found.", "none", 0);

            cx.setOptimizationLevel(-1);
            cx.setGeneratingDebug(false);
            cx.setGeneratingSource(false);
            cx.setOptimizationLevel(0);
            cx.setDebugger(null, null);
            
            retval =
                ((Function) fun).call(cx, global, global, args);
            
//                ScriptRuntime.call(cx, fun, global, args, global);

            if (retval instanceof Wrapper)
                retval = ((Wrapper) retval).unwrap();
        } 
        catch (Throwable t) {
            handleError(t);
        } 
        finally {
            Context.exit();
        }
        return retval;
    }