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