static Object jsToJava(final Object obj) { if (obj instanceof Scriptable) { final Object javaObj = Context.jsToJava(obj, Object.class); if (javaObj instanceof NativeArray) { return new ScriptableList((NativeArray) javaObj); } if (javaObj instanceof Scriptable) { return new ScriptableMap((Scriptable) javaObj); } return javaObj; } if (obj instanceof Wrapper) { return ((Wrapper) obj).unwrap(); } if (obj == Scriptable.NOT_FOUND) { return null; } return obj; }
/** * Converts JavaScript object to Java object, e.g. HostedJavaObject into wrapped Java object. * * @param jsObj JavaScript object to be converted. * @return Converted Java object. */ public static Object jsToJava(Object jsObj) { if (jsObj instanceof Wrapper) { Wrapper njb = (Wrapper) jsObj; if (njb instanceof NativeJavaClass) { return njb; } Object obj = njb.unwrap(); if (obj instanceof Number || obj instanceof String || obj instanceof Boolean || obj instanceof Character) { return njb; } else { return obj; } } else { return jsObj; } }
/** * The bindings function takes a JavaScript scope object * of type ExternalScriptable and returns the underlying Bindings * instance. * * var page = scope(pageBindings); * with (page) { * // code that uses page scope * } * var b = bindings(page); * // operate on bindings here. */ public static Object bindings(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length == 1) { Object arg = args[0]; if (arg instanceof Wrapper) { arg = ((Wrapper)arg).unwrap(); } if (arg instanceof ExternalScriptable) { ScriptContext ctx = ((ExternalScriptable)arg).getContext(); Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE); return Context.javaToJS(bind, ScriptableObject.getTopLevelScope(thisObj)); } } return Context.getUndefinedValue(); }
/** * The scope function creates a new JavaScript scope object * with given Bindings object as backing store. This can be used * to create a script scope based on arbitrary Bindings instance. * For example, in webapp scenario, a 'page' level Bindings instance * may be wrapped as a scope and code can be run in JavaScripe 'with' * statement: * * var page = scope(pageBindings); * with (page) { * // code that uses page scope * } */ public static Object scope(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length == 1) { Object arg = args[0]; if (arg instanceof Wrapper) { arg = ((Wrapper)arg).unwrap(); } if (arg instanceof Bindings) { ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings((Bindings)arg, ScriptContext.ENGINE_SCOPE); Scriptable res = new ExternalScriptable(ctx); res.setPrototype(ScriptableObject.getObjectPrototype(thisObj)); res.setParentScope(ScriptableObject.getTopLevelScope(thisObj)); return res; } } return Context.getUndefinedValue(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public Scriptable construct(Context cx, Scriptable scope, Object[] args) throws RhinoException { if (args.length == 2) { Class clazz = null; Object obj1 = args[0]; if (obj1 instanceof Wrapper) { Object o = ((Wrapper) obj1).unwrap(); if (o instanceof Class && ((Class) o).isInterface()) { clazz = (Class) o; } } else if (obj1 instanceof Class) { if (((Class) obj1).isInterface()) { clazz = (Class) obj1; } } if (clazz == null) { throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class"); } Scriptable topLevel = ScriptableObject.getTopLevelScope(scope); return Context.toObject(engine.getInterface(args[1], clazz), topLevel); } else { throw Context.reportRuntimeError("JavaAdapter requires two arguments"); } }
public OMElement fromScript(Object o) { if (!(o instanceof XMLObject)) { return super.fromScript(o); } // TODO: E4X Bug? Shouldn't need this copy, but without it the outer element gets lost. See Mozilla bugzilla 361722 Scriptable jsXML = (Scriptable) ScriptableObject.callMethod((Scriptable) o, "copy", new Object[0]); Wrapper wrapper = (Wrapper) ScriptableObject.callMethod((XMLObject)jsXML, "getXmlObject", new Object[0]); XmlObject xmlObject = (XmlObject)wrapper.unwrap(); try { StAXOMBuilder builder = new StAXOMBuilder(xmlObject.newInputStream()); OMElement omElement = builder.getDocumentElement(); return omElement; } catch (XMLStreamException e) { throw new RuntimeException(e); } }
/** * 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 ); }
/** * The bindings function takes a JavaScript scope object * of type ExternalScriptable and returns the underlying Bindings * instance. * * var page = scope(pageBindings); * with (page) { * // code that uses page scope * } * var b = bindings(page); * // operate on bindings here. */ public static Object bindings(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length == 1) { Object arg = args[0]; if (arg instanceof Wrapper) { arg = ((Wrapper)arg).unwrap(); } if (arg instanceof ExternalScriptable) { ScriptContext ctx = ((ExternalScriptable)arg).getContext(); Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE); return Context.javaToJS(bind, ScriptableObject.getTopLevelScope(thisObj)); } } return cx.getUndefinedValue(); }
/** * The scope function creates a new JavaScript scope object * with given Bindings object as backing store. This can be used * to create a script scope based on arbitrary Bindings instance. * For example, in webapp scenario, a 'page' level Bindings instance * may be wrapped as a scope and code can be run in JavaScripe 'with' * statement: * * var page = scope(pageBindings); * with (page) { * // code that uses page scope * } */ public static Object scope(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length == 1) { Object arg = args[0]; if (arg instanceof Wrapper) { arg = ((Wrapper)arg).unwrap(); } if (arg instanceof Bindings) { ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings((Bindings)arg, ScriptContext.ENGINE_SCOPE); Scriptable res = new ExternalScriptable(ctx); res.setPrototype(ScriptableObject.getObjectPrototype(thisObj)); res.setParentScope(ScriptableObject.getTopLevelScope(thisObj)); return res; } } return cx.getUndefinedValue(); }
public Scriptable construct(Context cx, Scriptable scope, Object[] args) throws RhinoException { if (args.length == 2) { Class<?> clazz = null; Object obj1 = args[0]; if (obj1 instanceof Wrapper) { Object o = ((Wrapper)obj1).unwrap(); if (o instanceof Class && ((Class)o).isInterface()) { clazz = (Class) o; } } else if (obj1 instanceof Class) { if (((Class)obj1).isInterface()) { clazz = (Class) obj1; } } if (clazz == null) { throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class"); } Scriptable topLevel = ScriptableObject.getTopLevelScope(scope); return cx.toObject(engine.getInterface(args[1], clazz), topLevel); } else { throw Context.reportRuntimeError("JavaAdapter requires two arguments"); } }
/** * {@inheritDoc} */ @Override public Object convertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final Object result; if (value instanceof Wrapper) { final Object unwrapped = ((Wrapper) value).unwrap(); if (!expectedClass.isInstance(unwrapped)) { result = globalDelegate.convertValueForJava(unwrapped, expectedClass); } else { result = unwrapped; } } else { result = null; } return result; }
public boolean hasInstance(Scriptable value) { if (!(value instanceof Wrapper)) return false; Object instance = ((Wrapper)value).unwrap(); return Map.class.isInstance(instance); }
/** * 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); } }
private Object evalJs(ContextFactory contextFactory, String code) { Object result = contextFactory.call(new ContextEval(code)); System.out.println(code); System.out.println("\t" + (result != null ? result.getClass() : null)); System.out.println("\tis Wrapper: " + (result instanceof Wrapper)); System.out.println("\t" + result); return result; }
@Override public Object unwrap() { if (object instanceof com.jsen.core.Wrapper) { return ((com.jsen.core.Wrapper<?>)object).unwrap(); } return object; }
/** * Unwraps passed value from JavaScript wrapper interface. * * @param value Value to be unwrapped. * @return Unwrapped value. */ protected Object unwrap(Object value) { if (value == null) { return null; } if (value instanceof Wrapper) { value = ((Wrapper)value).unwrap(); } return value; }
Object unwrapReturnValue(Object result) { if (result instanceof Wrapper) { result = ( (Wrapper) result).unwrap(); } return result instanceof Undefined ? null : result; }
/** * We convert script values to the nearest Java value. * We unwrap wrapped Java objects so that access from * Bindings.get() would return "workable" value for Java. * But, at the same time, we need to make few special cases * and hence the following function is used. */ private Object jsToJava(Object jsObj) { if (jsObj instanceof Wrapper) { Wrapper njb = (Wrapper) jsObj; /* importClass feature of ImporterTopLevel puts * NativeJavaClass in global scope. If we unwrap * it, importClass won't work. */ if (njb instanceof NativeJavaClass) { return njb; } /* script may use Java primitive wrapper type objects * (such as java.lang.Integer, java.lang.Boolean etc) * explicitly. If we unwrap, then these script objects * will become script primitive types. For example, * * var x = new java.lang.Double(3.0); print(typeof x); * * will print 'number'. We don't want that to happen. */ Object obj = njb.unwrap(); if (obj instanceof Number || obj instanceof String || obj instanceof Boolean || obj instanceof Character) { // special type wrapped -- we just leave it as is. return njb; } else { // return unwrapped object for any other object. return obj; } } else { // not-a-Java-wrapper return jsObj; } }
@Override public Object unwrap() { if (object instanceof org.fit.cssbox.scriptbox.script.Wrapper) { return ((org.fit.cssbox.scriptbox.script.Wrapper<?>)object).unwrap(); } return object; }
@Override public Object unwrap() { if (wrappedScriptable instanceof Wrapper) { return ((Wrapper) wrappedScriptable).unwrap(); } return wrappedScriptable; }
public SanboxedJavaObject(Scriptable wrappedScriptable, Shutter shutter) { super(wrappedScriptable); this.shutter = shutter; this.visitedMethods = new HashMap<Method, Boolean>(); this.visitedFields = new HashMap<String, Boolean>(); if (wrappedScriptable instanceof Wrapper) { this.wrappedObject = ((Wrapper)wrappedScriptable).unwrap(); } else { this.wrappedObject = wrappedScriptable; } this.wrappedObjectType = wrappedObject.getClass(); }
/** * 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 ); }
@Override public String toString(final Object obj) { return runWithContext(new RhinoCallable<String, RuntimeException>() { @Override protected String doCall(Context cx, Scriptable scope) throws RuntimeException { if (obj == null) { return "null"; } else if (obj instanceof Wrapper) { Object unwrapped = ((Wrapper) obj).unwrap(); return unwrapped.toString(); } else if (obj instanceof Scriptable) { return (String) cx.evaluateString((Scriptable) obj, "(function(obj) { " + " try { " + " var str = JSON.stringify(obj); " + " if (typeof str === 'string') return str; " + " } catch (e) { } " + " return obj.toString(); " + "})(this)", "<toString>", 1, null); } else if (obj instanceof Undefined) { return "undefined"; } return obj.toString(); } }); }
public static Object unwrap(Object obj) { if (obj instanceof Wrapper) { obj = ((Wrapper) obj).unwrap(); } else if (obj == Undefined.instance) { obj = null; } return obj; }
Object unwrapReturnValue(Object result) { if (result instanceof Wrapper) { result = ((Wrapper) result).unwrap(); } return result instanceof Undefined ? null : result; }
/** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { PropertyCheck.mandatory(this, "registry", this.registry); this.registry.registerValueInstanceConverter(Object.class, this); this.registry.registerValueInstanceConverter(Wrapper.class, this); }
/** * {@inheritDoc} */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { return value instanceof Wrapper && (expectedClass.isInstance(((Wrapper) value).unwrap()) || globalDelegate.canConvertValueForJava( ((Wrapper) value).unwrap(), expectedClass)); }
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); }
private Object unwrap(Object obj) { return obj instanceof Wrapper ? ((Wrapper) obj).unwrap() : obj; }
/** * 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. */ @Override 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; }