]> gerrit.simantics Code Review - simantics/platform.git/blob
94afd0b95397c2d355b773329c51f273f0a4f209
[simantics/platform.git] /
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.databoard.serialization.Serializer;
9 import org.simantics.graph.compiler.ExternalFileLoader;
10 import org.simantics.graph.compiler.internal.store.LocationStore;
11 import org.simantics.graph.compiler.internal.store.PreValueStore;
12 import org.simantics.graph.query.IGraph;
13 import org.simantics.graph.store.GraphStore;
14 import org.simantics.ltk.Location;
15 import org.simantics.ltk.Problem;
16
17 public enum LoadDataValueTemplate implements ITemplate {
18         INSTANCE;
19         
20         @Override
21         public void apply(IGraph graph, GraphStore store,
22                         int[] parameters, ExternalFileLoader fileLoader, Collection<Problem> problems) {
23                 String fileName = store.getStore(PreValueStore.class).getStringValue(parameters[1]);
24                 if(fileName == null) {
25                         Location location = store.getStore(LocationStore.class)
26                         .getLocation(parameters[1]);
27                         problems.add(new Problem(
28                                 location, "Expected a string"));
29                 }               
30                 try {
31                     byte[] bytes = fileLoader.load(fileName);
32                     Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
33                     Variant value = (Variant)variantSerializer.deserialize(bytes);
34                         store.values.setValue(parameters[0], value);
35                 } catch (IOException e) {
36                         Location location = store.getStore(LocationStore.class)
37                                 .getLocation(parameters[1]);
38                         problems.add(new Problem(
39                                 location, e.getMessage()));
40                 }               
41         }
42
43 }