From 8329b096013bcbe489010fbc25cc433ae77495d5 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 23 Dec 2017 01:14:38 +0100 Subject: [PATCH] Fixed rules loading --- .../org/warp/picalculator/StaticVars.java | 2 +- .../java/org/warp/picalculator/Utils.java | 67 +++++++++++++++++++ .../picalculator/math/rules/RulesManager.java | 21 ++++-- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/warp/picalculator/StaticVars.java b/src/main/java/org/warp/picalculator/StaticVars.java index f0ef99c6..0ab7be8c 100644 --- a/src/main/java/org/warp/picalculator/StaticVars.java +++ b/src/main/java/org/warp/picalculator/StaticVars.java @@ -12,7 +12,7 @@ public class StaticVars { public static boolean debugOn; public static int outputLevel = 5; public static final boolean debugWindow2x = true; - public static final ClassLoader classLoader = StaticVars.class.getClassLoader(); + public static final Class classLoader = Main.instance.getClass(); private StaticVars() { diff --git a/src/main/java/org/warp/picalculator/Utils.java b/src/main/java/org/warp/picalculator/Utils.java index f4f99374..5bef4fe5 100755 --- a/src/main/java/org/warp/picalculator/Utils.java +++ b/src/main/java/org/warp/picalculator/Utils.java @@ -4,6 +4,8 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -16,9 +18,22 @@ import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.FileSystem; +import java.nio.file.FileSystemAlreadyExistsException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.nevec.rjm.BigDecimalMath; import org.nevec.rjm.Rational; @@ -39,6 +54,8 @@ import org.warp.picalculator.math.functions.Variable; import org.warp.picalculator.math.functions.equations.Equation; import org.warp.picalculator.math.functions.equations.EquationsSystemPart; +import com.jogamp.common.util.IOUtil; + import it.unimi.dsi.fastutil.objects.ObjectArrayList; public class Utils { @@ -744,4 +761,54 @@ public class Utils { t.add(o); return t; } + + public static Path getResource(String string) throws IOException, URISyntaxException { + URL res = Main.instance.getClass().getResource(string); + boolean isResource = res != null; + if (isResource) { + try { + final URI uri = res.toURI(); + if (res.getProtocol().equalsIgnoreCase("jar")) { + try { + FileSystems.newFileSystem(uri, Collections.emptyMap()); + } catch (FileSystemAlreadyExistsException e) { + FileSystems.getFileSystem(uri); + } + Path myFolderPath = Paths.get(uri); + return myFolderPath; + } else { + return Paths.get(uri); + } + } catch (java.lang.IllegalArgumentException e) { + throw e; + } + } else { + return Paths.get(string.substring(1)); + } + } + + public static InputStream getResourceStream(String string) throws IOException, URISyntaxException { + URL res = Main.instance.getClass().getResource(string); + boolean isResource = res != null; + if (isResource) { + try { + final URI uri = res.toURI(); + if (res.getProtocol().equalsIgnoreCase("jar")) { + try { + FileSystems.newFileSystem(uri, Collections.emptyMap()); + } catch (FileSystemAlreadyExistsException e) { + FileSystems.getFileSystem(uri); + } + Path myFolderPath = Paths.get(uri); + return Files.newInputStream(myFolderPath); + } else { + return Files.newInputStream(Paths.get(uri)); + } + } catch (java.lang.IllegalArgumentException e) { + throw e; + } + } else { + return Files.newInputStream(Paths.get(string.substring(1))); + } + } } diff --git a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java index 7c4a52ab..a8288ce5 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -4,6 +4,11 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.Writer; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; @@ -13,6 +18,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.script.Bindings; +import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; @@ -42,7 +49,7 @@ public class RulesManager { rules[val.ordinal()] = new ObjectArrayList(); } try { - final Path rulesPath = Paths.get(StaticVars.classLoader.getResource("rules.csv").toURI()); + final Path rulesPath = Utils.getResource("/rules.csv"); if (!Files.exists(rulesPath)) { throw new FileNotFoundException("rules.csv not found!"); } @@ -51,12 +58,13 @@ public class RulesManager { for (String rulesLine : ruleLines) { String[] ruleDetails = rulesLine.split(",", 1); String ruleName = ruleDetails[0]; - URL resourceURL = StaticVars.classLoader.getResource("rules" + File.separator + ruleName.replace(".", "_").replace('/', File.separatorChar) + ".js"); - if (resourceURL == null) { - throw new FileNotFoundException("rules/" + ruleName + ".js not found!"); + Utils.out.println("Evaluating /rules/" + ruleName + ".js"); + InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js"); + if (resourcePath == null) { + System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!")); + } else { + engine.eval(new InputStreamReader(resourcePath)); } - Path rulePath = Paths.get(resourceURL.toURI()); - engine.eval(new FileReader(rulePath.toString())); } } catch (URISyntaxException | IOException e) { e.printStackTrace(); @@ -67,7 +75,6 @@ public class RulesManager { } public static void addRule(Rule rule) { - MathContext mc = new MathContext(); rules[rule.getRuleType().ordinal()].add(rule); Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "Loaded rule " + rule.getRuleName()); }