X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectGUIComponentCreator.java;h=c6d1529f08ef97692da0571d49c7e61d2ac43416;hb=6e4cba5f4efabd7c069f53fbf41336ee1c7db402;hp=6513f3ddb2d6159534134aa0356d99f34c0439e5;hpb=186fa572e73bcb0e2e7ff161698da45c8d46e228;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java index 6513f3dd..c6d1529f 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java @@ -3,30 +3,39 @@ package net.mograsim.logic.model.serializing; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; +import java.util.Objects; import com.google.gson.JsonElement; import com.google.gson.JsonNull; +import com.google.gson.JsonObject; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.components.GUIComponent; -import net.mograsim.logic.model.snippets.CodeSnippetSupplier; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.util.JsonHandler; public class IndirectGUIComponentCreator { private static final Map standardComponentIDs = new HashMap<>(); - private static final Set standardComponentIDSetUnmodifiable = Collections.unmodifiableSet(standardComponentIDs.keySet()); + private static final Map standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs); private static final Map componentSuppliers = new HashMap<>(); + private static final Map resourceLoaders = new HashMap<>(); + private static final Map componentCache = new HashMap<>(); + private static final ResourceLoader defaultResourceLoader; static { - try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json")) + defaultResourceLoader = ClassLoaderBasedResourceLoader.create(IndirectGUIComponentCreator.class.getClassLoader()); + loadStandardComponentIDs(IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json")); + } + + public static void loadStandardComponentIDs(InputStream standardComponentIdMappingStream) + { + try (InputStream s = standardComponentIdMappingStream) { if (s == null) throw new IOException("Resource not found"); @@ -52,19 +61,19 @@ public class IndirectGUIComponentCreator public static void addStandardComponentID(String standardComponentID, String associatedComponentID) { - if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:")) + if (!checkIDIsValidResolvedID(associatedComponentID)) throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID); standardComponentIDs.put(standardComponentID, associatedComponentID); } - public static Collection getStandardComponentIDs() + public static Map getStandardComponentIDs() { - return standardComponentIDSetUnmodifiable; + return standardComponentIDsUnmodifiable; } - public static void setComponentSupplier(String className, ComponentSupplier componentSupplier) + public static void setComponentSupplier(String id, ComponentSupplier componentSupplier) { - componentSuppliers.put(className, componentSupplier); + componentSuppliers.put(id, componentSupplier); } public static GUIComponent createComponent(ViewModelModifiable model, String id) @@ -84,40 +93,126 @@ public class IndirectGUIComponentCreator public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name) { - if (id != null) + if (id == null) + throw new NullPointerException("Component ID is null"); + if (componentCache.containsKey(id)) + return loadComponentFromJsonObject(model, id, name, componentCache.get(id)); + String resolvedID = resolveID(id); + if (resolvedID == null) + throw new IllegalArgumentException("Unknown standard ID or illegal resolved ID: " + id); + String[] parts = resolvedID.split(":"); + String firstPart = parts[0]; + if (firstPart.equals("jsonfile")) { - String resolvedID; - if (id.startsWith("class:") || id.startsWith("file:")) - resolvedID = id; - else - resolvedID = standardComponentIDs.get(id); - if (resolvedID.startsWith("class:")) - { - String className = resolvedID.substring(6); - tryLoadComponentClass(className); - ComponentSupplier componentSupplier = componentSuppliers.get(className); - if (componentSupplier != null) - return componentSupplier.create(model, params, name); - } else - // we know id has to start with "file:" here - // because standardComponentIDs only contains strings starting with "class:" or "file:" - if (params != null && !JsonNull.INSTANCE.equals(params)) - throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file"); + JsonObject jsonContents; try { - return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null); + // don't use parts[1], because the path could contain ':' + jsonContents = JsonHandler.readJson(resolvedID.substring("jsonfile:".length()), JsonObject.class); } catch (IOException e) { - throw new UncheckedIOException(e); + throw new UncheckedIOException("Error loading JSON file", e); } + return loadComponentFromJsonObject(model, id, name, jsonContents); + } + ResourceLoader loader; + String resTypeID; + String resID; + if (firstPart.equals("resloader")) + { + String loaderID = parts[1]; + loader = resourceLoaders.get(loaderID); + if (loader == null) + tryLoadResourceLoader(loaderID); + loader = resourceLoaders.get(loaderID); + if (loader == null) + throw new IllegalArgumentException( + "Unknown resource loader: " + loaderID + " (but class was found. Probably the static initializer is missing)"); + resTypeID = parts[2]; + resID = parts[3]; + } else + { + loader = defaultResourceLoader; + resTypeID = parts[0]; + resID = parts[1]; } - throw new RuntimeException("Could not get component supplier for ID " + id); + if (resTypeID.equals("jsonres")) + { + JsonObject jsonContents; + try + { + @SuppressWarnings("resource") // jsonStream is closed in JsonHandler + InputStream jsonStream = Objects.requireNonNull(loader.loadResource(resID), "Error loading JSON resource: Not found"); + jsonContents = JsonHandler.readJson(jsonStream, JsonObject.class); + } + catch (IOException e) + { + throw new UncheckedIOException("Error loading JSON resource", e); + } + return loadComponentFromJsonObject(model, id, name, jsonContents); + } else if (resTypeID.equals("class")) + { + ComponentSupplier componentSupplier = componentSuppliers.get(resID); + if (componentSupplier == null) + try + { + loader.loadClass(resID); + } + catch (@SuppressWarnings("unused") ClassNotFoundException e) + { + throw new IllegalArgumentException("Unknown component supplier: " + resID); + } + componentSupplier = componentSuppliers.get(resID); + if (componentSupplier == null) + throw new IllegalArgumentException( + "Unknown component supplier: " + resID + " (but class was found. Probably the static initializer is missing)"); + return componentSupplier.create(model, params, name); + } else + throw new IllegalStateException("Unknown resource type ID: " + resTypeID); + } + + public static String resolveID(String id) + { + if (checkIDIsValidResolvedID(id)) + return id; + return standardComponentIDs.get(id); + } + + private static boolean checkIDIsValidResolvedID(String id) + { + return id.matches("jsonfile:(.+)|(resloader:([^:]+):)?(jsonres|class):[^:]+"); + } + + private static SubmodelComponent loadComponentFromJsonObject(ViewModelModifiable model, String id, String name, JsonObject jsonContents) + { + componentCache.putIfAbsent(id, jsonContents); + SerializablePojo jsonContentsAsSerializablePojo = JsonHandler.parser.fromJson(jsonContents, SerializablePojo.class); + if (jsonContentsAsSerializablePojo.version == null) + return LegacySubmodelComponentSerializer.deserialize(model, + JsonHandler.parser.fromJson(jsonContents, LegacySubmodelComponentParams.class), name, id, null); + return SubmodelComponentSerializer.deserialize(model, JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class), + name, id, null); + } + + public static void registerResourceLoader(ResourceLoader resourceLoader) + { + registerResourceLoader(resourceLoader, resourceLoader.getClass()); + } + + public static void registerResourceLoader(ResourceLoader resourceLoader, Class reference) + { + resourceLoaders.put(reference.getName(), Objects.requireNonNull(resourceLoader)); + } + + public static void registerResourceLoader(ResourceLoader resourceLoader, String reference) + { + resourceLoaders.put(reference, Objects.requireNonNull(resourceLoader)); } - private static void tryLoadComponentClass(String componentClassName) + private static void tryLoadResourceLoader(String loaderClassName) { - CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n"); + ReflectionHelper.tryInvokeStaticInitializer(loaderClassName, "Error loading resoruce loader %s: %s\n"); } public static interface ComponentSupplier