From: Fabian Stemmler Date: Wed, 26 Jun 2019 19:11:21 +0000 (+0200) Subject: mapping.json is now loaded by the class loader X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=c5b5d3cb19298d33d787b5a3e0a87ce5d7f4a1a0;p=Mograsim.git mapping.json is now loaded by the class loader --- diff --git a/net.mograsim.logic.ui.am2900/components/mapping.json b/net.mograsim.logic.ui.am2900/components/mapping.json deleted file mode 100644 index 6c7a8350..00000000 --- a/net.mograsim.logic.ui.am2900/components/mapping.json +++ /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 diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponentCreator.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponentCreator.java index c8e33333..76b89190 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponentCreator.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponentCreator.java @@ -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 componentMapping; - private final static String componentMappingPath = "../net.mograsim.logic.ui.am2900/components/mapping.json"; // TODO: manage this - // somewhere else static { Map 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 index 00000000..6c7a8350 --- /dev/null +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json @@ -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 diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java index dc8ace3d..99ff8d1d 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java @@ -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 readJson(String path, Class 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 readJson(InputStream input, Class 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);