From c5b5d3cb19298d33d787b5a3e0a87ce5d7f4a1a0 Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Wed, 26 Jun 2019 21:11:21 +0200 Subject: [PATCH] mapping.json is now loaded by the class loader --- .../model/components/GUIComponentCreator.java | 7 +++---- .../logic/ui/model}/components/mapping.json | 0 .../net/mograsim/logic/ui/util/JsonHandler.java | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) rename {net.mograsim.logic.ui.am2900 => net.mograsim.logic.ui/src/net/mograsim/logic/ui/model}/components/mapping.json (100%) 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.am2900/components/mapping.json b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json similarity index 100% rename from net.mograsim.logic.ui.am2900/components/mapping.json rename to net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/mapping.json 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); -- 2.17.1