KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > Path


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.core;
14
15 import java.io.File JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.net.URLDecoder JavaDoc;
18
19 import javax.jcr.RepositoryException;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.safehaus.uuid.UUIDGenerator;
24
25
26 /**
27  * @author Sameer Charles
28  * @version 2.0 $Id: Path.java 7440 2006-11-14 13:52:39Z philipp $
29  */

30 public final class Path {
31
32     public static final String JavaDoc JAVAX_FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path"; //$NON-NLS-1$
33

34     public static final String JavaDoc MGNL_REQUEST_URI_DECODED = "mgnl.request.uri.decoded"; //$NON-NLS-1$
35

36     /**
37      *
38      */

39     private static final String JavaDoc DEFAULT_UNTITLED_NODE_NAME = "untitled";
40
41     private static final String JavaDoc ENCODING_DEFAULT = "UTF-8"; //$NON-NLS-1$
42

43     /**
44      * Utility class, don't instantiate.
45      */

46     private Path() {
47         // unused
48
}
49
50     /**
51      * Gets the cache directory path (cms.cache.startdir) as set with Java options while startup or in web.xml.
52      * @return Cache directory path
53      */

54     public static String JavaDoc getCacheDirectoryPath() {
55         return getCacheDirectory().getAbsolutePath();
56     }
57
58     public static File JavaDoc getCacheDirectory() {
59         String JavaDoc path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_CACHE_STARTDIR);
60         File JavaDoc dir = isAbsolute(path) ? new File JavaDoc(path) : new File JavaDoc(Path.getAppRootDir(), path);
61         dir.mkdirs();
62         return dir;
63     }
64
65     /**
66      * Gets the temporary directory path (cms.upload.tmpdir) as set with Java options while startup or in web.xml.
67      * @return Temporary directory path
68      */

69     public static String JavaDoc getTempDirectoryPath() {
70         return getTempDirectory().getAbsolutePath();
71     }
72
73     public static File JavaDoc getTempDirectory() {
74         String JavaDoc path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_UPLOAD_TMPDIR);
75         File JavaDoc dir = isAbsolute(path) ? new File JavaDoc(path) : new File JavaDoc(Path.getAppRootDir(), path);
76         dir.mkdirs();
77         return dir;
78     }
79
80     /**
81      * Gets cms.exchange.history file location as set with Java options while startup or in web.xml.
82      * @return exchange history file location
83      */

84     public static String JavaDoc getHistoryFilePath() {
85         return getHistoryFile().getAbsolutePath();
86     }
87
88     public static File JavaDoc getHistoryFile() {
89         String JavaDoc path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_EXCHANGE_HISTORY);
90         return isAbsolute(path) ? new File JavaDoc(path) : new File JavaDoc(Path.getAppRootDir(), path);
91     }
92
93     /**
94      * Gets repositories file location as set with Java options while startup or in web.xml.
95      * @return file location
96      */

97     public static String JavaDoc getRepositoriesConfigFilePath() {
98         return getRepositoriesConfigFile().getAbsolutePath();
99     }
100
101     public static File JavaDoc getRepositoriesConfigFile() {
102         String JavaDoc path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_REPOSITORIES_CONFIG);
103         return isAbsolute(path) ? new File JavaDoc(path) : new File JavaDoc(Path.getAppRootDir(), path);
104     }
105
106     /**
107      * Gets the root directory for the magnolia web application.
108      * @return magnolia root dir
109      */

110     public static File JavaDoc getAppRootDir() {
111         return new File JavaDoc(SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR));
112     }
113
114     /**
115      * Gets absolute filesystem path, adds application root if path is not absolute
116      */

117     public static String JavaDoc getAbsoluteFileSystemPath(String JavaDoc path) {
118         if (Path.isAbsolute(path)) {
119             return path;
120         }
121         // using the file() constructor will allow relative paths in the form ../../apps
122
return new File JavaDoc(Path.getAppRootDir(), path).getAbsolutePath();
123     }
124
125     /**
126      * Returns the URI of the current request, without the context path.
127      * @param req request
128      * @return request URI without servlet context
129      */

130     public static String JavaDoc getURI(HttpServletRequest JavaDoc req) {
131
132         String JavaDoc uri = (String JavaDoc) req.getAttribute(MGNL_REQUEST_URI_DECODED);
133         if (uri == null) {
134             uri = getDecodedURI(req);
135             // cache decoded uri
136
req.setAttribute(MGNL_REQUEST_URI_DECODED, uri);
137         }
138
139         return uri;
140     }
141
142     public static void setURI(String JavaDoc uri, HttpServletRequest JavaDoc req) {
143         req.setAttribute(MGNL_REQUEST_URI_DECODED, uri);
144     }
145
146     /**
147      * Returns the URI of the current request, but uses the uri to repository mapping to remove any prefix.
148      * @param req request
149      * @return request URI without servlet context and without repository mapping prefix
150      */

151     public static String JavaDoc getHandle(HttpServletRequest JavaDoc req) {
152         return (String JavaDoc) req.getAttribute(Aggregator.HANDLE);
153     }
154
155     /**
156      * If you forward a request, this will return the original requests uri.
157      * @param req
158      */

159     public static String JavaDoc getOriginalURI(HttpServletRequest JavaDoc req) {
160         return (String JavaDoc) req.getAttribute(JAVAX_FORWARD_SERVLET_PATH);
161     }
162
163     /**
164      * Returns the decoded URI of the current request, without the context path.
165      * @param req request
166      * @return request URI without servlet context
167      */

