KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > util > Path


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.util;
16
17 /**
18  * @author Jacob Hookom
19  * @version $Id: Path.java,v 1.2 2005/08/24 04:38:53 jhook Exp $
20  */

21 public final class Path {
22
23     public static final String JavaDoc normalize(String JavaDoc path) {
24         if (path.length() == 0)
25             return path;
26         String JavaDoc n = path;
27         boolean abs = false;
28         while (n.indexOf('\\') >= 0) {
29             n = n.replace('\\', '/');
30         }
31         if (n.charAt(0) != '/') {
32             n = '/' + n;
33             abs = true;
34         }
35         int idx = 0;
36         while (true) {
37             idx = n.indexOf("%20");
38             if (idx == -1) {
39                 break;
40             }
41             n = n.substring(0, idx) + " " + n.substring(idx + 3);
42         }
43         while (true) {
44             idx = n.indexOf("/./");
45             if (idx == -1) {
46                 break;
47             }
48             n = n.substring(0, idx) + n.substring(idx + 2);
49         }
50         if (abs) {
51             n = n.substring(1);
52         }
53         return n;
54     }
55
56     public static final String JavaDoc relative(String JavaDoc ctx, String JavaDoc path) {
57         if (path.length() == 0) {
58             return context(ctx);
59         }
60         String JavaDoc c = context(normalize(ctx));
61         String JavaDoc p = normalize(path);
62         p = c + p;
63
64         int idx = 0;
65         while (true) {
66             idx = p.indexOf("/../");
67             if (idx == -1) {
68                 break;
69             }
70             int s = p.lastIndexOf('/', idx - 3);
71             if (s == -1) {
72                 break;
73             }
74             p = p.substring(0, s) + p.substring(idx + 3);
75         }
76         return p;
77     }
78
79     public static final String JavaDoc context(String JavaDoc path) {
80         int idx = path.lastIndexOf('/');
81         if (idx == -1) {
82             return "/";
83         }
84         return path.substring(0, idx + 1);
85     }
86
87 }
88
Popular Tags