AbstractPropertyMap.java
11.5 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFormatException
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.resource.JcrResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socialgraph.impl;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public abstract class AbstractPropertyMap
implements ValueMap {
private static final Logger log = LoggerFactory.getLogger(AbstractPropertyMap.class);
protected final String path;
protected abstract Node getNode(boolean var1);
protected AbstractPropertyMap(String path) {
this.path = path;
}
public void clear() {
throw new UnsupportedOperationException("Clearing a JCR backed value map is not supported, yet");
}
public Object put(String key, Object value) {
if (key.indexOf(47) != -1) {
throw new IllegalArgumentException("Invalid key: " + key);
}
if (value == null) {
throw new NullPointerException("Value should not be null (key = " + key + ")");
}
Node node = this.getNode(true);
Object oldValue = this.internalGet(node, key);
this.internalSet(node, key, value);
return oldValue;
}
public void putAll(Map<? extends String, ? extends Object> t) {
if (t != null) {
for (Map.Entry<? extends String, ? extends Object> entry : t.entrySet()) {
this.put(entry.getKey(), entry.getValue());
}
}
}
public Object remove(Object key) {
String sKey = key.toString();
if (sKey.indexOf(47) != -1) {
throw new IllegalArgumentException("Invalid key: " + sKey);
}
Node node = this.getNode(true);
Object oldValue = this.internalGet(node, sKey);
try {
if (node.hasProperty(sKey)) {
node.getProperty(sKey).remove();
}
}
catch (RepositoryException e) {
// empty catch block
}
return oldValue;
}
public void reset() {
throw new UnsupportedOperationException("Resetting a JCR backed value map is not supported, yet");
}
public <T> T get(String key, Class<T> type) {
if (type == null) {
return (T)this.get(key);
}
Node node = this.getNode(false);
return node == null ? null : (T)this.internalGet(node, key, type);
}
public <T> T get(String key, T defaultValue) {
if (defaultValue == null) {
return (T)this.get(key);
}
Class<?> type = this.normalizeClass(defaultValue.getClass());
Class value = this.get(key, (T)type);
if (value == null) {
value = defaultValue;
}
return (T)value;
}
public Object get(Object key) {
Node node = this.getNode(false);
return node == null ? null : this.internalGet(node, key.toString());
}
public boolean containsKey(Object key) {
Node node = this.getNode(false);
try {
return node != null && node.hasProperty(key.toString());
}
catch (RepositoryException e) {
return false;
}
}
public boolean containsValue(Object value) {
throw new UnsupportedOperationException("containsValue on a JCR backed value map is not supported, yet");
}
public boolean isEmpty() {
return this.getNode(false) == null;
}
public int size() {
Node node = this.getNode(false);
int cnt = 0;
if (node != null) {
try {
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
iter.next();
++cnt;
}
}
catch (RepositoryException e) {
// empty catch block
}
}
return cnt;
}
public Set<Map.Entry<String, Object>> entrySet() {
HashMap<String, Object> map = new HashMap<String, Object>();
Node node = this.getNode(false);
if (node != null) {
try {
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
Property p = iter.nextProperty();
map.put(p.getName(), JcrResourceUtil.toJavaObject((Property)p));
}
}
catch (RepositoryException e) {
// empty catch block
}
}
return Collections.unmodifiableSet(map.entrySet());
}
public Set<String> keySet() {
HashSet<String> set = new HashSet<String>();
Node node = this.getNode(false);
if (node != null) {
try {
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
Property p = iter.nextProperty();
set.add(p.getName());
}
}
catch (RepositoryException e) {
// empty catch block
}
}
return Collections.unmodifiableSet(set);
}
public Collection<Object> values() {
HashSet<Object> set = new HashSet<Object>();
Node node = this.getNode(false);
if (node != null) {
try {
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
Property p = iter.nextProperty();
set.add(JcrResourceUtil.toJavaObject((Property)p));
}
}
catch (RepositoryException e) {
// empty catch block
}
}
return Collections.unmodifiableSet(set);
}
private Object internalGet(Node node, String key) {
try {
if (node.hasProperty(key)) {
return JcrResourceUtil.toJavaObject((Property)node.getProperty(key));
}
}
catch (RepositoryException e) {
// empty catch block
}
return null;
}
private <T> T internalGet(Node node, String key, Class<T> type) {
try {
if (node.hasProperty(key)) {
return this.internalGet(node.getProperty(key), type);
}
}
catch (RepositoryException e) {
// empty catch block
}
return null;
}
private <T> T internalGet(Property p, Class<T> type) {
?[] result = null;
try {
boolean array = type.isArray();
if (p.isMultiple()) {
Value[] values = p.getValues();
if (array) {
result = this.convertToArray(p, values, type.getComponentType());
} else if (values.length > 0) {
result = this.convertToType(p, values[0], type);
}
} else {
Value value = p.getValue();
result = array ? this.convertToArray(p, new Value[]{value}, type.getComponentType()) : (?[])this.convertToType(p, value, type);
}
}
catch (ValueFormatException vfe) {
log.info("converToType: Cannot convert value of " + (Object)p + " to " + type, (Throwable)vfe);
}
catch (RepositoryException re) {
log.info("converToType: Cannot get value of " + (Object)p, (Throwable)re);
}
return (T)result;
}
private void internalSet(Node node, String key, Object value) {
try {
Session session = node.getSession();
if (value.getClass().isArray()) {
Object[] values = (Object[])value;
Value[] jcrValues = new Value[values.length];
for (int i = 0; i < values.length; ++i) {
jcrValues[i] = JcrResourceUtil.createValue((Object)values[i], (Session)session);
if (jcrValues[i] != null) continue;
throw new IllegalArgumentException("Value can't be stored in the repository: " + values[i]);
}
node.setProperty(key, jcrValues);
} else {
Value jcrValue = JcrResourceUtil.createValue((Object)value, (Session)session);
if (jcrValue == null) {
throw new IllegalArgumentException("Value can't be stored in the repository: " + value);
}
node.setProperty(key, jcrValue);
}
}
catch (RepositoryException e) {
throw new IllegalArgumentException("Unable to store value", (Throwable)e);
}
}
private <T> T[] convertToArray(Property p, Value[] jcrValues, Class<T> type) throws ValueFormatException, RepositoryException {
ArrayList<T> values = new ArrayList<T>();
for (Value jcrValue : jcrValues) {
T value = this.convertToType(p, jcrValue, type);
if (value == null) continue;
values.add(value);
}
Object[] result = (Object[])Array.newInstance(type, values.size());
return values.toArray(result);
}
private <T> T convertToType(Property p, Value jcrValue, Class<T> type) throws ValueFormatException, RepositoryException {
if (String.class == type) {
return (T)jcrValue.getString();
}
if (Byte.class == type) {
return (T)Byte.valueOf((byte)jcrValue.getLong());
}
if (Short.class == type) {
return (short)jcrValue.getLong();
}
if (Integer.class == type) {
return (int)jcrValue.getLong();
}
if (Long.class == type) {
return jcrValue.getLong();
}
if (Float.class == type) {
return (T)Float.valueOf((float)jcrValue.getDouble());
}
if (Double.class == type) {
return jcrValue.getDouble();
}
if (BigDecimal.class == type) {
return (T)jcrValue.getDecimal();
}
if (Boolean.class == type) {
return jcrValue.getBoolean();
}
if (Date.class == type) {
return (T)jcrValue.getDate().getTime();
}
if (Calendar.class == type) {
return (T)jcrValue.getDate();
}
if (Value.class == type) {
return (T)jcrValue;
}
if (Property.class == type) {
return (T)p;
}
return null;
}
private Class<?> normalizeClass(Class<?> type) {
if (Calendar.class.isAssignableFrom(type)) {
type = Calendar.class;
} else if (Date.class.isAssignableFrom(type)) {
type = Date.class;
} else if (Value.class.isAssignableFrom(type)) {
type = Value.class;
} else if (Property.class.isAssignableFrom(type)) {
type = Property.class;
}
return type;
}
}