DeepModifiableValueMapDecorator.java
4.58 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.ArrayUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ModifiableValueMapDecorator
* org.apache.sling.api.wrappers.ValueMapDecorator
*/
package com.adobe.granite.rest.utils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.ArrayUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ModifiableValueMapDecorator;
import org.apache.sling.api.wrappers.ValueMapDecorator;
public class DeepModifiableValueMapDecorator
extends ModifiableValueMapDecorator {
protected final String pathPrefix;
protected final ResourceResolver resolver;
protected final Map<String, Object> base;
protected final String[] subnodes;
public DeepModifiableValueMapDecorator(Resource resource, Map<String, Object> base, String[] subnodes) {
super(base);
this.pathPrefix = resource.getPath() + "/";
this.resolver = resource.getResourceResolver();
this.base = base;
this.subnodes = subnodes;
}
public DeepModifiableValueMapDecorator(Resource resource, Map<String, Object> base) {
this(resource, base, new String[0]);
}
protected ValueMap getValueMap(String name) {
ValueMap vm;
int pos = name.lastIndexOf("/");
if (pos == -1) {
return new ModifiableValueMapDecorator(this.base);
}
Resource rsrc = this.resolver.getResource(this.pathPrefix + name.substring(0, pos));
if (rsrc != null && (vm = (ValueMap)rsrc.adaptTo(ModifiableValueMap.class)) != null) {
return new ModifiableValueMapDecorator((Map)vm);
}
return ModifiableValueMap.EMPTY;
}
protected String getPropertyName(String name) {
int pos = name.lastIndexOf("/");
if (pos == -1) {
return name;
}
return name.substring(pos + 1);
}
public <T> T get(String name, Class<T> type) {
return (T)this.getValueMap(name).get(this.getPropertyName(name), type);
}
public <T> T get(String name, T defaultValue) {
return (T)this.getValueMap(name).get(this.getPropertyName(name), defaultValue);
}
public boolean containsKey(Object key) {
if (key == null) {
return false;
}
String name = key.toString();
return this.getValueMap(name).containsKey((Object)this.getPropertyName(name));
}
public Object get(Object key) {
if (key == null) {
return null;
}
String name = key.toString();
if (ArrayUtils.contains((Object[])this.subnodes, (Object)name)) {
return this.getValueMap(name + "/");
}
return this.getValueMap(name).get((Object)this.getPropertyName(name));
}
public Object put(String key, Object value) {
if (key == null) {
return null;
}
String name = key.toString();
if (name.indexOf("/") > -1) {
String subnode = name.substring(0, name.indexOf("/"));
if (ArrayUtils.contains((Object[])this.subnodes, (Object)subnode)) {
return this.getValueMap(name).put((Object)this.getPropertyName(name), value);
}
return null;
}
return this.getValueMap(name).put((Object)this.getPropertyName(name), value);
}
public void putAll(Map<? extends String, ?> t) {
for (String key : t.keySet()) {
this.put(key, t.get(key));
}
}
public Set<String> keySet() {
return this.buildAggregatedMap().keySet();
}
public Collection<Object> values() {
return this.buildAggregatedMap().values();
}
public Set<Map.Entry<String, Object>> entrySet() {
return this.buildAggregatedMap().entrySet();
}
private ValueMap buildAggregatedMap() {
ValueMapDecorator entries = new ValueMapDecorator(new HashMap());
for (Map.Entry entry : this.getValueMap("").entrySet()) {
entries.put(entry.getKey(), entry.getValue());
}
for (String subnode : this.subnodes) {
entries.put((Object)subnode, (Object)this.getValueMap(subnode + "/"));
}
return entries;
}
}