All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.aguacate.regex.spi.script.RegexScriptEngineJs Maven / Gradle / Ivy

There is a newer version: 0.10.9
Show newest version
package net.sf.aguacate.regex.spi.script;

import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptException;

import net.sf.aguacate.regex.Regex;

public class RegexScriptEngineJs implements Regex {

	private final Invocable invocable;

	private final CompiledScript compiled;

	public RegexScriptEngineJs(Invocable invocable, CompiledScript compiled) {
		this.invocable = invocable;
		this.compiled = compiled;
	}

	@Override
	public boolean matches(String value) {
		try {
			Object result = invocable.invokeMethod(compiled.eval(), "test", value);
			Class klass = result.getClass();
			if (Boolean.class == klass) {
				return ((Boolean) result).booleanValue();
			} else {
				throw new IllegalStateException(klass.getName());
			}
		} catch (NoSuchMethodException | ScriptException e) {
			throw new IllegalStateException(e);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy