]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/LoadBytesTemplate.java
1bfbe8f7413f5b981fc56a46b683482d7ceaa6aa
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / templates / LoadBytesTemplate.java
1 package org.simantics.graph.compiler.internal.templates;
2
3 import java.io.IOException;
4 import java.util.Collection;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.binding.mutable.Variant;
8 import org.simantics.graph.compiler.ExternalFileLoader;
9 import org.simantics.graph.compiler.internal.store.LocationStore;
10 import org.simantics.graph.compiler.internal.store.PreValueStore;
11 import org.simantics.graph.query.IGraph;
12 import org.simantics.graph.store.GraphStore;
13 import org.simantics.ltk.Location;
14 import org.simantics.ltk.Problem;
15
16 public enum LoadBytesTemplate implements ITemplate {
17         INSTANCE;
18         
19         @Override
20         public void apply(IGraph graph, GraphStore store,
21                         int[] parameters, ExternalFileLoader fileLoader, Collection<Problem> problems) {
22                 String fileName = store.getStore(PreValueStore.class).getStringValue(parameters[1]);
23                 if(fileName == null) {
24                         Location location = store.getStore(LocationStore.class)
25                         .getLocation(parameters[1]);
26                         problems.add(new Problem(
27                                 location, "Expected a string"));
28                 }               
29                 try {
30                         byte[] data = fileLoader.load(fileName);
31                         store.values.setValue(parameters[0], 
32                                         new Variant(Bindings.BYTE_ARRAY, data)
33                                         );
34                 } catch (IOException e) {
35                         Location location = store.getStore(LocationStore.class)
36                                 .getLocation(parameters[1]);
37                         problems.add(new Problem(
38                                         location, e.getMessage()));
39                 }               
40         }
41
42 }