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