168     private static String JavaDoc getDecodedURI(HttpServletRequest JavaDoc req) {
169         String JavaDoc encoding = StringUtils.defaultString(req.getCharacterEncoding(), ENCODING_DEFAULT);
170         String JavaDoc decodedURL = null;
171         try {
172             decodedURL = URLDecoder.decode(req.getRequestURI(), encoding);
173         }
174         catch (UnsupportedEncodingException JavaDoc e) {
175             decodedURL = req.getRequestURI();
176         }
177         return StringUtils.substringAfter(decodedURL, req.getContextPath());
178     }
179
180     public static String JavaDoc getExtension(HttpServletRequest JavaDoc req) {
181         String JavaDoc ext = (String JavaDoc) req.getAttribute(Aggregator.EXTENSION);
182         if (ext == null) {
183             ext = StringUtils.substringAfterLast(req.getRequestURI(), ".");
184             req.setAttribute(Aggregator.EXTENSION, ext);
185         }
186         return ext;
187     }
188
189     public static String JavaDoc getUniqueLabel(HierarchyManager hierarchyManager, String JavaDoc parent, String JavaDoc label) {
190         if (parent.equals("/")) { //$NON-NLS-1$
191
parent = StringUtils.EMPTY;
192         }
193         while (hierarchyManager.isExist(parent + "/" + label)) { //$NON-NLS-1$
194
label = createUniqueName(label);
195         }
196         return label;
197     }
198
199     public static String JavaDoc getUniqueLabel(Content parent, String JavaDoc label) {
200         try {
201             while (parent.hasContent(label) || parent.hasNodeData(label)) { //$NON-NLS-1$
202
label = createUniqueName(label);
203             }
204         }
205         catch (RepositoryException e) {
206             label = UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
207         }
208         return label;
209     }
210
211     protected static boolean isAbsolute(String JavaDoc path) {
212
213         if (path == null) {
214             return false;
215         }
216
217         if (path.startsWith("/") || path.startsWith(File.separator)) { //$NON-NLS-1$
218
return true;
219         }
220
221         // windows c:
222
if (path.length() >= 3 && Character.isLetter(path.charAt(0)) && path.charAt(1) == ':') {
223             return true;
224         }
225
226         return false;
227     }
228
229     /**
230      * <p>
231      * Replace illegal characters by [_] [0-9], [A-Z], [a-z], [-], [_]
232      * </p>
233      * @param label label to validate
234      * @return validated label
235      */

236     public static String JavaDoc getValidatedLabel(String JavaDoc label) {
237         StringBuffer JavaDoc s = new StringBuffer JavaDoc(label);
238         StringBuffer JavaDoc newLabel = new StringBuffer JavaDoc();
239         for (int i = 0; i < s.length(); i++) {
240             int charCode = s.charAt(i);
241             // charCodes: 48-57: [0-9]; 65-90: [A-Z]; 97-122: [a-z]; 45: [-]; 95:[_]
242
if (((charCode >= 48) && (charCode <= 57))
243                 || ((charCode >= 65) && (charCode <= 90))
244                 || ((charCode >= 97) && (charCode <= 122))
245                 || charCode == 45
246                 || charCode == 95) {
247                 newLabel.append(s.charAt(i));
248             }
249             else {
250                 newLabel.append("-"); //$NON-NLS-1$
251
}
252         }
253         if (newLabel.length() == 0) {
254             newLabel.append(DEFAULT_UNTITLED_NODE_NAME);
255         }
256         return newLabel.toString();
257     }
258
259     /**
260      * @param baseName
261      */

262     private static String JavaDoc createUniqueName(String JavaDoc baseName) {
263         int pos;
264         for (pos = baseName.length() - 1; pos >= 0; pos--) {
265             char c = baseName.charAt(pos);
266             if (c < '0' || c > '9') {
267                 break;
268             }
269         }
270         String JavaDoc base;
271         int cnt;
272         if (pos == -1) {
273             if (baseName.length() > 1) {
274                 pos = baseName.length() - 2;
275             }
276         }
277         if (pos == -1) {
278             base = baseName;
279             cnt = -1;
280         }
281         else {
282             pos++;
283             base = baseName.substring(0, pos);
284             if (pos == baseName.length()) {
285                 cnt = -1;
286             }
287             else {
288                 cnt = new Integer JavaDoc(baseName.substring(pos)).intValue();
289             }
290         }
291         return (base + ++cnt);
292     }
293
294     public static String JavaDoc getAbsolutePath(String JavaDoc path, String JavaDoc label) {
295         if (StringUtils.isEmpty(path) || (path.equals("/"))) { //$NON-NLS-1$
296
return "/" + label; //$NON-NLS-1$
297
}
298
299         return path + "/" + label; //$NON-NLS-1$
300
}
301
302     public static String JavaDoc getAbsolutePath(String JavaDoc path) {
303         if (!path.startsWith("/")) { //$NON-NLS-1$
304
return "/" + path; //$NON-NLS-1$
305
}
306         return path;
307     }
308
309     public static String JavaDoc getNodePath(String JavaDoc path, String JavaDoc label) {
310         if (StringUtils.isEmpty(path) || (path.equals("/"))) { //$NON-NLS-1$
311
return label;
312         }
313         return getNodePath(path + "/" + label); //$NON-NLS-1$
314
}
315
316     public static String JavaDoc getNodePath(String JavaDoc path) {
317         if (path.startsWith("/")) { //$NON-NLS-1$
318
return path.replaceFirst("/", StringUtils.EMPTY); //$NON-NLS-1$
319
}
320         return path;
321     }
322
323     public static String JavaDoc getParentPath(String JavaDoc path) {
324         int lastIndexOfSlash = path.lastIndexOf("/"); //$NON-NLS-1$
325
if (lastIndexOfSlash > 0) {
326             return StringUtils.substringBefore(path, "/"); //$NON-NLS-1$
327
}
328         return "/"; //$NON-NLS-1$
329
}
330 }
331
Popular Tags