926e48b5b9dfd34936953cb3e26c6a1a2ccc4c4f
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / IndirectModelComponentCreator.java
1 package net.mograsim.logic.model.serializing;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UncheckedIOException;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.Objects;
10
11 import com.google.gson.JsonElement;
12 import com.google.gson.JsonNull;
13
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
17 import net.mograsim.logic.model.util.JsonHandler;
18
19 public class IndirectModelComponentCreator
20 {
21         private static final Map<String, String> standardComponentIDs = new HashMap<>();
22         private static final Map<String, String> standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs);
23
24         private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
25         private static final Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
26         private static final Map<String, SubmodelComponentParams> componentCache = new HashMap<>();
27
28         private static final ResourceLoader defaultResourceLoader;
29         static
30         {
31                 defaultResourceLoader = ClassLoaderBasedResourceLoader.create(IndirectModelComponentCreator.class.getClassLoader());
32                 loadStandardComponentIDs(IndirectModelComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json"));
33         }
34
35         public static void loadStandardComponentIDs(InputStream standardComponentIdMappingStream)
36         {
37                 try (InputStream s = standardComponentIdMappingStream)
38                 {
39                         if (s == null)
40                                 throw new IOException("Resource not found");
41                         Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
42                         // don't use putAll to apply sanity checks
43                         tmp.forEach((st, id) ->
44                         {
45                                 try
46                                 {
47                                         addStandardComponentID(st, id);
48                                 }
49                                 catch (IllegalArgumentException e)
50                                 {
51                                         System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
52                                 }
53                         });
54                 }
55                 catch (IOException e)
56                 {
57                         System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
58                 }
59         }
60
61         public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
62         {
63                 if (!checkIDIsValidResolvedID(associatedComponentID))
64                         throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
65                 standardComponentIDs.put(standardComponentID, associatedComponentID);
66         }
67
68         public static Map<String, String> getStandardComponentIDs()
69         {
70                 return standardComponentIDsUnmodifiable;
71         }
72
73         public static void setComponentSupplier(String id, ComponentSupplier componentSupplier)
74         {
75                 componentSuppliers.put(id, componentSupplier);
76         }
77
78         public static ModelComponent createComponent(LogicModelModifiable model, String id)
79         {
80                 return createComponent(model, id, (String) null);
81         }
82
83         public static ModelComponent createComponent(LogicModelModifiable model, String id, String name)
84         {
85                 return createComponent(model, id, JsonNull.INSTANCE, name);
86         }
87
88         public static ModelComponent createComponent(LogicModelModifiable model, String id, JsonElement params)
89         {
90                 return createComponent(model, id, params, null);
91         }
92
93         public static ModelComponent createComponent(LogicModelModifiable model, String id, JsonElement params, String name)
94         {
95                 if (id == null)
96                         throw new NullPointerException("Component ID is null");
97                 if (componentCache.containsKey(id))
98                         return loadComponentFromJsonObject(model, id, name, componentCache.get(id));
99                 String resolvedID = resolveID(id);
100                 if (resolvedID == null)
101                         throw new IllegalArgumentException("Unknown standard ID or illegal resolved ID: " + id);
102                 String[] parts = resolvedID.split(":");
103                 String firstPart = parts[0];
104                 if (firstPart.equals("jsonfile"))
105                 {
106                         SubmodelComponentParams jsonContents;
107                         try
108                         {
109                                 // don't use parts[1], because the path could contain ':'
110                                 jsonContents = JsonHandler.readJson(resolvedID.substring("jsonfile:".length()), SubmodelComponentParams.class);
111                         }
112                         catch (IOException e)
113                         {
114                                 throw new UncheckedIOException("Error loading JSON file", e);
115                         }
116                         return loadComponentFromJsonObject(model, id, name, jsonContents);
117                 }
118                 ResourceLoader loader;
119                 String resTypeID;
120                 String resID;
121                 if (firstPart.equals("resloader"))
122                 {
123                         String loaderID = parts[1];
124                         loader = resourceLoaders.get(loaderID);
125                         if (loader == null)
126                                 tryLoadResourceLoader(loaderID);
127                         loader = resourceLoaders.get(loaderID);
128                         if (loader == null)
129                                 throw new IllegalArgumentException(
130                                                 "Unknown resource loader: " + loaderID + " (but class was found. Probably the static initializer is missing)");
131                         resTypeID = parts[2];
132                         resID = parts[3];
133                 } else
134                 {
135                         loader = defaultResourceLoader;
136                         resTypeID = parts[0];
137                         resID = parts[1];
138                 }
139                 if (resTypeID.equals("jsonres"))
140                 {
141                         SubmodelComponentParams jsonContents;
142                         try
143                         {
144                                 @SuppressWarnings("resource") // jsonStream is closed in JsonHandler
145                                 InputStream jsonStream = Objects.requireNonNull(loader.loadResource(resID), "Error loading JSON resource: Not found");
146                                 jsonContents = JsonHandler.readJson(jsonStream, SubmodelComponentParams.class);
147                         }
148                         catch (IOException e)
149                         {
150                                 throw new UncheckedIOException("Error loading JSON resource", e);
151                         }
152                         return loadComponentFromJsonObject(model, id, name, jsonContents);
153                 } else if (resTypeID.equals("class"))
154                 {
155                         ComponentSupplier componentSupplier = componentSuppliers.get(resID);
156                         if (componentSupplier == null)
157                                 try
158                                 {
159                                         loader.loadClass(resID);
160                                 }
161                                 catch (@SuppressWarnings("unused") ClassNotFoundException e)
162                                 {
163                                         throw new IllegalArgumentException("Unknown component supplier: " + resID);
164                                 }
165                         componentSupplier = componentSuppliers.get(resID);
166                         if (componentSupplier == null)
167                                 throw new IllegalArgumentException(
168                                                 "Unknown component supplier: " + resID + " (but class was found. Probably the static initializer is missing)");
169                         return componentSupplier.create(model, params, name);
170                 } else
171                         throw new IllegalStateException("Unknown resource type ID: " + resTypeID);
172         }
173
174         public static String resolveID(String id)
175         {
176                 if (checkIDIsValidResolvedID(id))
177                         return id;
178                 return standardComponentIDs.get(id);
179         }
180
181         private static boolean checkIDIsValidResolvedID(String id)
182         {
183                 return id.matches("jsonfile:(.+)|(resloader:([^:]+):)?(jsonres|class):[^:]+");
184         }
185
186         private static SubmodelComponent loadComponentFromJsonObject(LogicModelModifiable model, String id, String name,
187                         SubmodelComponentParams jsonContents)
188         {
189                 componentCache.putIfAbsent(id, jsonContents);
190                 return SubmodelComponentSerializer.deserialize(model, jsonContents, name, id, null);
191         }
192
193         public static void clearComponentCache()
194         {
195                 componentCache.clear();
196         }
197
198         public static void registerResourceLoader(ResourceLoader resourceLoader)
199         {
200                 registerResourceLoader(resourceLoader, resourceLoader.getClass());
201         }
202
203         public static void registerResourceLoader(ResourceLoader resourceLoader, Class<?> reference)
204         {
205                 resourceLoaders.put(reference.getName(), Objects.requireNonNull(resourceLoader));
206         }
207
208         public static void registerResourceLoader(ResourceLoader resourceLoader, String reference)
209         {
210                 resourceLoaders.put(reference, Objects.requireNonNull(resourceLoader));
211         }
212
213         private static void tryLoadResourceLoader(String loaderClassName)
214         {
215                 ReflectionHelper.tryInvokeStaticInitializer(loaderClassName, "Error loading resoruce loader %s: %s\n");
216         }
217
218         public static interface ComponentSupplier
219         {
220                 public ModelComponent create(LogicModelModifiable model, JsonElement params, String name);
221         }
222 }