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