Skip to content

Instantly share code, notes, and snippets.

@brunocribeiro
Created April 27, 2015 16:29
Show Gist options
  • Save brunocribeiro/2d053889a8f6056e28c7 to your computer and use it in GitHub Desktop.
Save brunocribeiro/2d053889a8f6056e28c7 to your computer and use it in GitHub Desktop.
Exemplo de execução de função JS com o Nashorn
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class NashornExample {
private static final Logger LOGGER = Logger.getLogger(NashornExample.class.getName());
private static final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
private static final Invocable invocable = (Invocable) engine;
public static void main(final String[] args) {
final String[] inputs = {"50kb", "20 Mb", "aa20kb"};
final String processFunction = processFunction();
for (final String input : inputs) {
final String resultado = nashornExecuter(processFunction, "process", input);
out.println(String.format("Entrada: %s | Resultado: %s", input, resultado));
}
}
public static String nashornExecuter(final String function, final String value, final Object... params) {
try {
engine.eval(function);
return (String) invocable.invokeFunction(value, params);
} catch (final ScriptException | NoSuchMethodException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
return value;
}
private static String processFunction() {
final StringBuilder function = new StringBuilder();
function.append("function process(val) {");
function.append(" var isPeso = '(?:k|m|g)b$';");
function.append(" var normalizado = val.trim().toLowerCase().replace(' ', '');");
function.append(" var unidade = '';");
function.append(" var numberPattern = /\\d+(\\.\\d{1,2})?|(Livre)/i;");
function.append(" var myArray = normalizado.match(isPeso);");
function.append(" if (myArray != null) {");
function.append(" unidade = myArray;");
function.append(" var getNumbers = val.match(numberPattern);");
function.append(" var i;");
function.append(" var valores = [];");
function.append(" for (i = 0; i < getNumbers.length - 1; i++) {");
function.append(" valores.push(getNumbers[i]);");
function.append(" }");
function.append(" var out = '';");
function.append(" if (normalizado.indexOf('superior') > -1) {");
function.append(" return valores[0] + unidade + ' ou superior';");
function.append(" } else if (normalizado.indexOf('até') > -1) {");
function.append(" if (valores.length == 1) {");
function.append(" out = 'até ' + valores[0];");
function.append(" } else {");
function.append(" out = valores[0] + ' até ' + valores[1];");
function.append(" }");
function.append(" } else if (normalizado.indexOf('ou') > -1 || normalizado.indexOf('/') > -1) {");
function.append(" out = valores[0];");
function.append(" for (i = 1; i < valores.length - 1; i++) {");
function.append(" out += valores[i];");
function.append(" }");
function.append(" } else if (normalizado.indexOf('*') > -1 || normalizado.indexOf('livre') > -1) {");
function.append(" out = 'Livre';");
function.append(" } else {");
function.append(" if (valores.length > 0) {");
function.append(" out = valores[0];");
function.append(" }");
function.append(" }");
function.append(" if (out.length == 0 || out == null) {");
function.append(" return '';");
function.append(" }");
function.append(" return out + unidade;");
function.append(" }");
function.append("}");
return function.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment