KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > UriPath


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/UriPath.java,v 1.5 2004/07/28 09:38:08 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:38:08 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * An URI path.
30  *
31  * @version $Revision: 1.5 $
32  */

33 public final class UriPath {
34     
35     private String JavaDoc[] tokens;
36     
37     private UriPath() {
38     }
39     
40     private UriPath(String JavaDoc[] tokens) {
41         this.tokens = tokens;
42     }
43     
44     public UriPath(String JavaDoc uri) {
45         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc( uri, "/" );
46         this.tokens = new String JavaDoc[t.countTokens()];
47         for (int i = 0; t.hasMoreTokens(); i++) {
48             tokens[i] = t.nextToken();
49         }
50     }
51     
52     public String JavaDoc[] tokens() {
53         return tokens;
54     }
55     
56     public String JavaDoc lastSegment() {
57         return tokens.length > 0
58             ? tokens[ tokens.length - 1 ]
59             : null;
60     }
61     
62     public UriPath parent() {
63         if (this.tokens.length == 0) {
64             return null;
65         }
66         return subUriPath(0, tokens.length - 1);
67     }
68     
69     public UriPath child( String JavaDoc segment ) {
70         String JavaDoc[] ctokens = new String JavaDoc[tokens.length + 1];
71         for (int i = 0; i < tokens.length; i++) {
72             ctokens[i] = tokens[i];
73         }
74         ctokens[tokens.length] = segment;
75         return new UriPath(ctokens);
76     }
77     
78     public UriPath subUriPath(int start, int end) {
79         UriPath result = new UriPath();
80         result.tokens = new String JavaDoc[end - start];
81         System.arraycopy(tokens, start, result.tokens, 0, result.tokens.length);
82         return result;
83     }
84     
85     public boolean equals(Object JavaDoc o) {
86         boolean result = false;
87         if (o instanceof UriPath) {
88             UriPath other = (UriPath)o;
89             if (other.tokens.length == this.tokens.length) {
90                 result = true;
91                 for (int i = 0; i < this.tokens.length; i++) {
92                     if (!other.tokens[i].equals(this.tokens[i])) {
93                         result = false;
94                         break;
95                     }
96                 }
97             }
98         }
99         return result;
100     }
101     
102     public int hashCode() {
103         if (tokens.length > 0) {
104             return tokens[tokens.length - 1].hashCode();
105         }
106         else {
107             return 0;
108         }
109     }
110     
111     public String JavaDoc toString() {
112         if (tokens.length > 0) {
113             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
114             for (int i = 0; i < tokens.length; i++) {
115                 b.append("/").append(tokens[i]);
116             }
117             return b.toString();
118         }
119         else {
120             return "/";
121         }
122     }
123 }
124
Popular Tags