KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > URLHashSet


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.utils;
17
18 import java.io.File JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * Class URLHashSet
25  *
26  * @author Davanum Srinivas (dims@apache.org)
27  */

28 public class URLHashSet extends HashSet JavaDoc {
29
30     /**
31      * Adds the specified URL to this set if it is not already present.
32      *
33      * @param url url to be added to this set.
34      * @return true if the set did not already contain the specified element.
35      */

36     public boolean add(URL JavaDoc url) {
37         return super.add(normalize(url));
38     }
39
40     /**
41      * Removes the given URL from this set if it is present.
42      *
43      * @param url url to be removed from this set, if present.
44      * @return true if the set contained the specified element.
45      */

46     public boolean remove(URL JavaDoc url) {
47         return super.remove(normalize(url));
48     }
49
50     /**
51      * Returns true if this set contains the specified element.
52      *
53      * @param url url whose presence in this set is to be tested.
54      * @return true if this set contains the specified element.
55      */

56     public boolean contains(URL JavaDoc url) {
57         return super.contains(normalize(url));
58     }
59
60     /**
61      * if the url points to a file then make sure we cleanup ".." "." etc.
62      *
63      * @param url url to be normalized
64      * @return normalized url
65      */

66     public static URL JavaDoc normalize(URL JavaDoc url) {
67         if (url.getProtocol().equals("file")) {
68             try {
69                 File JavaDoc f = new File JavaDoc(cleanup(url.getFile()));
70                 if(f.exists())
71                     return f.toURL();
72             } catch (Exception JavaDoc e) {}
73         }
74         return url;
75     }
76
77     /**
78      * Normalize a uri containing ../ and ./ paths.
79      *
80      * @param uri The uri path to normalize
81      * @return The normalized uri
82      */

83     private static String JavaDoc cleanup(String JavaDoc uri) {
84         String JavaDoc[] dirty = tokenize(uri, "/\\", false);
85         int length = dirty.length;
86         String JavaDoc[] clean = new String JavaDoc[length];
87         boolean path;
88         boolean finished;
89
90         while (true) {
91             path = false;
92             finished = true;
93             for (int i = 0, j = 0; (i < length) && (dirty[i] != null); i++) {
94                 if (".".equals(dirty[i])) {
95                     // ignore
96
} else if ("..".equals(dirty[i])) {
97                     clean[j++] = dirty[i];
98                     if (path) {
99                         finished = false;
100                     }
101                 } else {
102                     if ((i + 1 < length) && ("..".equals(dirty[i + 1]))) {
103                         i++;
104                     } else {
105                         clean[j++] = dirty[i];
106                         path = true;
107                     }
108                 }
109             }
110             if (finished) {
111                 break;
112             } else {
113                 dirty = clean;
114                 clean = new String JavaDoc[length];
115             }
116         }
117         StringBuffer JavaDoc b = new StringBuffer JavaDoc(uri.length());
118
119         for (int i = 0; (i < length) && (clean[i] != null); i++) {
120             b.append(clean[i]);
121             if ((i + 1 < length) && (clean[i + 1] != null)) {
122                 b.append("/");
123             }
124         }
125         return b.toString();
126     }
127
128     /**
129      * Constructs a string tokenizer for the specified string. All characters
130      * in the delim argument are the delimiters for separating tokens.
131      * If the returnTokens flag is true, then the delimiter characters are
132      * also returned as tokens. Each delimiter is returned as a string of
133      * length one. If the flag is false, the delimiter characters are skipped
134      * and only serve as separators between tokens. Then tokenizes the str
135      * and return an String[] array with tokens.
136      *
137      * @param str a string to be parsed
138      * @param delim the delimiters
139      * @param returnTokens flag indicating whether to return the delimiters
140      * as tokens
141      *
142      * @return array with tokens
143      */

144     private static String JavaDoc[] tokenize(String JavaDoc str, String JavaDoc delim, boolean returnTokens) {
145         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(str, delim, returnTokens);
146         String JavaDoc[] tokens = new String JavaDoc[tokenizer.countTokens()];
147         int i = 0;
148         while (tokenizer.hasMoreTokens()) {
149             tokens[i] = tokenizer.nextToken();
150             i++;
151         }
152         return tokens;
153     }
154 }
155
Popular Tags