KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > PathUtilities


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7  1. Redistributions of source code must retain the above copyright
8     notice, this list of conditions, and the following disclaimer.
9  2. Redistributions in binary form must reproduce the above copyright
10     notice, this list of conditions, and the disclaimer that follows
11     these conditions in the documentation and/or other materials
12     provided with the distribution.
13  3. The name "JPublish" must not be used to endorse or promote products
14     derived from this software without prior written permission. For
15     written permission, please contact info@aetrion.com.
16  4. Products derived from this software may not be called "JPublish", nor
17     may "JPublish" appear in their name, without prior written permission
18     from Aetrion LLC (info@aetrion.com).
19  In addition, the authors of this software request (but do not require)
20  that you include in the end-user documentation provided with the
21  redistribution and/or in the software itself an acknowledgement equivalent
22  to the following:
23      "This product includes software developed by
24       Aetrion LLC (http://www.aetrion.com/)."
25  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
26  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
29  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36  For more information on JPublish, please see <http://www.jpublish.org/>.
37  */

38 package org.jpublish.util;
39
40 import java.io.File JavaDoc;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.jpublish.SiteContext;
45 import org.jpublish.util.uri.RepositoryURI;
46 import org.jpublish.util.uri.TemplateURI;
47
48 /**
49  * Utility class for working with request paths.
50  *
51  * @author Anthony Eden
52  */

53
54 public final class PathUtilities {
55
56     /**
57      * Description of the Field
58      */

59     public final static String JavaDoc WILDCARD = "*";
60     /**
61      * Description of the Field
62      */

63     public final static String JavaDoc TEMPLATE_PROTOCOL = "template";
64     /**
65      * Description of the Field
66      */

67     public final static String JavaDoc REPOSITORY_PROTOCOL = "repository";
68
69     private static Log log = LogFactory.getLog(PathUtilities.class);
70
71     /**
72      * Internal constructor.
73      */

74
75     private PathUtilities() {
76         // no op
77
}
78
79     /**
80      * Match a path which may contain a wildcard.
81      *
82      * @param requestPath The request path submitted by the client
83      * @param exPath The match path
84      * @return Description of the Return Value
85      */

86
87     public static boolean match(String JavaDoc requestPath, String JavaDoc exPath) {
88         if (requestPath == null) {
89             return exPath.equals(WILDCARD);
90         }
91
92         int wildcardIndex = exPath.indexOf(WILDCARD);
93         if (wildcardIndex == -1) {
94             return exPath.equals(requestPath);
95         } else {
96             if (log.isDebugEnabled()) {
97                 log.debug("Wildcard index: " + wildcardIndex);
98             }
99             if (wildcardIndex == (exPath.length() - 1)) {
100                 String JavaDoc checkString = exPath.substring(0, exPath.length() - 1);
101                 boolean answer = requestPath.startsWith(checkString);
102                 return answer;
103             } else {
104                 String JavaDoc preMatch = exPath.substring(0, wildcardIndex);
105                 String JavaDoc postMatch = exPath.substring(wildcardIndex + 1);
106
107                 return requestPath.startsWith(preMatch) &&
108                         requestPath.endsWith(postMatch);
109             }
110         }
111     }
112
113     /**
114      * Extract the page name from the given path. The page name is the name of the file in the path without its suffix.
115      *
116      * @param path The request path
117      * @return The page name
118      */

119
120     public static String JavaDoc extractPageName(String JavaDoc path) {
121         File JavaDoc file = new File JavaDoc(path);
122
123         String JavaDoc fileName = file.getName();
124         int dotIndex = fileName.lastIndexOf(".");
125         if (dotIndex < 0) {
126             return null;
127         }
128
129         String JavaDoc pageName = fileName.substring(0, dotIndex);
130
131         return pageName;
132     }
133
134     /**
135      * Extract the page path from the given request path. This method will return the path from the page root to the
136      * page descriptor file.
137      *
138      * @param path The request path
139      * @return The page path
140      */

141
142     public static String JavaDoc extractPagePath(String JavaDoc path) {
143         File JavaDoc file = new File JavaDoc(path);
144         File JavaDoc parentDirectory = file.getParentFile();
145
146         String JavaDoc pagePath = null;
147
148         if (parentDirectory == null) {
149             pagePath = extractPageName(path);
150         } else {
151             pagePath = new File JavaDoc(parentDirectory.getPath(),
152                     extractPageName(path)).getPath();
153             pagePath = pagePath.replace(File.separatorChar, '/');
154         }
155
156         return pagePath;
157     }
158
159     /**
160      * Extract the parent path from the given path. If there is no parent path for the specified path then this method
161      * will return '/'. Otherwise this method will return a path representing the parent directory of the original
162      * path.
163      *
164      * @param path The path
165      * @return The parent path
166      */

167     public static String JavaDoc extractPageParent(String JavaDoc path) {
168         File JavaDoc file = new File JavaDoc(path);
169         File JavaDoc parentDirectory = file.getParentFile();
170
171         String JavaDoc pageParent = null;
172         if (parentDirectory == null) {
173             pageParent = "";
174         } else {
175             pageParent = parentDirectory.getPath();
176             pageParent = pageParent.replace(File.separatorChar, '/');
177         }
178
179         if (pageParent.startsWith("/")) {
180             pageParent = pageParent.substring(1);
181         }
182
183         return pageParent;
184     }
185
186     /**
187      * Return the page type extracting it from the path. For example: index.html would return "html" as the page type.
188      * If the type cannot be determined then this method returns null.
189      *
190      * @param path The path
191      * @return The page type
192      */

193     public static String JavaDoc extractPageType(String JavaDoc path) {
194         File JavaDoc file = new File JavaDoc(path);
195
196         String JavaDoc fileName = file.getName();
197
198         int dotIndex = fileName.lastIndexOf(".");
199         if (dotIndex < 0) {
200             return null;
201         }
202
203         String JavaDoc pageType = fileName.substring(dotIndex + 1);
204
205         return pageType;
206     }
207
208     /**
209      * Return a path String which includes a starting slash so that it matches the requirements of the Servlet API's
210      * getResource() methods.
211      *
212      * @param path The path
213      * @return The correct resource path
214      * @since 2.0
215      */

216     public static String JavaDoc toResourcePath(String JavaDoc path) {
217         if (path.startsWith("/")) {
218             return path;
219         } else {
220             return "/" + path;
221         }
222     }
223
224     /**
225      * Convert a path to a relative path if it is currently an absolute path.
226      *
227      * @param path The relative or absolute path
228      * @return A relative path
229      */

230     public static String JavaDoc toRelativePath(String JavaDoc path) {
231         if (path.startsWith("/")) {
232             return path.substring(1);
233         } else {
234             return path;
235         }
236     }
237
238     /**
239      * Make a URI path for a template.
240      *
241      * @param path The relative template path
242      * @param templateManagerName The template manager name (may be null)
243      * @return The URI
244      * @since 2.0
245      */

246
247     // Note: maybe move this into the TemplateManager?
248

249     public static String JavaDoc makeTemplateURI(String JavaDoc path,
250             String JavaDoc templateManagerName) {
251         TemplateURI uri = new TemplateURI();
252         uri.setTemplateManagerName(templateManagerName);
253         uri.setProtocol(TEMPLATE_PROTOCOL);
254         uri.setPath(path);
255         return uri.toURI();
256     }
257
258     /**
259      * Make a URI path for a repository item.
260      *
261      * @param repositoryName The repository name
262      * @param path The relative path
263      * @return The URI
264      * @since 2.0
265      */

266
267     // Note: maybe move this into the Repository?
268

269     public static String JavaDoc makeRepositoryURI(String JavaDoc repositoryName, String JavaDoc path) {
270         RepositoryURI uri = new RepositoryURI();
271         uri.setRepositoryName(repositoryName);
272         uri.setProtocol(REPOSITORY_PROTOCOL);
273         uri.setPath(path);
274         return uri.toURI();
275     }
276
277     /**
278      * Get the "real" path for the given request path. This method will append the default page to the request path if
279      * the path does not include a suffix.
280      *
281      * @param path The request path
282      * @param siteContext Description of the Parameter
283      * @return The real path
284      */

285
286     public static String JavaDoc getRealPath(SiteContext siteContext, String JavaDoc path) {
287         if (path == null) {
288             path = "";
289         }
290
291         if (path.lastIndexOf(".") == -1) {
292             if (!path.endsWith("/")) {
293                 path = path + "/";
294             }
295             path = path + siteContext.getDefaultPage();
296         }
297
298         return path;
299     }
300
301 }
302
303
Popular Tags