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