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.raw;
35
36 import static java.util.stream.Collectors.toList;
37
38 import info.magnolia.config.registry.DefinitionRawView;
39
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44
45
46
47
48 public class DefinitionRawViewMapWrapper implements DefinitionRawView {
49
50 private final Map<Object, Object> map;
51
52 public DefinitionRawViewMapWrapper(Map map) {
53 this.map = map;
54 }
55
56 @Override
57 public List<Property> properties() {
58 return map.entrySet().stream()
59 .map(this::toValue)
60 .collect(toList());
61 }
62
63 public Map getRawMap() {
64 return Collections.unmodifiableMap(map);
65 }
66
67 private Property deriveValue(Object key, Object o) {
68
69 String name = key != null ? String.valueOf(key) : null;
70 if (o instanceof Map) {
71 return Property.subBean(name, new DefinitionRawViewMapWrapper((Map) o));
72 } else if (o instanceof Collection) {
73 Collection<Property> derivedCollection = ((Collection<?>) o).stream()
74 .map(this::toValue)
75 .collect(toList());
76 return Property.collection(name, derivedCollection);
77 } else if (o instanceof String || o instanceof Boolean || o instanceof Number) {
78 return Property.simple(name, o);
79 } else if (o == null) {
80 return Property.simple(name, "");
81 } else {
82 throw new IllegalStateException("Unsupported property type in map !? " + o + "(" + o.getClass() + ")");
83 }
84 }
85
86 private Property toValue(Object input) {
87 if (input instanceof Map.Entry) {
88 Map.Entry e = (Map.Entry) input;
89 return deriveValue(e.getKey(), e.getValue());
90 } else if (input instanceof Map) {
91
92 Map map = (Map) input;
93 return deriveValue(map.get("name"), map);
94 }
95 return deriveValue(null, input);
96 }
97 }