+ 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));