1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27
28 import org.apache.commons.configuration2.ex.ConfigurationException;
29 import org.apache.commons.configuration2.io.ConfigurationLogger;
30 import org.apache.commons.configuration2.tree.ImmutableNode;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class AbstractYAMLBasedConfiguration extends BaseHierarchicalConfiguration {
45
46
47
48 protected AbstractYAMLBasedConfiguration() {
49 initLogger(new ConfigurationLogger(getClass()));
50 }
51
52
53
54
55
56
57 protected AbstractYAMLBasedConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
58 super(c);
59 initLogger(new ConfigurationLogger(getClass()));
60 }
61
62
63
64
65
66
67
68 protected void load(final Map<String, Object> map) {
69 final List<ImmutableNode> roots = constructHierarchy("", map);
70 getNodeModel().setRootNode(roots.get(0));
71 }
72
73
74
75
76
77
78
79 protected Map<String, Object> constructMap(final ImmutableNode node) {
80 final Map<String, Object> map = new HashMap<>(node.getChildren().size());
81 node.forEach(cNode -> addEntry(map, cNode.getNodeName(), cNode.getChildren().isEmpty() ? cNode.getValue() : constructMap(cNode)));
82 return map;
83 }
84
85
86
87
88
89
90
91
92
93 private static void addEntry(final Map<String, Object> map, final String key, final Object value) {
94 final Object oldValue = map.get(key);
95 if (oldValue == null) {
96 map.put(key, value);
97 } else if (oldValue instanceof Collection) {
98
99 @SuppressWarnings("unchecked")
100 final Collection<Object> values = (Collection<Object>) oldValue;
101 values.add(value);
102 } else {
103 final Collection<Object> values = new ArrayList<>();
104 values.add(oldValue);
105 values.add(value);
106 map.put(key, values);
107 }
108 }
109
110
111
112
113
114
115
116
117
118 private static List<ImmutableNode> constructHierarchy(final String key, final Object elem) {
119 if (elem instanceof Map) {
120 return parseMap((Map<String, Object>) elem, key);
121 }
122 if (elem instanceof Collection) {
123 return parseCollection((Collection<Object>) elem, key);
124 }
125 return Collections.singletonList(new ImmutableNode.Builder().name(key).value(elem).create());
126 }
127
128
129
130
131
132
133
134
135 private static List<ImmutableNode> parseMap(final Map<String, Object> map, final String key) {
136 final ImmutableNode.Builder subtree = new ImmutableNode.Builder().name(key);
137 map.forEach((k, v) -> constructHierarchy(k, v).forEach(subtree::addChild));
138 return Collections.singletonList(subtree.create());
139 }
140
141
142
143
144
145
146
147
148 private static List<ImmutableNode> parseCollection(final Collection<Object> col, final String key) {
149 return col.stream().flatMap(elem -> constructHierarchy(key, elem).stream()).collect(Collectors.toList());
150 }
151
152
153
154
155
156
157
158 static void rethrowException(final Exception e) throws ConfigurationException {
159 if (e instanceof ClassCastException) {
160 throw new ConfigurationException("Error parsing", e);
161 }
162 throw new ConfigurationException("Unable to load the configuration", e);
163 }
164 }