]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/LoadBytesTemplate.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / templates / LoadBytesTemplate.java
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/LoadBytesTemplate.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/LoadBytesTemplate.java
new file mode 100644 (file)
index 0000000..a9e3dd6
--- /dev/null
@@ -0,0 +1,42 @@
+package org.simantics.graph.compiler.internal.templates;\r
+\r
+import java.io.IOException;\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.graph.compiler.ExternalFileLoader;\r
+import org.simantics.graph.compiler.internal.store.LocationStore;\r
+import org.simantics.graph.compiler.internal.store.PreValueStore;\r
+import org.simantics.graph.query.IGraph;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.ltk.Location;\r
+import org.simantics.ltk.Problem;\r
+\r
+public enum LoadBytesTemplate implements ITemplate {\r
+       INSTANCE;\r
+       \r
+       @Override\r
+       public void apply(IGraph graph, GraphStore store,\r
+                       int[] parameters, ExternalFileLoader fileLoader, Collection<Problem> problems) {\r
+               String fileName = store.getStore(PreValueStore.class).getStringValue(parameters[1]);\r
+               if(fileName == null) {\r
+                       Location location = store.getStore(LocationStore.class)\r
+                       .getLocation(parameters[1]);\r
+                       problems.add(new Problem(\r
+                               location, "Expected a string"));\r
+               }               \r
+               try {\r
+                       byte[] data = fileLoader.load(fileName);\r
+                       store.values.setValue(parameters[0], \r
+                                       new Variant(Bindings.BYTE_ARRAY, data)\r
+                                       );\r
+               } catch (IOException e) {\r
+                       Location location = store.getStore(LocationStore.class)\r
+                               .getLocation(parameters[1]);\r
+                       problems.add(new Problem(\r
+                                       location, e.getMessage()));\r
+               }               \r
+       }\r
+\r
+}\r