Search in sources :

Example 6 with Wrapper

use of org.mozilla.javascript.Wrapper in project sling by apache.

the class RhinoJavaScriptEngine method getBoundProperties.

private void getBoundProperties(Scriptable scope, Bindings bindings) {
    Object[] ids = scope.getIds();
    for (Object id : ids) {
        if (id instanceof String) {
            String key = (String) id;
            Object value = scope.get(key, scope);
            if (value != Scriptable.NOT_FOUND) {
                if (value instanceof Wrapper) {
                    bindings.put(key, ((Wrapper) value).unwrap());
                } else {
                    bindings.put(key, value);
                }
            }
        }
    }
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 7 with Wrapper

use of org.mozilla.javascript.Wrapper in project sling by apache.

the class ScriptEngineHelper method eval.

public Object eval(String javascriptCode, Map<String, Object> data, final StringWriter sw) throws ScriptException {
    final PrintWriter pw = new PrintWriter(sw, true);
    ScriptContext ctx = new SimpleScriptContext();
    final Bindings b = new SimpleBindings();
    b.put("out", pw);
    if (data != null) {
        for (Map.Entry<String, Object> e : data.entrySet()) {
            b.put(e.getKey(), e.getValue());
        }
    }
    ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
    ctx.setWriter(sw);
    ctx.setErrorWriter(new OutputStreamWriter(System.err));
    Object result = getEngine().eval(javascriptCode, ctx);
    if (result instanceof Wrapper) {
        result = ((Wrapper) result).unwrap();
    }
    if (result instanceof ScriptableObject) {
        Context.enter();
        try {
            result = ((ScriptableObject) result).getDefaultValue(null);
        } finally {
            Context.exit();
        }
    }
    return result;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) OutputStreamWriter(java.io.OutputStreamWriter) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 8 with Wrapper

use of org.mozilla.javascript.Wrapper in project wso2-synapse by wso2.

the class CommonScriptMessageContext method serializeJson.

/**
 * Serialize json payload and writes to output stream.
 *
 * @param obj Json object which required to be serialized
 * @param out Output stream which is required to be written with serialized json
 */
private void serializeJson(Object obj, OutputStream out) throws IOException {
    if (out == null) {
        logger.warn("#serializeJson. Did not serialize JSON object. Object: " + obj + "  Stream: " + out);
        return;
    }
    if (obj instanceof Wrapper) {
        obj = ((Wrapper) obj).unwrap();
    }
    if (obj == null) {
        out.write("null".getBytes());
    } else if (obj instanceof NativeObject) {
        out.write('{');
        NativeObject o = (NativeObject) obj;
        Object[] ids = o.getIds();
        boolean first = true;
        for (Object id : ids) {
            String key = (String) id;
            Object value = o.get((String) id, o);
            if (!first) {
                out.write(',');
                out.write(' ');
            } else {
                first = false;
            }
            out.write('"');
            out.write(key.getBytes());
            out.write('"');
            out.write(':');
            serializeJson(value, out);
        }
        out.write('}');
    } else if (obj instanceof NativeArray) {
        out.write('[');
        NativeArray o = (NativeArray) obj;
        Object[] ids = o.getIds();
        boolean first = true;
        for (Object id : ids) {
            Object value = o.get((Integer) id, o);
            if (!first) {
                out.write(',');
                out.write(' ');
            } else {
                first = false;
            }
            serializeJson(value, out);
        }
        out.write(']');
    } else if (obj instanceof Object[]) {
        out.write('[');
        boolean first = true;
        for (Object value : (Object[]) obj) {
            if (!first) {
                out.write(',');
                out.write(' ');
            } else {
                first = false;
            }
            serializeJson(value, out);
        }
        out.write(']');
    } else if (obj instanceof String) {
        out.write('"');
        out.write(((String) obj).getBytes());
        out.write('"');
    } else if (obj instanceof ConsString) {
        // This class represents a string composed of two components using the "+" operator
        // in java script with rhino7 upward. ex:var str = "val1" + "val2";
        out.write('"');
        out.write((((ConsString) obj).toString()).getBytes());
        out.write('"');
    } else if (obj instanceof Integer || obj instanceof Long || obj instanceof Float || obj instanceof Double || obj instanceof Short || obj instanceof BigInteger || obj instanceof BigDecimal || obj instanceof Boolean) {
        out.write(obj.toString().getBytes());
    } else {
        out.write('{');
        out.write('}');
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Wrapper(org.mozilla.javascript.Wrapper) ConsString(org.mozilla.javascript.ConsString) ConsString(org.mozilla.javascript.ConsString) BigDecimal(java.math.BigDecimal) NativeObject(org.mozilla.javascript.NativeObject) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) XMLObject(org.mozilla.javascript.xml.XMLObject) NativeObject(org.mozilla.javascript.NativeObject)

Example 9 with Wrapper

use of org.mozilla.javascript.Wrapper in project cxf by apache.

the class AbstractDOMProvider method invoke.

public DOMSource invoke(DOMSource request) {
    DOMSource response = new DOMSource();
    Context cx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable scope = cx.newObject(scriptScope);
        scope.setPrototype(scriptScope);
        scope.setParentScope(null);
        Node node = request.getNode();
        Object inDoc = null;
        if (isE4X) {
            try {
                inDoc = Context.toObject(node, scope);
                Object[] args = { inDoc };
                inDoc = cx.newObject(scope, "XML", args);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            inDoc = Context.toObject(node, scope);
        }
        Object[] args = { inDoc };
        Object jsResp = invokeFunc.call(cx, scope, scope, args);
        if (jsResp instanceof Wrapper) {
            jsResp = ((Wrapper) jsResp).unwrap();
        }
        if (jsResp instanceof XMLObject) {
            jsResp = org.mozilla.javascript.xmlimpl.XMLLibImpl.toDomNode(jsResp);
        }
        if (jsResp instanceof Node) {
            node = (Node) jsResp;
            response.setNode(node);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        Context.exit();
    }
    return response;
}
Also used : Context(org.mozilla.javascript.Context) Wrapper(org.mozilla.javascript.Wrapper) DOMSource(javax.xml.transform.dom.DOMSource) Node(org.w3c.dom.Node) XMLObject(org.mozilla.javascript.xml.XMLObject) XMLObject(org.mozilla.javascript.xml.XMLObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 10 with Wrapper

use of org.mozilla.javascript.Wrapper in project jaggery by wso2.

the class LogHostObject method getThrowable.

private static Throwable getThrowable(Object error) {
    if (error instanceof Throwable) {
        return (Throwable) error;
    }
    if (!(error instanceof Scriptable)) {
        return null;
    }
    Scriptable scriptable = (Scriptable) error;
    if (!"Error".equals(scriptable.getClassName())) {
        return null;
    }
    error = scriptable.get(RHINO_EXCEPTION_KEY, scriptable);
    if (error == null) {
        error = scriptable.get(JAVA_EXCEPTION_KEY, scriptable);
    }
    if (error instanceof Wrapper) {
        error = ((Wrapper) error).unwrap();
    }
    if (!(error instanceof Throwable)) {
        return null;
    }
    return (Throwable) error;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Wrapper (org.mozilla.javascript.Wrapper)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)7 Context (org.mozilla.javascript.Context)4 Scriptable (org.mozilla.javascript.Scriptable)4 NativeArray (org.mozilla.javascript.NativeArray)3 XMLObject (org.mozilla.javascript.xml.XMLObject)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ConsString (org.mozilla.javascript.ConsString)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 NativeObject (org.mozilla.javascript.NativeObject)2 List (com.scratchdisk.list.List)1 ReadOnlyList (com.scratchdisk.list.ReadOnlyList)1 ReadOnlyStringIndexList (com.scratchdisk.list.ReadOnlyStringIndexList)1 StringIndexList (com.scratchdisk.list.StringIndexList)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1