Search in sources :

Example 16 with XMLObject

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);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) XMLObject(org.mozilla.javascript.xml.XMLObject) Callable(java.util.concurrent.Callable) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ExecutorService(java.util.concurrent.ExecutorService) XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 17 with XMLObject

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;
}
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)

Aggregations

XMLObject (org.mozilla.javascript.xml.XMLObject)17 StringReader (java.io.StringReader)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Abdera (org.apache.abdera.Abdera)1 Factory (org.apache.abdera.factory.Factory)1 IRISyntaxException (org.apache.abdera.i18n.iri.IRISyntaxException)1 LogFactory (org.apache.commons.logging.LogFactory)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 Context (org.mozilla.javascript.Context)1 Scriptable (org.mozilla.javascript.Scriptable)1 Wrapper (org.mozilla.javascript.Wrapper)1 Node (org.w3c.dom.Node)1