import javax.script.ScriptEngine;
import net.sf.jmatchparser.template.CallbackFunction;
public class DefaultCallbackFunctions {
static void addTo(ParserImpl parser) {
parser.addCallback("replace", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length % 2 != 1)
throw new RuntimeException("Replace needs odd number of arguments: " + arguments.length);
String result = arguments[0];
for (int i = 1; i < arguments.length; i += 2) {
result = result.replaceAll(arguments[i], arguments[i + 1]);
}
return result;
}
});
parser.addCallback("uppercase", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length != 1)
throw new RuntimeException("Uppercase needs 1 argument: " + arguments.length);
return arguments[0].toUpperCase();
}
});
parser.addCallback("lowercase", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length != 1)
throw new RuntimeException("Lowercase needs 1 argument: " + arguments.length);
return arguments[0].toLowerCase();
}
});
parser.addCallback("addentities", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length != 1)
throw new RuntimeException("AddEntities needs 1 argument: " + arguments.length);
return addEntities(arguments[0]);
}
});
parser.addCallback("stripentities", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length != 1)
throw new RuntimeException("StripEntities needs 1 argument: " + arguments.length);
return stripEntities(arguments[0]);
}
});
parser.addCallback("js", new CallbackFunction() {
@Override
public String call(String[] arguments) {
if (arguments.length == 0)
throw new RuntimeException("JS needs at least 1 argument: " + arguments.length);
ScriptEngine engine = new javax.script.ScriptEngineManager().getEngineByName("rhino");