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 package info.magnolia.cms.core;
35
36 import java.io.File;
37 import java.io.IOException;
38
39 import javax.jcr.RepositoryException;
40 import javax.jcr.Session;
41
42 import org.apache.commons.lang.StringUtils;
43 import org.safehaus.uuid.UUIDGenerator;
44
45
46
47
48
49
50
51 public final class Path {
52
53
54
55 private static final String DEFAULT_UNTITLED_NODE_NAME = "untitled";
56
57
58
59
60 private Path() {
61
62 }
63
64
65
66
67
68 public static String getCacheDirectoryPath() {
69 return getCacheDirectory().getAbsolutePath();
70 }
71
72 public static File getCacheDirectory() {
73 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_CACHE_STARTDIR);
74 File dir = isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
75 dir.mkdirs();
76 return dir;
77 }
78
79
80
81
82
83 public static String getTempDirectoryPath() {
84 return getTempDirectory().getAbsolutePath();
85 }
86
87 public static File getTempDirectory() {
88 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_UPLOAD_TMPDIR);
89 File dir = isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
90 dir.mkdirs();
91 return dir;
92 }
93
94
95
96
97
98 public static String getHistoryFilePath() {
99 return getHistoryFile().getAbsolutePath();
100 }
101
102 public static File getHistoryFile() {
103 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_EXCHANGE_HISTORY);
104 return isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
105 }
106
107
108
109
110
111 public static String getRepositoriesConfigFilePath() {
112 return getRepositoriesConfigFile().getAbsolutePath();
113 }
114
115 public static File getRepositoriesConfigFile() {
116 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_REPOSITORIES_CONFIG);
117 return isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
118 }
119
120
121
122
123
124 public static File getAppRootDir() {
125 return new File(SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR));
126 }
127
128
129 private void checkAppRootDir() throws IOException {
130 String root = null;
131
132 try {
133 File f = getAppRootDir();
134 if (f.isDirectory()) {
135 root = f.getAbsolutePath();
136 }
137 }
138 catch (Exception e) {
139
140 }
141
142 if (root == null) {
143 throw new IOException("Invalid magnolia " + SystemProperty.MAGNOLIA_APP_ROOTDIR + " path");
144 }
145 }
146
147
148
149
150 public static String getAbsoluteFileSystemPath(String path) {
151 if (isAbsolute(path)) {
152 return path;
153 }
154
155 return new File(getAppRootDir(), path).getAbsolutePath();
156 }
157
158 public static String getUniqueLabel(HierarchyManager hierarchyManager, String parent, String label) {
159 if (parent.equals("/")) {
160 parent = StringUtils.EMPTY;
161 }
162 while (hierarchyManager.isExist(parent + "/" + label)) {
163 label = createUniqueName(label);
164 }
165 return label;
166 }
167
168 public static String getUniqueLabel(Session session, String parent, String label) throws RepositoryException {
169 if (parent.equals("/")) {
170 parent = StringUtils.EMPTY;
171 }
172 while (session.itemExists(parent + "/" + label)) {
173 label = createUniqueName(label);
174 }
175 return label;
176 }
177
178 public static String getUniqueLabel(Content parent, String label) {
179 try {
180 while (parent.hasContent(label) || parent.hasNodeData(label)) {
181 label = createUniqueName(label);
182 }
183 }
184 catch (RepositoryException e) {
185 label = UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
186 }
187 return label;
188 }
189
190 public static boolean isAbsolute(String path) {
191
192 if (path == null) {
193 return false;
194 }
195
196 if (path.startsWith("/") || path.startsWith(File.separator)) {
197 return true;
198 }
199
200
201 if (path.length() >= 3 && Character.isLetter(path.charAt(0)) && path.charAt(1) == ':') {
202 return true;
203 }
204
205 return false;
206 }
207
208
209
210
211
212
213 public static String getValidatedLabel(String label)
214 {
215 String charset = StringUtils.EMPTY;
216 if ((SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED)))
217 {
218 charset = "UTF-8";
219 }
220 return getValidatedLabel(label, charset);
221
222 }
223
224
225
226
227
228
229
230
231
232
233 public static String getValidatedLabel(String label, String charset)
234 {
235 StringBuffer s = new StringBuffer(label);
236 StringBuffer newLabel = new StringBuffer();
237
238 for (int i = 0; i < s.length(); i++)
239 {
240 int charCode = s.charAt(i);
241 if (isCharValid(charCode, charset))
242 {
243 newLabel.append(s.charAt(i));
244 }
245 else
246 {
247 newLabel.append("-");
248 }
249 }
250 if (newLabel.length() == 0)
251 {
252 newLabel.append(DEFAULT_UNTITLED_NODE_NAME);
253 }
254 return newLabel.toString();
255 }
256
257
258
259
260
261
262 public static boolean isCharValid(int charCode, String charset)
263 {
264
265
266
267
268
269
270
271 if ("UTF-8".equals(charset))
272 {
273
274
275 if (charCode != 32
276 && charCode != '['
277 && charCode != ']'
278 && charCode != '*'
279 && charCode != '"'
280 && charCode != '.'
281 && charCode != ':'
282 && charCode != 92
283 && charCode != 39
284 && charCode != ';'
285 && charCode != '/'
286 && charCode != '?'
287 && charCode != '+'
288 && charCode != '%'
289 && charCode != '!'
290 && charCode != '#'
291 && charCode != '@'
292 && charCode != '&'
293 && charCode != '=')
294 {
295 return true;
296 }
297 }
298 else
299 {
300
301 if (((charCode >= 48) && (charCode <= 57))
302 || ((charCode >= 65) && (charCode <= 90))
303 || ((charCode >= 97) && (charCode <= 122))
304 || charCode == 45
305 || charCode == 95)
306 {
307 return true;
308 }
309
310 }
311 return false;
312
313 }
314
315 private static String createUniqueName(String baseName) {
316 int pos;
317 for (pos = baseName.length() - 1; pos >= 0; pos--) {
318 char c = baseName.charAt(pos);
319 if (c < '0' || c > '9') {
320 break;
321 }
322 }
323 String base;
324 int cnt;
325 if (pos == -1) {
326 if (baseName.length() > 1) {
327 pos = baseName.length() - 2;
328 }
329 }
330 if (pos == -1) {
331 base = baseName;
332 cnt = -1;
333 }
334 else {
335 pos++;
336 base = baseName.substring(0, pos);
337 if (pos == baseName.length()) {
338 cnt = -1;
339 }
340 else {
341 cnt = new Integer(baseName.substring(pos)).intValue();
342 }
343 }
344 return (base + ++cnt);
345 }
346
347 public static String getAbsolutePath(String path, String label) {
348 if (StringUtils.isEmpty(path) || (path.equals("/"))) {
349 return "/" + label;
350 }
351
352 return path + "/" + label;
353 }
354
355 public static String getAbsolutePath(String path) {
356 if (!path.startsWith("/")) {
357 return "/" + path;
358 }
359 return path;
360 }
361
362
363
364
365 @Deprecated
366 public static String getNodePath(String path, String label) {
367 if (StringUtils.isEmpty(path) || (path.equals("/"))) {
368 return label;
369 }
370 return getNodePath(path + "/" + label);
371 }
372
373
374
375
376 @Deprecated
377 public static String getNodePath(String path) {
378 if (path.startsWith("/")) {
379 return path.replaceFirst("/", StringUtils.EMPTY);
380 }
381 return path;
382 }
383
384
385
386
387 @Deprecated
388 public static String getParentPath(String path) {
389 int lastIndexOfSlash = path.lastIndexOf("/");
390 if (lastIndexOfSlash > 0) {
391 return StringUtils.substringBefore(path, "/");
392 }
393 return "/";
394 }
395 }