Search in sources :

Example 51 with ScriptException

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

the class TomEEEmbeddedMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("catalinaBase", base);
            engine.eval(new StringReader(js), bindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 52 with ScriptException

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

the class AbstractTomEEMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException {
    if (customizers != null) {
        final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
        if (engine == null) {
            throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
        }
        for (final String js : customizers) {
            try {
                final SimpleBindings bindings = new SimpleBindings();
                bindings.put("catalinaBase", catalinaBase.getAbsolutePath());
                bindings.put("resolver", new Resolver() {

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String classifier, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }
                });
                engine.eval(new StringReader(js), bindings);
            } catch (final ScriptException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ZipFile(java.util.zip.ZipFile) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Example 53 with ScriptException

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

the class JFloatField method eval.

/**
     * BigDecimal and the interpreter cannot parse ',' in string
     * representations of decimals. This method will replace any ',' with '.'
     * and then try parsing with BigDecimal. If this fails, then it is assumed
     * that the user has used mathematical operators and then evaluates the
     * string as a mathematical expression.
     *
     * @return A string representation of the resulting decimal
     */
private String eval() {
    String text = getText();
    if (text == null || text.isEmpty()) {
        return "";
    }
    if (DEBUG) {
        System.out.println("eval s0: " + text);
    }
    // strip out any group separators (This could be '.' for certain
    // locales)
    StringBuilder temp = new StringBuilder();
    for (int j = 0; j < text.length(); j++) {
        char c = text.charAt(j);
        if (c != group) {
            temp.append(c);
        }
    }
    text = temp.toString();
    if (DEBUG) {
        System.out.println("eval s1: " + text);
    }
    // needed
    if (fraction == ',') {
        text = text.replace(',', '.');
    }
    if (DEBUG) {
        System.out.println("eval s2: " + text);
    }
    try {
        BigDecimal d = new BigDecimal(text);
        if (DEBUG) {
            System.out.println("eval result: " + d);
        }
        return d.toString();
    } catch (NumberFormatException nfe) {
        if (DEBUG) {
            Logger.getLogger(JFloatField.class.getName()).log(Level.SEVERE, nfe.getLocalizedMessage(), nfe);
        }
        try {
            Object o;
            o = jsEngine.eval(text);
            if (o instanceof Number) {
                // scale the number
                return new BigDecimal(o.toString()).setScale(scale, MathConstants.roundingMode).toString();
            }
        } catch (ScriptException ex) {
            if (DEBUG) {
                Logger.getLogger(JFloatField.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
            return "";
        }
    }
    return "";
}
Also used : ScriptException(javax.script.ScriptException) BigDecimal(java.math.BigDecimal)

Example 54 with ScriptException

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

the class ExecuteJavaScriptAction method showAndWait.

public static void showAndWait() {
    final ResourceBundle resources = ResourceUtils.getBundle();
    final FileChooser fileChooser = configureFileChooser();
    fileChooser.setTitle(resources.getString("Title.SelFile"));
    final File file = fileChooser.showOpenDialog(MainView.getPrimaryStage());
    if (file != null) {
        Preferences pref = Preferences.userNodeForPackage(ExecuteJavaScriptAction.class);
        pref.put(LAST_DIR, file.getParentFile().getAbsolutePath());
        Platform.runLater(() -> {
            try (final Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
                new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
            } catch (IOException | ScriptException ex) {
                Logger.getLogger(ExecuteJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
            }
        });
    }
}
Also used : ScriptException(javax.script.ScriptException) FileChooser(javafx.stage.FileChooser) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 55 with ScriptException

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

the class RunJavaScriptAction method actionPerformed.

@Override
public void actionPerformed(final ActionEvent e) {
    final Preferences pref = Preferences.userNodeForPackage(RunJavaScriptAction.class);
    JFileChooser chooser = new JFileChooser(pref.get(JAVASCRIPT_DIR, null));
    chooser.setMultiSelectionEnabled(false);
    if (chooser.showOpenDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(JAVASCRIPT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        final Path path = chooser.getSelectedFile().toPath();
        EventQueue.invokeLater(() -> {
            try (final Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
                new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
            } catch (IOException | ScriptException ex) {
                Logger.getLogger(RunJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ScriptException(javax.script.ScriptException) JFileChooser(javax.swing.JFileChooser) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences)

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