Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Added a function to improve jscript prefix identifier #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/main/java/org/lsc/utils/ScriptingEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import javax.script.*;

Expand Down Expand Up @@ -34,14 +35,16 @@ public class ScriptingEvaluator {

private Optional<ScriptableEvaluator> defaultImplementation;

private Map<Pattern, String> prefixRegex = new HashMap<>();

static {
implementetionsCache = new HashMap<String, Class<? extends ScriptableEvaluator>>();
instancesCache = CacheBuilder.newBuilder().maximumSize(15).build();
mgr = new ScriptEngineManager();
}

private ScriptingEvaluator() {
instancesTypeCache = new HashMap<String, ScriptableEvaluator>();
instancesTypeCache = new HashMap<>();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory sef : factories) {
boolean loaded = false;
Expand Down Expand Up @@ -94,6 +97,9 @@ else if ("graal.js".equals(name)) {
defaultImplementation = Optional.ofNullable(Optional.ofNullable(instancesTypeCache.get("js"))
.orElse(instancesTypeCache.get("rjs")));
}

// Compile regex jscript pattern match
compileRegexPatternMatch();
}

private static ScriptingEvaluator getInstance() {
Expand All @@ -115,13 +121,40 @@ public static void contribute(String implementationName,

private ScriptableEvaluator identifyScriptingEngine(String expression) throws LscServiceException {
String[] parts = expression.split(":");
if (parts != null && parts.length > 0 && parts[0].length() < 10
&& instancesTypeCache.containsKey(parts[0])) {
return instancesTypeCache.get(parts[0]);
String match = matchJScriptEvaluator(parts[0]);
if (!match.isEmpty()) {
return instancesTypeCache.get(match);
}
return defaultImplementation.orElseThrow(() -> new LscServiceException("Missing Script evaluator"));
}

/**
* Matches the prefix specifying the jscript evaluator.
*
* @param jscript the prefix
* @return the matched jscript evaluator
*/
public String matchJScriptEvaluator(String jscript) {
for (Map.Entry<Pattern, String> entry : prefixRegex.entrySet()) {
if (entry.getKey().matcher(jscript).matches()) {
return entry.getValue();
}
}
return "";
}

/**
* Compiling Regex pattern to match jscript engine.
*/
public void compileRegexPatternMatch() {
for (String jscriptEngine: instancesTypeCache.keySet()) {
Pattern pattern = Pattern.compile("((\\n.*)+)" + jscriptEngine);
prefixRegex.put(pattern, jscriptEngine);
pattern = Pattern.compile(jscriptEngine);
prefixRegex.put(pattern, jscriptEngine);
}
}

/**
* Remove scripting engine prefix if required
* @param expression the expression
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/lsc/utils/ScriptingEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,39 @@ public void testBoolean() throws LscServiceException {
boolean booleanOutput = ScriptingEvaluator.evalToBoolean(task, expression, new HashMap<>());
assertTrue(booleanOutput);
}

@Test
public void testJSPrefix1() throws LscServiceException {
String expression = "\njs:true";
boolean booleanOutput = ScriptingEvaluator.evalToBoolean(task, expression, new HashMap<>());
assertTrue(booleanOutput);
}

@Test
public void testJSPrefix2() throws LscServiceException {
String expression = "\njs:\ntrue";
boolean booleanOutput = ScriptingEvaluator.evalToBoolean(task, expression, new HashMap<>());
assertTrue(booleanOutput);
}

@Test
public void testJSPrefix3() throws LscServiceException {
String expression = "\njs:test=\"js\"";
String stringOutput = ScriptingEvaluator.evalToString(task, expression, new HashMap<>());
assertEquals("js", stringOutput);
}

@Test
public void testJSPrefix4() throws LscServiceException {
String expression = "\njs:\ntest=\"js\"";
String stringOutput = ScriptingEvaluator.evalToString(task, expression, new HashMap<>());
assertEquals("js", stringOutput);
}

@Test
public void testJSPrefix5() throws LscServiceException {
String expression = "\n\njs:\n\ntest=\"js\"";
String stringOutput = ScriptingEvaluator.evalToString(task, expression, new HashMap<>());
assertEquals("js", stringOutput);
}
}
Loading