use of org.mozilla.javascript.xml.XMLObject in project jaggery by wso2.
the class XSLTHostObject method jsFunction_transform.
/**
* transform(xml)
* transform(xml, callback)
* transform(xml, paramMap)
* transform(xml, paramMap, callback)
* trasformer(xml, callback, uriResolver)
* trasformer(xml, paramMap, callback, uriResolver)
*/
public static String jsFunction_transform(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "transform";
int argsCount = args.length;
if (argsCount > 4) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
StringReader xml = null;
Function uriResolver = null;
NativeObject paramMap = null;
Function callback = null;
if (args[0] instanceof String) {
xml = new StringReader((String) args[0]);
} else if (args[0] instanceof XMLObject) {
xml = new StringReader(args[0].toString());
} else {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string | xml", args[0], true);
}
if (argsCount == 2) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
} else if (args[1] instanceof Function) {
callback = (Function) args[1];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object | function", args[1], false);
}
} else if (argsCount == 3) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
if (args[2] instanceof Function) {
callback = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
} else if (args[1] instanceof Function) {
callback = (Function) args[1];
if (args[2] instanceof Function) {
uriResolver = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object", args[1], false);
}
} else if (argsCount == 4) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object", args[1], false);
}
if (args[2] instanceof Function) {
callback = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
if (args[3] instanceof Function) {
uriResolver = (Function) args[3];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "4", "function", args[3], false);
}
}
final XSLTHostObject xho = (XSLTHostObject) thisObj;
try {
final StringWriter result = new StringWriter();
if (callback == null) {
if (xho.transformer == null) {
xho.transformer = getTransformer(cx, thisObj, xho, paramMap, uriResolver);
} else {
if (paramMap != null) {
setParams(xho.transformer, paramMap);
}
if (uriResolver != null) {
xho.transformer.setURIResolver(getUriResolver(cx, funObj, uriResolver));
}
}
xho.transformer.transform(new StreamSource(xml), new StreamResult(result));
return result.toString();
}
final StringReader finalXml = xml;
final Function finalCallback = callback;
final Function finalUriResolver = uriResolver;
final NativeObject finalParamMap = paramMap;
final ContextFactory factory = cx.getFactory();
final ExecutorService es = Executors.newCachedThreadPool();
es.submit(new Callable() {
public Object call() throws Exception {
Context ctx = RhinoEngine.enterContext(factory);
try {
getTransformer(ctx, thisObj, xho, finalParamMap, finalUriResolver).transform(new StreamSource(finalXml), new StreamResult(result));
finalCallback.call(ctx, xho, xho, new Object[] { result.toString() });
} catch (TransformerException e) {
log.warn(e);
} finally {
es.shutdown();
RhinoEngine.exitContext();
}
return null;
}
});
return null;
} catch (TransformerException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.mozilla.javascript.xml.XMLObject 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;
}
Aggregations