WCMInheritanceValueMap.java
3.68 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.ValueMapWrapper
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.sightly.internal;
import com.day.cq.commons.ValueMapWrapper;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
public class WCMInheritanceValueMap
extends ValueMapWrapper {
private Resource resource;
public WCMInheritanceValueMap(Resource resource) {
super(ResourceUtil.getValueMap((Resource)resource));
this.resource = resource;
}
public Object get(Object key) {
Object o = super.get(key);
if (o == null) {
o = this.getInherited(key.toString(), null);
}
return o;
}
public <T> T get(String name, Class<T> type) {
Object inherited;
Object o = super.get(name, type);
if (o == null && (inherited = this.getInherited(name, type)) != null) {
return (T)inherited;
}
return (T)o;
}
public <T> T get(String name, T defaultValue) {
if (defaultValue == null) {
return (T)this.get(name);
}
Class type = defaultValue.getClass();
Class value = this.get(name, (T)type);
if (value == null) {
return defaultValue;
}
return (T)value;
}
private <T> Object getInherited(String key, Class<T> type) {
return this.getParentPageValue(WCMInheritanceValueMap.getInnerPath(this.resource), key, type);
}
private <T> Object getParentPageValue(String innerPath, String key, Class<T> type) {
ResourceResolver resolver;
Resource content;
if (this.resource == null) {
return null;
}
Resource parent = this.resource.getParent();
boolean isContentNode = this.resource.getName().equals("jcr:content");
if (parent == null) {
parent = WCMInheritanceValueMap.getNextExistingParent(this.resource);
if (parent == null) {
return null;
}
isContentNode = false;
}
if (!isContentNode && (content = (resolver = parent.getResourceResolver()).getResource(parent, "jcr:content")) != null) {
Resource innerResource = resolver.getResource(content, innerPath);
ValueMap innerResourceProperties = (ValueMap)innerResource.adaptTo(ValueMap.class);
Object value = type != null ? innerResourceProperties.get(key, type) : innerResourceProperties.get((Object)key);
if (value != null) {
return value;
}
}
return new WCMInheritanceValueMap(parent).getParentPageValue(innerPath, key, type);
}
private static String getInnerPath(Resource resource) {
if (resource == null) {
return ".";
}
String resPath = resource.getPath();
int pos = resPath.indexOf("jcr:content/");
if (pos <= 0) {
return ".";
}
return resPath.substring(pos + "jcr:content".length() + 1);
}
private static Resource getNextExistingParent(Resource resource) {
ResourceResolver resolver = resource.getResourceResolver();
String path = resource.getPath();
do {
if ((path = ResourceUtil.getParent((String)path)) != null) continue;
return null;
} while ((resource = resolver.getResource(path)) == null);
return resource;
}
}