1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.config.source.yaml;
35
36 import info.magnolia.config.source.yaml.construct.IncludeYamlFile;
37 import info.magnolia.config.source.yaml.construct.MgnlYamlConstruct;
38 import info.magnolia.config.source.yaml.dependency.YamlConfigurationDependency;
39 import info.magnolia.resourceloader.Resource;
40
41 import java.util.HashSet;
42 import java.util.Set;
43
44 import org.yaml.snakeyaml.constructor.Construct;
45 import org.yaml.snakeyaml.constructor.Constructor;
46 import org.yaml.snakeyaml.nodes.MappingNode;
47 import org.yaml.snakeyaml.nodes.Node;
48 import org.yaml.snakeyaml.nodes.ScalarNode;
49 import org.yaml.snakeyaml.nodes.SequenceNode;
50 import org.yaml.snakeyaml.nodes.Tag;
51
52
53
54
55
56
57
58 public class MgnlYamlConstructor extends Constructor {
59
60 private final YamlConfigurationDependencyAggregator dependencyAggregator;
61
62 MgnlYamlConstructor(YamlReader yamlReader, Resource baseResource, YamlConfigurationDependencyAggregator dependencyAggregator) {
63 this.dependencyAggregator = dependencyAggregator;
64 registerConstruct(IncludeYamlFile.TAG, new IncludeYamlFile(yamlReader, baseResource.getOrigin(), problem -> {}));
65 }
66
67 void registerConstruct(String tag, Construct construct) {
68 if (construct instanceof MgnlYamlConstruct) {
69 ((MgnlYamlConstruct) construct).setConstructor(this);
70 }
71 yamlConstructors.put(new Tag(tag), construct);
72 }
73
74 void registerMultiConstruct(String tagPrefix, Construct construct) {
75 if (construct instanceof MgnlYamlConstruct) {
76 ((MgnlYamlConstruct) construct).setConstructor(this);
77 }
78 yamlMultiConstructors.put(tagPrefix, construct);
79 }
80
81 public Construct getConstructByNodeType(Node node) {
82
83
84
85 if (node instanceof MappingNode) {
86 return new ConstructYamlMap();
87 } else if (node instanceof SequenceNode) {
88 return new ConstructYamlSeq();
89 }
90
91 return new ConstructScalar() {
92 @Override
93 public Object construct(Node n) {
94 return ((ScalarNode) n).getValue();
95 }
96 };
97 }
98
99 public YamlConfigurationDependencyAggregator getDependencyAggregator() {
100 return dependencyAggregator;
101 }
102
103
104
105
106 public static class YamlConfigurationDependencyAggregator {
107
108 private Set<YamlConfigurationDependency> dependencies = new HashSet<>();
109
110 YamlConfigurationDependencyAggregator() {}
111
112 public void addDependency(YamlConfigurationDependency dependency) {
113 this.dependencies.add(dependency);
114 }
115
116 public Set<YamlConfigurationDependency> getDependencies() {
117 return this.dependencies;
118 }
119
120 }
121 }