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.List;
43 import java.util.Map;
44 import java.util.Set;
45
46 import org.yaml.snakeyaml.constructor.Construct;
47 import org.yaml.snakeyaml.constructor.Constructor;
48 import org.yaml.snakeyaml.nodes.MappingNode;
49 import org.yaml.snakeyaml.nodes.Node;
50 import org.yaml.snakeyaml.nodes.NodeTuple;
51 import org.yaml.snakeyaml.nodes.ScalarNode;
52 import org.yaml.snakeyaml.nodes.SequenceNode;
53 import org.yaml.snakeyaml.nodes.Tag;
54
55
56
57
58
59
60
61 public class MgnlYamlConstructor extends Constructor {
62
63 private final YamlConfigurationDependencyAggregator dependencyAggregator;
64
65 MgnlYamlConstructor(YamlReader yamlReader, Resource baseResource, YamlConfigurationDependencyAggregator dependencyAggregator) {
66 this.dependencyAggregator = dependencyAggregator;
67 registerConstruct(IncludeYamlFile.TAG, new IncludeYamlFile(yamlReader, baseResource.getOrigin(), problem -> {}));
68 }
69
70 void registerConstruct(String tag, Construct construct) {
71 if (construct instanceof MgnlYamlConstruct) {
72 ((MgnlYamlConstruct) construct).setConstructor(this);
73 }
74 yamlConstructors.put(new Tag(tag), construct);
75 }
76
77 void registerMultiConstruct(String tagPrefix, Construct construct) {
78 if (construct instanceof MgnlYamlConstruct) {
79 ((MgnlYamlConstruct) construct).setConstructor(this);
80 }
81 yamlMultiConstructors.put(tagPrefix, construct);
82 }
83
84 public Construct getConstructByNodeType(Node node) {
85
86
87
88 if (node instanceof MappingNode) {
89 return new ConstructYamlMap();
90 } else if (node instanceof SequenceNode) {
91 return new ConstructYamlSeq();
92 }
93
94 return new ConstructScalar() {
95 @Override
96 public Object construct(Node n) {
97 return ((ScalarNode) n).getValue();
98 }
99 };
100 }
101
102 @Override
103 protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
104 List<NodeTuple> nodeValue = node.getValue();
105 for (NodeTuple tuple : nodeValue) {
106 if (Tag.INT.equals(tuple.getKeyNode().getTag())) {
107 tuple.getKeyNode().setTag(Tag.STR);
108 }
109 }
110 super.constructMapping2ndStep(node, mapping);
111 }
112
113 public YamlConfigurationDependencyAggregator getDependencyAggregator() {
114 return dependencyAggregator;
115 }
116
117
118
119
120 public static class YamlConfigurationDependencyAggregator {
121
122 private Set<YamlConfigurationDependency> dependencies = new HashSet<>();
123
124 YamlConfigurationDependencyAggregator() {}
125
126 public void addDependency(YamlConfigurationDependency dependency) {
127 this.dependencies.add(dependency);
128 }
129
130 public Set<YamlConfigurationDependency> getDependencies() {
131 return this.dependencies;
132 }
133
134 }
135 }