1 package org.catacomb.druid.load;
2
3 import java.io.File;
4
5 import org.catacomb.interlish.service.ContentLoader;
6 import org.catacomb.interlish.structure.Factory;
7
8
9
10
11 import org.catacomb.be.FileSourcable;
12 import org.catacomb.interlish.resource.ResourceRole;
13 import org.catacomb.interlish.resource.Role;
14 import org.catacomb.interlish.service.ResourceAccess;
15 import org.catacomb.interlish.structure.*;
16 import org.catacomb.interlish.util.JUtil;
17 import org.catacomb.report.E;
18 import org.catacomb.serial.Deserializer;
19 import org.catacomb.druid.manifest.*;
20 import org.catacomb.util.FileUtil;
21 import org.catacomb.util.NetUtil;
22 import org.catacomb.util.PathUtil;
23
24 import java.net.URL;
25
26
27
28
29 public class DruidContentLoader implements ContentLoader {
30
31
32 private XMLStore store;
33
34 private RoleMap roleMap;
35
36
37
38 Class rootClass;
39
40
41
42 public static void initLoader(Object projroot) {
43 DruidContentLoader loader = new DruidContentLoader();
44 ResourceAccess.setContentLoader(loader);
45 loader.init(projroot);
46 }
47
48 private DruidContentLoader() {
49 }
50
51
52
53 public static Object getFromURL(URL url) {
54 String s = NetUtil.readStringFromURL(url);
55 Object obj = Deserializer.deserialize(s);
56 return obj;
57 }
58
59
60
61
62 public void init(Object projroot) {
63 store = new XMLStore();
64 roleMap = new RoleMap();
65
66
67
68 String xtxt = JUtil.getRelativeResource("DecManifest.xml");
69 DecManifest dm = (DecManifest)(Deserializer.deserialize(xtxt));
70 roleMap.addRoles(dm);
71 store.addClasspathManifest(dm);
72
73
74 if (projroot != null) {
75 String atxt = JUtil.getRelativeResource(projroot, "DecManifest.xml");
76 DecManifest adm = (DecManifest)(Deserializer.deserialize(atxt));
77 roleMap.addRoles(adm);
78 store.addClasspathManifest(adm);
79
80
81 }
82 }
83
84
85
86
87 public void addFileSystemManifest(DecManifest dm, File fdtop) {
88 store.addFileSystemManifest(dm, fdtop);
89 }
90
91
92 public Object loadFromFileNoCache(File f) {
93 String s = FileUtil.readStringFromFile(f);
94 Object obj = Deserializer.deserialize(s);
95 return obj;
96 }
97
98
99 public Object getResource(String name, String selector) {
100 DecFile decFile = getSource(name, selector);
101 Object ret = null;
102 if (decFile != null) {
103 if (decFile.hasCachedObject()) {
104
105 } else {
106
107
108 loadToObject(decFile);
109 }
110 ret = decFile.getCachedObject();
111 }
112 return ret;
113 }
114
115
116
117
118 private void loadToObject(DecFile decFile) {
119 if (decFile.hasCachedText()) {
120 loadObject(decFile);
121
122 } else {
123 loadText(decFile);
124 loadObject(decFile);
125 }
126 }
127
128
129 private void loadObject(DecFile decFile) {
130
131 E.cacheAction(decFile.getFullID());
132 String ppath = PathUtil.parentPackage(decFile.getFullID());
133 Object obj = readObject(decFile.getCachedText(), ppath);
134 decFile.setCachedObject(obj);
135 if (obj instanceof FileSourcable && decFile.inFileSystem()) {
136 ((FileSourcable)obj).setSourceFile(decFile.getSourceFile());
137 }
138 }
139
140
141 public Object readObject(String txt) {
142 return readObject(txt, null);
143 }
144
145
146 public Object readObject(String txt, String srcPath) {
147
148 Object obj = Deserializer.deserialize(txt);
149
150 if (obj instanceof PathLocated) {
151 ((PathLocated)obj).setPathLocation(srcPath);
152 }
153
154
155 if (obj instanceof Transitional) {
156 obj = ((Transitional)obj).getOutcome();
157 }
158
159 if (obj instanceof PathLocated) {
160 ((PathLocated)obj).setPathLocation(srcPath);
161 }
162
163 if (obj instanceof Resolvable) {
164 ((Resolvable)obj).resolve();
165 }
166 return obj;
167 }
168
169
170 private void loadText(DecFile df) {
171 if (df.inClasspath()) {
172 String stxt = JUtil.getXMLResource(df.getFullID());
173 if (stxt != null) {
174 df.setCachedText(stxt);
175 } else {
176 E.error("cant read " + df.getFullID() + " no such resource");
177 df.setCachedText("");
178 }
179
180 } else if (df.inFileSystem()) {
181 File fsrc = df.getSourceFile();
182
183 String s = FileUtil.readStringFromFile(fsrc);
184 if (s != null) {
185 df.setCachedText(s);
186 } else {
187 E.error("cant read " + fsrc + " no such file");
188 df.setCachedText("");
189 }
190 }
191 }
192
193
194
195
196
197 private DecFile getSource(String location, String selectorin) {
198 DecFile decFile = null;
199 String selector = selectorin;
200
201 if (selector == null) {
202 selector = " ";
203 }
204
205 if (store.containsSource(location)) {
206 if (store.hasMultipleSources(location)) {
207
208 int ibm = 1000;
209 for (DecFile df : store.getSources(location)) {
210 if (!df.hasCachedText()) {
211 loadText(df);
212 }
213 String s = df.getCachedText();
214 int isel = s.indexOf(selector);
215 if (isel >= 0 && isel < ibm) {
216 decFile = df;
217 ibm = isel;
218
219 } else {
220
221 }
222 }
223 if (decFile == null) {
224 E.error("there are multiple sources named " + location + " but the selector " +
225 selector + " does not match text in any of them");
226 } else {
227
228 }
229
230
231 } else {
232 decFile = store.getSource(location);
233 }
234
235 } else if (location.startsWith("file:")) {
236 decFile = new DecFile(location);
237 store.addSource(decFile);
238 } else {
239 E.error("XMLStore cant find " + location + " ");
240 }
241 return decFile;
242 }
243
244
245
246
247
248 public boolean hasEditorOf(String s) {
249 return hasRolePlayer("edits", s);
250 }
251
252
253
254 public String getEditorPath(String s) {
255 Role r = roleMap.getRole("edits", s);
256 DecFile decFile = getSource(r.getResource(), null);
257 return decFile.getFullID();
258 }
259
260
261
262 public boolean hasFactoryFor(String s) {
263 return (hasRolePlayer("makes", s) || hasRolePlayer("domainof", s));
264 }
265
266
267 public Factory getFactoryFor(String s) {
268 Factory fac = null;
269 if (hasRolePlayer("makes", s)) {
270 fac = (Factory)(getRolePlayer("makes", s));
271 } else {
272 fac = (Factory)(getRolePlayer("domainof", s));
273 }
274 return fac;
275 }
276
277
278
279
280
281 public boolean hasRunMappingFor(String s) {
282 return hasRolePlayer("runmaps", s);
283 }
284
285
286 public Object getRunMappingFor(String s) {
287 return getRolePlayer("runmaps", s);
288 }
289
290
291
292 public boolean hasProviderOf(String s) {
293 return hasRolePlayer("provides", s);
294 }
295
296
297 public Object getProviderOf(String s) {
298 return getNewRolePlayer("provides", s);
299 }
300
301
302
303 public boolean hasExposerOf(String s) {
304 return hasRolePlayer("exposes", s);
305 }
306
307
308 public Object getExposerOf(String s) {
309 return getRolePlayer("exposes", s);
310 }
311
312
313
314 public boolean hasTablizerOf(String s) {
315 return hasRolePlayer("tablizes", s);
316 }
317
318
319 public Object getTablizerOf(String s) {
320 return getRolePlayer("tablizes", s);
321 }
322
323
324 private boolean hasRolePlayer(String role, String subject) {
325 return roleMap.hasRole(role, subject);
326 }
327
328 private Object getRolePlayer(String role, String subject) {
329 return getRolePlayer(role, subject, true);
330 }
331
332 private Object getNewRolePlayer(String role, String subject) {
333 return getRolePlayer(role, subject, false);
334 }
335
336
337 private Object getRolePlayer(String role, String subject, boolean cacheOK) {
338 Object ret = null;
339 Role r = roleMap.getRole(role, subject);
340 if (r.hasCachedPlayer() && cacheOK) {
341 ret = r.getCachedPlayer();
342
343 } else {
344 if (r instanceof ClassRole) {
345 ret = readClassRole((ClassRole)r);
346
347 } else if (r instanceof ResourceRole) {
348 ret = readResourceRole((ResourceRole)r);
349 }
350 }
351 r.cachePlayer(ret);
352 return ret;
353 }
354
355
356
357 private Object readClassRole(ClassRole cr) {
358 String cnm = cr.getResource();
359
360 return JUtil.newInstance(cnm);
361 }
362
363
364 private Object readResourceRole(ResourceRole rr) {
365 String resnm = rr.getResource();
366 Object ret = getResource(resnm, "ole");
367 return ret;
368 }
369
370
371 public void newSourceFile(File f, File rootFolder) {
372 store.newSourceFile(f, rootFolder);
373
374 }
375
376
377
378
379
380
381
382
383
384
385 }