001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: Path.java,v 1.2 2006-06-15 14:07:00 sinisa Exp $
022: */
023:
024: package org.enhydra.servlet.servlets;
025:
026: import java.io.File;
027: import java.util.StringTokenizer;
028:
029: /**
030: * Utility class to help manipulate paths. Method cleans up multiple
031: * '/' or '\' when constructing the object and prevents adding additional
032: * '/' or '\' when appending paths. For the purposes of this class a
033: * 'UrlPath' is a path with the path separators character of '/',
034: * regardless of platform; A 'FilePath' is a path with system dependent
035: * path separators characters (UNIX - '/' DOS - '\').
036: *
037: * @version $Revision: 1.2 $
038: * @author Mark Sanguinetti
039: */
040: class Path {
041:
042: /*
043: * Internal represenation of a path.
044: * This class always stores paths in URL format ('/').
045: */
046: private String path = null;
047:
048: /**
049: * Constructs a Path Object based on String p.
050: *
051: * @param p used to form the path.
052: */
053: Path(String p) {
054: path = cleanUpPath(p);
055: }
056:
057: /**
058: * Constructs a Path Object as by appending p2 to the end
059: * of p1.
060: *
061: * @param p1 the first part of the path.
062: * @param p2 the second part of the path.
063: */
064: Path(String p1, String p2) {
065: path = cleanUpPath(p1);
066: path = appendPath(p2);
067: }
068:
069: /**
070: * Converts the Path object to 'UrlPath' format. 'UrlPath' is
071: * a path with the path separators character of '/', regardless
072: * of platform.
073: *
074: * @return Path in URL format.
075: */
076: public String getUrlPath() {
077: return path;
078: }
079:
080: /**
081: * Converts the Path object to 'FilePath' format. A 'FilePath'
082: * is a path with system dependent path separators characters
083: * (UNIX - '/' DOS - '\').
084: *
085: * @return Path in FilePath format.
086: */
087: public String getFilePath() {
088: return (path.replace('/', File.separatorChar));
089: }
090:
091: /**
092: * Appends a path to the end of this object, and returns a
093: * string representing the new Url path without changing the
094: * this object.
095: *
096: * @param p The path to append to the end of this object.
097: * @return The new Url path.
098: */
099: public String appendUrlPath(String p) {
100: return appendPath(p);
101: }
102:
103: /**
104: * Appends a path to the end of this object, and returns a
105: * string representing the new File path without changing the
106: * this object.
107: *
108: * @param p The path to append to the end of this object.
109: * @return The new File path.
110: */
111: public String appendFilePath(String p) {
112: return (appendPath(p).replace('/', File.separatorChar));
113: }
114:
115: /*
116: *
117: */
118: private String appendPath(String p) {
119:
120: if (path == null) {
121: return null;
122: }
123: return (p == null ? path : path + cleanUpPath(p));
124: }
125:
126: /*
127: *
128: */
129: private String cleanUpPath(String p) {
130:
131: // covert to internal represenation of a path ('/' format)
132: String tmp = p.replace('\\', '/');
133: if (!tmp.startsWith("/")) {
134: tmp = "/" + tmp;
135: }
136: if (!tmp.endsWith("/")) {
137: tmp = tmp + "/";
138: }
139:
140: // parse out info in path and create a cleaned up
141: // url path
142: StringBuffer sb = new StringBuffer();
143: StringTokenizer st = new StringTokenizer(tmp, "/");
144: sb.append("/");
145: while (st.hasMoreTokens()) {
146: String tok = st.nextToken();
147: if (st.countTokens() > 0) {
148: sb.append(tok);
149: sb.append("/");
150: } else if (st.countTokens() == 0) {
151: sb.append(tok);
152: } else {
153: // should never happen
154: System.err.println("ERROR:Path.cleanUpPath()");
155: }
156: }
157: return sb.toString();
158: }
159:
160: }
|