ch.obermuhlner.scriptengine.java.name.DefaultNameStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-scriptengine Show documentation
Show all versions of java-scriptengine Show documentation
Java script engine for Java as a scripting language.
package ch.obermuhlner.scriptengine.java.name;
import javax.script.ScriptException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DefaultNameStrategy implements NameStrategy {
private static final Pattern NAME_PATTERN = Pattern.compile("public\\s+class\\s+([A-Za-z][A-Za-z0-9_$]*)");
private static final Pattern PACKAGE_PATTERN = Pattern.compile("package\\s+([A-Za-z][A-Za-z0-9_$.]*)");
@Override
public String getFullName(String script) throws ScriptException {
String fullPackage = null;
Matcher packageMatcher = PACKAGE_PATTERN.matcher(script);
if (packageMatcher.find()) {
fullPackage = packageMatcher.group(1);
}
Matcher nameMatcher = NAME_PATTERN.matcher(script);
if (nameMatcher.find()) {
String name = nameMatcher.group(1);
if (fullPackage == null) {
return name;
} else {
return fullPackage + "." + name;
}
}
throw new ScriptException("Could not determine fully qualified class name");
}
}