1 package io.guixer.lang;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.collect.Lists.newArrayList;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.List;
9
10 import com.avcompris.util.YamlUtils;
11 import com.avcompris.util.Yamled;
12
13 public abstract class GuixerScenarioLoader2 {
14
15 public static GuixerScenario load(
16 final File yamlFile
17 ) throws IOException, GuixerSyntaxException {
18
19 return AbstractGuixerScenarioLoader.load(yamlFile, new MyYamlStrategy2());
20 }
21
22 private static class MyYamlStrategy2 implements MyYamlStrategy {
23
24 @Override
25 public MyYaml loadYaml(
26 final File yamlFile
27 ) throws IOException {
28
29 final Yamled yaml = YamlUtils.loadYAML(yamlFile);
30
31 return new MyYaml2(yaml);
32 }
33 }
34
35 private static class MyYaml2 implements MyYaml {
36
37 private final Yamled yaml;
38
39 public MyYaml2(
40 final Yamled yaml
41 ) {
42
43 this.yaml = checkNotNull(yaml, "yaml");
44 }
45
46 @Override
47 public boolean isArray() {
48
49 return yaml.isList();
50 }
51
52 @Override
53 public boolean isMap() {
54
55 return yaml.isMap();
56 }
57
58 @Override
59 public String asString() {
60
61 return yaml.asString();
62 }
63
64 @Override
65 public boolean asBoolean() {
66
67 return yaml.asBoolean();
68 }
69
70 @Override
71 public boolean has(
72 final String name
73 ) {
74
75 return yaml.has(name);
76 }
77
78 @Override
79 public MyYaml get(
80 final String name
81 ) {
82
83 final Yamled sub = yaml.get(name);
84
85 return new MyYaml2(sub);
86 }
87
88 @Override
89 public Iterable<MyYaml> items() {
90
91 final List<MyYaml> items = newArrayList();
92
93 for (final Yamled sub : yaml.items()) {
94
95 items.add(new MyYaml2(sub));
96 }
97
98 return items;
99 }
100
101 @Override
102 public String uniqueKey() {
103
104 return yaml.label();
105 }
106
107 public Iterable<String> keysAsStrings() {
108
109 final List<String> keys = newArrayList();
110
111 for (final Yamled item : yaml.items(false)) {
112
113 final String key = item.label();
114
115 keys.add(key);
116 }
117
118 return keys;
119 }
120 }
121 }