01: /*
02: * $Id: Path.java,v 1.4 2004/10/05 22:08:47 csaltos Exp $
03: *
04: * Copyright 1999 PUCE [http://www.puce.edu.ec]
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18: package org.oxyus.util;
19:
20: /**
21: * @author Carlos Saltos (csaltos[@]users.sourceforge.net)
22: */
23: public class Path {
24:
25: /**
26: * Calculates and absolute path using a relative path, replacing the
27: * directories which contains <code>..</code>
28: * @param path Relative path
29: * @return Absolute path
30: */
31: public static String normalize(String path) {
32: if (path == null || path.length() == 0) {
33: return path;
34: }
35: int level;
36: while ((level = path.indexOf("/../")) != -1) {
37: int last;
38: int pos = path.indexOf('/');
39: do {
40: last = pos;
41: pos = path.indexOf('/', pos + 1);
42: } while (pos < level);
43: path = path.substring(0, last) + path.substring(level + 3);
44: }
45: return path;
46: }
47:
48: }
|