HierarchyNodeInheritanceValueMap.java
3.6 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.day.cq.commons.inherit;
import com.day.cq.commons.ValueMapWrapper;
import com.day.cq.commons.inherit.InheritanceValueMap;
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 HierarchyNodeInheritanceValueMap
extends ValueMapWrapper
implements InheritanceValueMap {
private Resource resource;
public HierarchyNodeInheritanceValueMap(Resource resource) {
super(ResourceUtil.getValueMap((Resource)resource));
this.resource = resource;
}
public HierarchyNodeInheritanceValueMap(ValueMap map) {
super(map);
this.resource = null;
}
@Override
public <T> T get(String name, Class<T> type) {
if (type == null) {
return (T)this.get(name);
}
return (T)super.get(name, type);
}
@Override
public <T> T getInherited(String name, Class<T> type) {
T value = this.get(name, type);
if (value == null) {
value = this.getParentPageValue(HierarchyNodeInheritanceValueMap.getInnerPath(this.resource), name, type);
}
return value;
}
@Override
public <T> T getInherited(String name, T defaultValue) {
Class type = defaultValue == null ? null : defaultValue.getClass();
Class value = this.getInherited(name, (T)type);
if (value == null) {
value = defaultValue;
}
return (T)value;
}
protected <T> T getParentPageValue(String innerPath, String name, Class<T> type) {
ResourceResolver resolver;
Resource content;
Resource innerResource;
Object value;
if (this.resource == null) {
return null;
}
Resource parent = ResourceUtil.getParent((Resource)this.resource);
boolean isContentNode = ResourceUtil.getName((Resource)this.resource).equals("jcr:content");
if (parent == null) {
parent = HierarchyNodeInheritanceValueMap.getNextExistingParent(this.resource);
if (parent == null) {
return null;
}
isContentNode = false;
}
if (!isContentNode && (content = (resolver = parent.getResourceResolver()).getResource(parent, "jcr:content")) != null && (value = ResourceUtil.getValueMap((Resource)(innerResource = resolver.getResource(content, innerPath))).get(name, type)) != null) {
return (T)value;
}
return new HierarchyNodeInheritanceValueMap(parent).getParentPageValue(innerPath, name, type);
}
protected 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);
}
protected 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;
}
}