KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > servlet > servlets > Path


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: Path.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25 package org.enhydra.servlet.servlets;
26
27 import java.io.File JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * Utility class to help manipulate paths. Method cleans up multiple
32  * '/' or '\' when constructing the object and prevents adding additional
33  * '/' or '\' when appending paths. For the purposes of this class a
34  * 'UrlPath' is a path with the path separators character of '/',
35  * regardless of platform; A 'FilePath' is a path with system dependent
36  * path separators characters (UNIX - '/' DOS - '\').
37  *
38  * @version $Revision: 1.2 $
39  * @author Mark Sanguinetti
40  */

41 class Path {
42
43     /*
44      * Internal represenation of a path.
45      * This class always stores paths in URL format ('/').
46      */

47     private String JavaDoc path = null;
48
49     /**
50      * Constructs a Path Object based on String p.
51      *
52      * @param p used to form the path.
53      */

54     Path(String JavaDoc p) {
55         path = cleanUpPath(p);
56     }
57
58     /**
59      * Constructs a Path Object as by appending p2 to the end
60      * of p1.
61      *
62      * @param p1 the first part of the path.
63      * @param p2 the second part of the path.
64      */

65     Path(String JavaDoc p1, String JavaDoc p2) {
66         path = cleanUpPath(p1);
67         path = appendPath(p2);
68     }
69
70     /**
71      * Converts the Path object to 'UrlPath' format. 'UrlPath' is
72      * a path with the path separators character of '/', regardless
73      * of platform.
74      *
75      * @return Path in URL format.
76      */

77     public String JavaDoc getUrlPath() {
78         return path;
79     }
80
81     /**
82      * Converts the Path object to 'FilePath' format. A 'FilePath'
83      * is a path with system dependent path separators characters
84      * (UNIX - '/' DOS - '\').
85      *
86      * @return Path in FilePath format.
87      */

88     public String JavaDoc getFilePath() {
89         return (path.replace('/', File.separatorChar));
90     }
91
92     /**
93      * Appends a path to the end of this object, and returns a
94      * string representing the new Url path without changing the
95      * this object.
96      *
97      * @param p The path to append to the end of this object.
98      * @return The new Url path.
99      */

100     public String JavaDoc appendUrlPath(String JavaDoc p) {
101         return appendPath(p);
102     }
103
104     /**
105      * Appends a path to the end of this object, and returns a
106      * string representing the new File path without changing the
107      * this object.
108      *
109      * @param p The path to append to the end of this object.
110      * @return The new File path.
111      */

112     public String JavaDoc appendFilePath(String JavaDoc p) {
113         return (appendPath(p).replace('/', File.separatorChar));
114     }
115
116     /*
117      *
118      */

119     private String JavaDoc appendPath(String JavaDoc p) {
120
121         if (path == null) {
122             return null;
123         }
124         return (p == null ? path : path + cleanUpPath(p));
125     }
126
127     /*
128      *
129      */

130     private String JavaDoc cleanUpPath(String JavaDoc p) {
131
132         // covert to internal represenation of a path ('/' format)
133
String JavaDoc tmp = p.replace('\\', '/');
134         if (!tmp.startsWith("/")) {
135             tmp = "/" + tmp;
136         }
137         if (!tmp.endsWith("/")) {
138             tmp = tmp + "/";
139         }
140
141         // parse out info in path and create a cleaned up
142
// url path
143
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(tmp, "/");
145         sb.append("/");
146         while (st.hasMoreTokens()) {
147             String JavaDoc tok = st.nextToken();
148             if (st.countTokens() > 0) {
149                 sb.append(tok);
150                 sb.append("/");
151             } else if (st.countTokens() == 0) {
152                 sb.append(tok);
153             } else {
154                 // should never happen
155
System.err.println("ERROR:Path.cleanUpPath()");
156             }
157         }
158         return sb.toString();
159     }
160
161 }
162
Popular Tags