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);
}
}
}
}
}
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;
}
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('}');
}
}
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;
}
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;
}
Aggregations