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