mapping.json is now loaded by the class loader
authorFabian Stemmler <stemmler@in.tum.de>
Wed, 26 Jun 2019 19:11:21 +0000 (21:11 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Wed, 26 Jun 2019 19:11:21 +0000 (21:11 +0200)
net.mograsim.logic.ui.am2900/components/mapping.json [deleted file]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponentCreator.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json [new file with mode: 0644]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java

diff --git a/net.mograsim.logic.ui.am2900/components/mapping.json b/net.mograsim.logic.ui.am2900/components/mapping.json
deleted file mode 100644 (file)
index 6c7a835..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-mograsim version: 0.1.2
-{
-  "GUIAm2901": "file:components/am2901/GUIAm2901.json",
-  "GUIAm2901ALUFuncDecode": "file:components/am2901/GUIAm2901ALUFuncDecode.json",
-  "GUIAm2901ALUInclDecode": "file:components/am2901/GUIAm2901ALUInclDecode.json",
-  "GUIAm2901ALUInclSourceDecodeInclFunctionDecode": "file:components/am2901/GUIAm2901ALUInclSourceDecodeInclFunctionDecode.json",
-  "GUIAm2901ALUOneBit": "file:components/am2901/GUIAm2901ALUOneBit.json",
-  "GUIAm2901DestDecode": "file:components/am2901/GUIAm2901DestDecode.json",
-  "GUIAm2901QReg": "file:components/am2901/GUIAm2901QReg.json",
-  "GUIAm2901SourceDecode": "file:components/am2901/GUIAm2901SourceDecode.json",
-  "GUIAndGate": "class:net.mograsim.logic.ui.model.components.GUIAndGate",
-  "GUINandGate": "class:net.mograsim.logic.ui.model.components.GUINandGate",
-  "GUIOrGate": "class:net.mograsim.logic.ui.model.components.GUIOrGate",
-  "GUI_rsLatch": "file:components/GUI_rsLatch.json",
-  "GUIand": "file:components/GUIand.json",
-  "GUIand41": "file:components/GUIand41.json",
-  "GUIandor414": "file:components/GUIandor414.json",
-  "GUIdemux2": "file:components/GUIdemux2.json",
-  "GUIdff": "file:components/GUIdff.json",
-  "GUIdlatch": "file:components/GUIdlatch.json",
-  "GUIdlatch4": "file:components/GUIdlatch4.json",
-  "GUIfulladder": "file:components/GUIfulladder.json",
-  "GUIhalfadder": "file:components/GUIhalfadder.json",
-  "GUImux1": "file:components/GUImux1.json",
-  "GUImux1_4": "file:components/GUImux1_4.json",
-  "GUInand3": "file:components/GUInand3.json",
-  "GUInot4": "file:components/GUInot4.json",
-  "GUIor4": "file:components/GUIor4.json",
-  "GUIor_4": "file:components/GUIor_4.json",
-  "GUIram2": "file:components/GUIram2.json",
-  "GUIram4": "file:components/GUIram4.json",
-  "GUIsel2_4": "file:components/GUIsel2_4.json",
-  "GUIsel3_4": "file:components/GUIsel3_4.json",
-  "GUIxor": "file:components/GUIxor.json",
-  "WireCrossPoint": "class:net.mograsim.logic.ui.model.wires.WireCrossPoint"
-}
\ No newline at end of file
index c8e3333..76b8919 100644 (file)
@@ -1,6 +1,7 @@
 package net.mograsim.logic.ui.model.components;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
@@ -13,15 +14,13 @@ import net.mograsim.logic.ui.util.JsonHandler;
 public class GUIComponentCreator
 {
        private final static Map<String, String> componentMapping;
-       private final static String componentMappingPath = "../net.mograsim.logic.ui.am2900/components/mapping.json"; // TODO: manage this
-                                                                                                                                                                                                                                       // somewhere else
 
        static
        {
                Map<String, String> tmp;
-               try
+               try (InputStream s = GUIComponentCreator.class.getResourceAsStream("./mapping.json"))
                {
-                       tmp = JsonHandler.readJson(componentMappingPath, Map.class);
+                       tmp = JsonHandler.readJson(s, Map.class);
                }
                catch (IOException e)
                {
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json
new file mode 100644 (file)
index 0000000..6c7a835
--- /dev/null
@@ -0,0 +1,36 @@
+mograsim version: 0.1.2
+{
+  "GUIAm2901": "file:components/am2901/GUIAm2901.json",
+  "GUIAm2901ALUFuncDecode": "file:components/am2901/GUIAm2901ALUFuncDecode.json",
+  "GUIAm2901ALUInclDecode": "file:components/am2901/GUIAm2901ALUInclDecode.json",
+  "GUIAm2901ALUInclSourceDecodeInclFunctionDecode": "file:components/am2901/GUIAm2901ALUInclSourceDecodeInclFunctionDecode.json",
+  "GUIAm2901ALUOneBit": "file:components/am2901/GUIAm2901ALUOneBit.json",
+  "GUIAm2901DestDecode": "file:components/am2901/GUIAm2901DestDecode.json",
+  "GUIAm2901QReg": "file:components/am2901/GUIAm2901QReg.json",
+  "GUIAm2901SourceDecode": "file:components/am2901/GUIAm2901SourceDecode.json",
+  "GUIAndGate": "class:net.mograsim.logic.ui.model.components.GUIAndGate",
+  "GUINandGate": "class:net.mograsim.logic.ui.model.components.GUINandGate",
+  "GUIOrGate": "class:net.mograsim.logic.ui.model.components.GUIOrGate",
+  "GUI_rsLatch": "file:components/GUI_rsLatch.json",
+  "GUIand": "file:components/GUIand.json",
+  "GUIand41": "file:components/GUIand41.json",
+  "GUIandor414": "file:components/GUIandor414.json",
+  "GUIdemux2": "file:components/GUIdemux2.json",
+  "GUIdff": "file:components/GUIdff.json",
+  "GUIdlatch": "file:components/GUIdlatch.json",
+  "GUIdlatch4": "file:components/GUIdlatch4.json",
+  "GUIfulladder": "file:components/GUIfulladder.json",
+  "GUIhalfadder": "file:components/GUIhalfadder.json",
+  "GUImux1": "file:components/GUImux1.json",
+  "GUImux1_4": "file:components/GUImux1_4.json",
+  "GUInand3": "file:components/GUInand3.json",
+  "GUInot4": "file:components/GUInot4.json",
+  "GUIor4": "file:components/GUIor4.json",
+  "GUIor_4": "file:components/GUIor_4.json",
+  "GUIram2": "file:components/GUIram2.json",
+  "GUIram4": "file:components/GUIram4.json",
+  "GUIsel2_4": "file:components/GUIsel2_4.json",
+  "GUIsel3_4": "file:components/GUIsel3_4.json",
+  "GUIxor": "file:components/GUIxor.json",
+  "WireCrossPoint": "class:net.mograsim.logic.ui.model.wires.WireCrossPoint"
+}
\ No newline at end of file
index dc8ace3..99ff8d1 100644 (file)
@@ -1,9 +1,11 @@
 package net.mograsim.logic.ui.util;
 
 import java.io.BufferedReader;
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
@@ -14,7 +16,18 @@ public class JsonHandler
 
        public static <T> T readJson(String path, Class<T> type) throws IOException
        {
-               try (FileReader reader = new FileReader(path); BufferedReader bf = new BufferedReader(reader))
+               try (FileInputStream jsonStream = new FileInputStream(path))
+               {
+                       return readJson(jsonStream, type);
+               }
+       }
+
+       /**
+        * @param input The Stream is closed after being read
+        */
+       public static <T> T readJson(InputStream input, Class<T> type) throws IOException
+       {
+               try (InputStreamReader reader = new InputStreamReader(input); BufferedReader bf = new BufferedReader(reader))
                {
                        String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y));
                        T params = parser.fromJson(json, type);