Search in sources :

Example 41 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project gdmatrix by gdmatrix.

the class ScriptClient method executeScript.

public Object executeScript(String scriptName, Scriptable scope) throws Exception {
    LOGGER.log(Level.INFO, "Executing {0} script.", new Object[] { scriptName });
    Script script;
    long now = System.currentTimeMillis();
    if (lastCacheRefresh + REFRESH_TIME < now)
        clearCache(now);
    script = getScript(scriptName);
    Object result = null;
    try {
        result = script.exec(context, scope);
        if (result instanceof NativeJavaObject) {
            NativeJavaObject nat = (NativeJavaObject) result;
            result = nat.unwrap();
        }
        if (result instanceof Undefined)
            result = null;
    } catch (JavaScriptException ex) {
        throw new Exception(ex.getMessage());
    } finally {
        Context.exit();
    }
    return result;
}
Also used : Script(org.mozilla.javascript.Script) Undefined(org.mozilla.javascript.Undefined) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 42 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project knetbuilder by Rothamsted.

the class JSInterpreter method process.

public String process(String command, OutputPrinter out) {
    Object result = null;
    if (pc != null)
        pc.processingStarted();
    cx.getFactory().enterContext();
    try {
        result = cx.evaluateString(scope, command, "<cmd>", 1, null);
    } catch (Exception e) {
        // e.printStackTrace();
        if (e instanceof WrappedException && ((WrappedException) e).getWrappedException() instanceof FunctionException) {
            e.printStackTrace();
            out.printAndPrompt(((WrappedException) e).getWrappedException().getMessage());
            return null;
        } else if (e instanceof EcmaError) {
            e.printStackTrace();
            out.printAndPrompt(e.getMessage().substring(0, e.getMessage().length() - 10));
            return null;
        } else if (e instanceof EvaluatorException) {
            e.printStackTrace();
            // out.feedOutput("Unknow command or syntax error.");
            out.printAndPrompt(e.getMessage());
            return null;
        }
        out.printAndPrompt(e.getMessage());
        Context.exit();
        return null;
    } finally {
        try {
            if (pc != null)
                pc.processingFinished();
        } catch (Exception y) {
            y.printStackTrace();
        }
    }
    try {
        if (result == null) {
            out.printAndPrompt("null");
        } else if (result.equals(Context.getUndefinedValue())) {
            out.printAndPrompt("Undefined");
        } else {
            Class<?> wrp = clsToWrapper.get(result.getClass());
            if (wrp != null)
                out.printAndPrompt("[Object " + wrp.getSimpleName() + "]");
            else if (result instanceof NativeJavaObject) {
                Object d = ((NativeJavaObject) result).unwrap();
                if (Number.class.isAssignableFrom(d.getClass()) || d instanceof String) {
                    out.printAndPrompt(Context.toString(result));
                } else {
                    out.printAndPrompt("[Object " + d.getClass().getSimpleName() + "]");
                }
            } else {
                out.printAndPrompt(Context.toString(result));
            }
        }
    } catch (Exception e) {
        out.printAndPrompt(e.getMessage());
        e.printStackTrace();
    }
    try {
        Context.exit();
    } catch (Exception e) {
        out.printAndPrompt(e.getMessage());
        e.printStackTrace();
    }
    return null;
}
Also used : WrappedException(org.mozilla.javascript.WrappedException) EcmaError(org.mozilla.javascript.EcmaError) EvaluatorException(org.mozilla.javascript.EvaluatorException) FunctionException(net.sourceforge.ondex.scripting.FunctionException) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) EvaluatorException(org.mozilla.javascript.EvaluatorException) FunctionException(net.sourceforge.ondex.scripting.FunctionException) WrappedException(org.mozilla.javascript.WrappedException)

Example 43 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project veraPDF-library by veraPDF.

the class JavaScriptEvaluator method evalVariableResult.

public static synchronized java.lang.Object evalVariableResult(Variable variable, Object object, ScriptableObject scope) {
    Script script;
    if (!variableScripts.containsKey(variable.getName())) {
        String source = getStringScript(object, variable.getValue());
        script = JavaScriptEvaluator.compileString(source);
        variableScripts.put(variable.getName(), script);
    } else {
        script = variableScripts.get(variable.getName());
    }
    scope.put("obj", scope, object);
    java.lang.Object res = script.exec(context, scope);
    if (res instanceof NativeJavaObject) {
        res = ((NativeJavaObject) res).unwrap();
    }
    return res;
}
Also used : Script(org.mozilla.javascript.Script) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 44 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project veraPDF-library by veraPDF.

the class BaseValidator method initializeAllVariables.

private void initializeAllVariables() {
    for (Variable var : this.profile.getVariables()) {
        if (var == null)
            continue;
        java.lang.Object res = JavaScriptEvaluator.evaluateString(var.getDefaultValue(), this.scope);
        if (res instanceof NativeJavaObject) {
            res = ((NativeJavaObject) res).unwrap();
        }
        this.scope.put(var.getName(), this.scope, res);
    }
}
Also used : Variable(org.verapdf.pdfa.validation.profiles.Variable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 45 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project echosvg by css4j.

the class GlobalWrapper method startMouseCapture.

/**
 * Wraps the 'startMouseCapture' method of the SVGGlobal interface.
 */
public static void startMouseCapture(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    int len = args.length;
    GlobalWrapper gw = (GlobalWrapper) thisObj;
    SVGGlobal global = (SVGGlobal) gw.window;
    if (len >= 3) {
        EventTarget et = null;
        if (args[0] instanceof NativeJavaObject) {
            Object o = ((NativeJavaObject) args[0]).unwrap();
            if (o instanceof EventTarget) {
                et = (EventTarget) o;
            }
        }
        if (et == null) {
            throw Context.reportRuntimeError("First argument to startMouseCapture must be an EventTarget");
        }
        boolean sendAll = Context.toBoolean(args[1]);
        boolean autoRelease = Context.toBoolean(args[2]);
        global.startMouseCapture(et, sendAll, autoRelease);
    }
}
Also used : SVGGlobal(io.sf.carte.echosvg.dom.svg12.SVGGlobal) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) EventTarget(org.w3c.dom.events.EventTarget)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)45 Scriptable (org.mozilla.javascript.Scriptable)16 Context (org.mozilla.javascript.Context)11 NativeArray (org.mozilla.javascript.NativeArray)9 ScriptableObject (org.mozilla.javascript.ScriptableObject)9 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 IOException (java.io.IOException)5 Test (org.junit.Test)4 NativeObject (org.mozilla.javascript.NativeObject)4 Undefined (org.mozilla.javascript.Undefined)4 NodeList (org.w3c.dom.NodeList)4 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)3 SolutionScope (com.servoy.j2db.scripting.SolutionScope)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Collection (java.util.Collection)3 Vector (java.util.Vector)3 FunctionObject (org.mozilla.javascript.FunctionObject)3 Node (org.w3c.dom.Node)3