PathInfo.java
4.46 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ResourceResolver
*/
package com.day.cq.commons;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
public class PathInfo
implements RequestPathInfo {
private final String path;
private final String selectorString;
private final String[] selectors;
private final String extension;
private final String suffix;
private Resource suffixResource;
public PathInfo(String urlPath) {
if (urlPath == null || !urlPath.startsWith("/")) {
throw new IllegalArgumentException("Not an absolute path: " + urlPath);
}
int lastDot = urlPath.lastIndexOf(".");
if (lastDot >= 0) {
String selectorStr;
String extPlusSuffix = urlPath.substring(lastDot + 1);
urlPath = urlPath.substring(0, lastDot);
int firstSlash = extPlusSuffix.indexOf("/");
if (firstSlash >= 0) {
this.extension = extPlusSuffix.substring(0, firstSlash);
this.suffix = extPlusSuffix.substring(firstSlash);
} else {
this.extension = extPlusSuffix;
this.suffix = null;
}
int lastSlash = urlPath.lastIndexOf("/");
if (lastSlash >= 0) {
selectorStr = urlPath.substring(lastSlash + 1);
urlPath = urlPath.substring(0, lastSlash + 1);
} else {
selectorStr = urlPath;
}
int firstDot = selectorStr.indexOf(".");
if (firstDot >= 0) {
urlPath = urlPath + selectorStr.substring(0, firstDot);
selectorStr = selectorStr.substring(firstDot + 1);
} else {
urlPath = urlPath + selectorStr;
selectorStr = null;
}
this.path = urlPath;
if (selectorStr == null) {
this.selectorString = null;
this.selectors = new String[0];
} else {
this.selectorString = selectorStr;
this.selectors = selectorStr.split("\\.");
}
} else {
this.path = urlPath;
this.selectorString = null;
this.selectors = new String[0];
this.extension = null;
this.suffix = null;
}
}
public PathInfo(ResourceResolver resolver, String urlPath) {
Resource resource = resolver.resolve(urlPath);
if (null != resource) {
String info = resource.getResourceMetadata().getResolutionPathInfo();
int slashIndex = StringUtils.indexOf((String)info, (String)"/");
this.suffix = slashIndex >= 0 ? StringUtils.substring((String)info, (int)(slashIndex + 1)) : null;
info = StringUtils.chomp((String)info, (String)("/" + this.suffix));
int extIndex = StringUtils.lastIndexOf((String)info, (String)".");
this.extension = extIndex >= 0 ? StringUtils.substring((String)info, (int)(extIndex + 1)) : null;
this.selectorString = info = StringUtils.chomp((String)info, (String)("." + this.extension));
this.selectors = StringUtils.split((String)info, (String)".");
this.path = resolver.map(resource.getPath());
if (this.suffix != null) {
this.suffixResource = resolver.getResource(this.suffix);
}
} else {
this.path = urlPath;
this.selectorString = null;
this.selectors = new String[0];
this.extension = null;
this.suffix = null;
this.suffixResource = null;
}
}
public String getResourcePath() {
return this.path;
}
public String getSelectorString() {
return this.selectorString;
}
public String[] getSelectors() {
return this.selectors;
}
public String getExtension() {
return this.extension;
}
public String getSuffix() {
return this.suffix;
}
public Resource getSuffixResource() {
return this.suffixResource;
}
}