Skip to content

Commit

Permalink
Fix: Added a function to improve jscript prefix identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
abpai94 committed Sep 4, 2024
1 parent 895059b commit a70130d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 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 @@ -41,7 +42,7 @@ public class ScriptingEvaluator {
}

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 @@ -115,13 +116,33 @@ 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 (String jscriptEngine : instancesTypeCache.keySet()) {
Pattern pattern = Pattern.compile("\\n.*" + jscriptEngine);
if (pattern.matcher(jscript).matches()) {
return jscriptEngine;
}
pattern = Pattern.compile(jscriptEngine);
if (pattern.matcher(jscript).matches()) {
return jscriptEngine;
}
}
return "";
}

/**
* Remove scripting engine prefix if required
* @param expression the expression
Expand Down

0 comments on commit a70130d

Please sign in to comment.