1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.link;
18
19 import javax.jcr.Node;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.Session;
22
23 import org.exoplatform.container.ExoContainer;
24 import org.exoplatform.container.ExoContainerContext;
25 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
26
27
28
29
30
31
32
33 public final class LinkUtils {
34
35
36
37
38
39
40
41 public static String evaluatePath(String path) {
42 if (!path.startsWith("/")) {
43 throw new IllegalArgumentException("The path '" + path + "' must be an absolute path");
44 }
45 path = cleanPath(path);
46 int index;
47 while ((index = path.indexOf("/..")) != -1) {
48 if (index == 0) {
49 path = path.substring(3);
50 } else {
51 path = createPath(getParentPath(path.substring(0, index)), path.substring(index + 3));
52 if (!path.endsWith("/")) path = path.concat("/");
53 }
54 }
55
56 while ((index = path.indexOf("/./")) != -1) {
57 if (index == 0) {
58 path = path.substring(2);
59 } else {
60 path = createPath(path.substring(0, index), path.substring(index + 2));
61 }
62 }
63 path = cleanPath(path);
64 return path.length() == 0 ? "/" : path;
65 }
66
67
68
69
70
71
72 public static String getItemName(String path) {
73 if (!path.startsWith("/")) {
74 throw new IllegalArgumentException("The path '" + path + "' must be an absolute path");
75 }
76 path = cleanPath(path);
77 int index = path.lastIndexOf('/');
78 return path.substring(index + 1);
79 }
80
81
82
83
84 public static String createPath(String parentPath, String relativePath) {
85 if (!parentPath.startsWith("/")) {
86 throw new IllegalArgumentException("The path '" + parentPath + "' must be an absolute path");
87 }
88 parentPath = cleanPath(parentPath);
89 relativePath = cleanPath(relativePath);
90 if (relativePath.startsWith("/")) {
91 relativePath = relativePath.substring(1);
92 }
93 StringBuilder path = new StringBuilder(128);
94 path.append(parentPath);
95 if (relativePath.length() > 0) {
96 if (!parentPath.equals("/")) {
97 path.append('/');
98 }
99 path.append(relativePath);
100 }
101 return path.toString();
102 }
103
104
105
106
107
108
109 public static int getDepth(String path) {
110 if (!path.startsWith("/")) {
111 throw new IllegalArgumentException("The path '" + path + "' must be an absolute path");
112 }
113 path = cleanPath(path);
114 if (path.equals("/")) {
115 return 0;
116 }
117 return path.substring(1).split("/").length;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static String getAncestorPath(String path, int depth) {
134 if (!path.startsWith("/")) {
135 throw new IllegalArgumentException("The path '" + path + "' must be an absolute path");
136 }
137 path = cleanPath(path);
138 if (depth == 0) {
139 return "/";
140 }
141 String[] subpath = path.substring(1).split("/");
142 StringBuilder result = new StringBuilder(128);
143 for (int i = 0; i < depth; i++) {
144 result.append('/');
145 result.append(subpath[i]);
146 }
147 return result.toString();
148 }
149
150
151
152
153
154
155 public static String getParentPath(String path) {
156 if (!path.startsWith("/")) {
157 throw new IllegalArgumentException("The path '" + path + "' must be an absolute path");
158 }
159 path = cleanPath(path);
160 if (path.equals("/")) {
161 return "/";
162 }
163 int index = path.lastIndexOf('/');
164 if (index == 0) {
165 return "/";
166 }
167 return path.substring(0, index);
168 }
169
170 public static NodeFinder getNodeFinder() {
171 ExoContainer context = ExoContainerContext.getCurrentContainer();
172 return (NodeFinder) context.getComponentInstance(NodeFinder.class);
173 }
174
175 public static LinkManager getLinkManager() {
176 return WCMCoreUtils.getService(LinkManager.class);
177 }
178
179
180
181
182
183
184
185
186
187 public static String getExistPath(Node node, String path) throws RepositoryException {
188
189
190 int deep = getDepth(path);
191
192
193 if (deep == 0) {
194 path = LinkUtils.getAncestorPath(path, 0);
195 } else {
196 Session session = node.getSession();
197 for (int i = deep; i > 0; i-- ) {
198 if (session.itemExists(path))
199 break;
200 path = getParentPath(path);
201 }
202 }
203 return path;
204 }
205
206 private static String cleanPath(String path) {
207
208
209 path = path.replaceAll("/+", "/");
210
211 if ((!(path.contains("/./"))) && path.length() > 1 && path.charAt(path.length() - 1) == '/') {
212 path = path.substring(0, path.length() - 1);
213 }
214 return path;
215 }
216
217 }