MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / loadSingleBox

Method loadSingleBox

src/main/java/fieldbox/io/IO.java:475–559  ·  view source on GitHub ↗

notes: it's up to the caller to call .loaded() once this box has been connected to the graph

(String f, Box root)

Source from the content-addressed store, hash-verified

473 public Box loadSingleBox(String f, Box root) {
474
475 Map<String, List<Object>> options = null;
476 if (pluginList != null) try {
477 options = pluginList.read(f, false);
478 pluginList.interpretClassPathAndOptions(options);
479 } catch (IOException e) {
480 e.printStackTrace();
481 }
482
483 Map<Object, String> m = (Map) (serializeFromString(readFromFile(filenameFor(f))));
484
485 String boxClass = m.getOrDefault("__boxclass__", Box.class.getName());
486 Box box;
487
488 try {
489 Class c = this.getClass()
490 .getClassLoader()
491 .loadClass(boxClass);
492 try {
493 Constructor<Box> cc = c.getDeclaredConstructor();
494 cc.setAccessible(true);
495 box = cc.newInstance();
496 } catch (NoSuchMethodException e) {
497 Constructor<Box> cc = c.getDeclaredConstructor(Box.class);
498 cc.setAccessible(true);
499 box = cc.newInstance(root);
500 }
501 } catch (Throwable e) {
502 Log.log("io.error", () -> " while looking for class <" + boxClass + "> an exception was thrown");
503 Log.log("io.error", () -> e);
504 Log.log("io.error", () -> " will proceed with just a vanilla Box class, but custom behavior will be lost ");
505 box = new Box();
506 }
507
508 String currentPrefix = new File(f).getParent();
509
510
511 for (Map.Entry<?, ?> entry : m.entrySet()) {
512 if (!(entry.getKey() instanceof String)) continue;
513
514 box.properties.put(new Dict.Prop((String) entry.getKey()), entry.getValue());
515
516 if (((String) entry.getKey()).startsWith("__filename__")) {
517 String suffix = ((String) entry.getKey()).substring("__filename__".length());
518
519 String p = (String) entry.getValue();
520
521 String additionalSearchPath = p.replace(WORKSPACE, currentPrefix + "/");
522 if (new File(additionalSearchPath).exists()) {
523 String p1 = readFromFile(new File(additionalSearchPath));
524 box.properties.put(new Dict.Prop<String>(suffix), p1);
525 } else {
526 File path = filenameFor(p);
527 try {
528
529 String p1 = readFromFile(path);
530 box.properties.put(new Dict.Prop<String>(suffix), p1);
531 } catch (Exception e) {
532 System.err.println(" exception thrown while loading a file :" + path + " for property :" + entry + " in box +" + box);

Callers 3

loadBoxMethod · 0.80
loadBoxMethod · 0.80
loadBoxMethod · 0.80

Calls 15

serializeFromStringMethod · 0.95
readFromFileMethod · 0.95
filenameForMethod · 0.95
logMethod · 0.95
replaceMethod · 0.80
existsMethod · 0.80
getDefaultSuffixMethod · 0.80
readMethod · 0.65
getMethod · 0.65
getNameMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected