- 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:"
- return SubmodelComponentDeserializer.create(model, resolvedID.substring(5), name);
+ 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);
+ throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")");
+ } else if (params != null && !JsonNull.INSTANCE.equals(params))
+ throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
+ if (resolvedID.startsWith("resource:"))
+ {
+ String[] parts = resolvedID.split(":");
+ if (parts.length != 3)
+ throw new IllegalArgumentException("invaild resource id: " + resolvedID);
+ String rLoadID = parts[1];
+ String resID = parts[2];
+ try
+ {
+ ResourceLoader loader;
+ if (!resourceLoaders.containsKey(rLoadID))
+ {
+ Class<?> c = Class.forName(rLoadID);
+ if (ResourceLoader.class.isAssignableFrom(c))
+ loader = (ResourceLoader) c.getConstructor().newInstance();
+ else
+ loader = (ResourceLoader) Objects.requireNonNull(c.getMethod("resourceLoader").invoke(null));
+ resourceLoaders.put(rLoadID, loader);
+ } else
+ {
+ loader = Objects.requireNonNull(resourceLoaders.get(parts[1]));
+ }
+ JsonObject jsonContents = JsonHandler.readJson(loader.loadResource(resID), JsonObject.class);
+ return loadComponentFromJsonObject(model, id, name, jsonContents);
+ }
+ catch (IOException e)
+ {
+ throw new UncheckedIOException(e);
+ }
+ catch (ClassCastException | ReflectiveOperationException e)
+ {
+ throw new IllegalArgumentException("invaild resource loader specified:" + parts[1], e);
+ }
+ } else if (resolvedID.startsWith("file:"))
+ {
+ try
+ {
+ String filename = resolvedID.substring(5);
+ JsonObject jsonContents = JsonHandler.readJson(filename, JsonObject.class);
+ return loadComponentFromJsonObject(model, id, name, jsonContents);
+ }
+ catch (IOException e)
+ {
+ throw new UncheckedIOException(e);
+ }
+ } else
+ {
+ throw new IllegalArgumentException("unable to resolve/interpret id" + resolvedID);
+ }
+ }