Search in sources :

Example 46 with Context

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

the class AsyncExtractor method convertCallback.

private static Function convertCallback(final UnaryCallback unaryCallback) {
    return new BaseFunction() {

        @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            Object arg = (args.length == 0) ? Context.getUndefinedValue() : args[0];
            unaryCallback.invoke(arg);
            return Context.getUndefinedValue();
        }
    };
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 47 with Context

use of org.mozilla.javascript.Context in project gradle by gradle.

the class RhinoWorkerUtils method parse.

public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }
    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }
    return scope;
}
Also used : Context(org.mozilla.javascript.Context) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Scriptable(org.mozilla.javascript.Scriptable) FileInputStream(java.io.FileInputStream)

Example 48 with Context

use of org.mozilla.javascript.Context in project gradle by gradle.

the class RhinoWorkerUtils method childScope.

public static <R> R childScope(Scriptable parentScope, ScopeOperation<R> operation) {
    Context context = Context.enter();
    try {
        operation.initContext(context);
        Scriptable childScope = context.newObject(parentScope);
        childScope.setParentScope(parentScope);
        return operation.action(childScope, context);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 49 with Context

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

the class JsSimpleDomParser method jsFunction_parseFromString.

// CHECKSTYLE:OFF
public Object jsFunction_parseFromString(String xml, String mimeType) {
    StringReader reader = new StringReader(xml);
    InputSource inputSource = new InputSource(reader);
    Document document;
    try {
        document = documentBuilder.parse(inputSource);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Context context = ContextFactory.getGlobal().enterContext();
    try {
        JsSimpleDomNode domNode = (JsSimpleDomNode) context.newObject(getParentScope(), "Node");
        domNode.initialize(document, null);
        return domNode;
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 50 with Context

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

the class JsXMLHttpRequest method doSend.

private void doSend(byte[] dataToSend, boolean xml) {
    // avoid warnings on stuff we arent using yet.
    if (storedUser != null || storedPassword != null) {
    // 
    }
    // 1 check state
    if (readyState != jsGet_OPENED()) {
        LOG.severe("send state != OPENED.");
        throwError("INVALID_STATE_ERR");
    }
    // 2 check flag
    if (sendFlag) {
        LOG.severe("send sendFlag set.");
        throwError("INVALID_STATE_ERR");
    }
    // 3
    sendFlag = storedAsync;
    // UTF-8 bytes.
    if (xml && !requestHeaders.containsKey("Content-Type")) {
        requestHeaders.put("Content-Type", "application/xml;charset=utf-8");
    }
    // 5 talk to the server.
    try {
        connection = url.openConnection();
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "send connection failed.", e);
        throwError("CONNECTION_FAILED");
    }
    connection.setDoInput(true);
    // Enable tunneling.
    connection.setUseCaches(false);
    boolean post = false;
    httpConnection = null;
    if (connection instanceof HttpURLConnection) {
        httpConnection = (HttpURLConnection) connection;
        try {
            httpConnection.setRequestMethod(storedMethod);
            if ("POST".equalsIgnoreCase(storedMethod)) {
                httpConnection.setDoOutput(true);
                post = true;
            }
            for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
                httpConnection.setRequestProperty(headerEntry.getKey(), headerEntry.getValue());
            }
        } catch (ProtocolException e) {
            LOG.log(Level.SEVERE, "send http protocol exception.", e);
            throwError("HTTP_PROTOCOL_ERROR");
        }
    }
    if (post) {
        OutputStream outputStream = null;
        try {
            // implicitly connects?
            outputStream = connection.getOutputStream();
            if (dataToSend != null) {
                outputStream.write(dataToSend);
                outputStream.flush();
            }
            outputStream.close();
        } catch (IOException e) {
            errorFlag = true;
            LOG.log(Level.SEVERE, "send output error.", e);
            throwError("NETWORK_ERR");
            try {
                outputStream.close();
            } catch (IOException e1) {
            // 
            }
        }
    }
    // 6
    notifyReadyStateChangeListener();
    if (storedAsync) {
        new Thread() {

            public void run() {
                try {
                    Context cx = ContextFactory.getGlobal().enterContext();
                    communicate(cx);
                } finally {
                    Context.exit();
                }
            }
        }.start();
    } else {
        communicate(Context.getCurrentContext());
    }
}
Also used : Context(org.mozilla.javascript.Context) ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Context (org.mozilla.javascript.Context)152 Scriptable (org.mozilla.javascript.Scriptable)68 ScriptableObject (org.mozilla.javascript.ScriptableObject)62 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 Date (java.util.Date)5 HashSet (java.util.HashSet)5 List (java.util.List)5