Search in sources :

Example 91 with ScriptException

use of javax.script.ScriptException in project jgnash by ccavanaugh.

the class ImportFilter method getDescription.

public String getDescription() {
    try (final Reader reader = getReader()) {
        engine.eval(reader);
        final Invocable invocable = (Invocable) engine;
        final Object result = invocable.invokeFunction("getDescription", Locale.getDefault());
        return result.toString();
    } catch (final ScriptException | IOException | NoSuchMethodException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return "";
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 92 with ScriptException

use of javax.script.ScriptException in project jgnash by ccavanaugh.

the class ImportFilter method processMemo.

public String processMemo(final String memo) {
    try (final Reader reader = getReader()) {
        engine.eval(reader);
        final Invocable invocable = (Invocable) engine;
        final Object result = invocable.invokeFunction("processMemo", memo);
        return result.toString();
    } catch (final ScriptException | IOException | NoSuchMethodException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return memo;
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 93 with ScriptException

use of javax.script.ScriptException in project jgnash by ccavanaugh.

the class ImportFilter method processPayee.

public String processPayee(final String payee) {
    try (final Reader reader = getReader()) {
        engine.eval(reader);
        final Invocable invocable = (Invocable) engine;
        final Object result = invocable.invokeFunction("processPayee", payee);
        return result.toString();
    } catch (final ScriptException | IOException | NoSuchMethodException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return payee;
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 94 with ScriptException

use of javax.script.ScriptException in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method executeScript.

/**
     * Execute an analysis script
     * 
     * @param <T>
     *            The result type.
     * @param scriptExecutable
     *            The script.
     * @param ruleParameters
     * @param severity
     *            The severity. @return The result.
     * @throws RuleExecutorException
     *             If script execution fails.
     */
private <T extends ExecutableRule> Result<T> executeScript(T executable, ScriptExecutable scriptExecutable, Map<String, Object> ruleParameters, Severity severity) throws RuleExecutorException {
    String language = scriptExecutable.getLanguage();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
    if (scriptEngine == null) {
        List<String> availableLanguages = new ArrayList<>();
        for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
            availableLanguages.addAll(factory.getNames());
        }
        throw new RuleExecutorException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
    }
    // Set default variables
    scriptEngine.put(ScriptVariable.STORE.getVariableName(), store);
    scriptEngine.put(ScriptVariable.RULE.getVariableName(), executable);
    scriptEngine.put(ScriptVariable.SEVERITY.getVariableName(), severity);
    // Set rule parameters
    for (Map.Entry<String, Object> entry : ruleParameters.entrySet()) {
        scriptEngine.put(entry.getKey(), entry.getValue());
    }
    Object scriptResult;
    try {
        scriptResult = scriptEngine.eval(scriptExecutable.getSource());
    } catch (ScriptException e) {
        throw new RuleExecutorException("Cannot execute script.", e);
    }
    if (!(scriptResult instanceof Result)) {
        throw new RuleExecutorException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
    }
    return Result.class.cast(scriptResult);
}
Also used : ScriptException(javax.script.ScriptException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 95 with ScriptException

use of javax.script.ScriptException in project sling by apache.

the class XProcScriptEngine method eval.

public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (helper == null) {
        throw new ScriptException("SlingScriptHelper missing from bindings");
    }
    String scriptName = helper.getScript().getScriptResource().getPath();
    try {
        XplBuilder xplBuilder = new XplBuilder();
        Pipeline xpl = (Pipeline) xplBuilder.build(reader);
        xpl.getEnv().setSling(helper);
        xpl.eval();
    } catch (Throwable t) {
        log.error("Failure running XProc script.", t);
        final ScriptException se = new ScriptException("Failure running XProc script " + scriptName);
        se.initCause(t);
        throw se;
    }
    return null;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) XplBuilder(org.apache.sling.scripting.xproc.xpl.XplBuilder) Pipeline(org.apache.sling.scripting.xproc.xpl.api.Pipeline)

Aggregations

ScriptException (javax.script.ScriptException)106 ScriptEngine (javax.script.ScriptEngine)42 IOException (java.io.IOException)41 Bindings (javax.script.Bindings)30 ScriptEngineManager (javax.script.ScriptEngineManager)20 InputStreamReader (java.io.InputStreamReader)12 CompiledScript (javax.script.CompiledScript)12 Map (java.util.Map)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)11 SimpleBindings (javax.script.SimpleBindings)10 File (java.io.File)8 Reader (java.io.Reader)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5