KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.utils;
56
57 import java.io.File JavaDoc;
58 import java.net.URL JavaDoc;
59 import java.util.HashSet JavaDoc;
60 import java.util.StringTokenizer JavaDoc;
61
62 /**
63  * Class URLHashSet
64  *
65  * @author Davanum Srinivas (dims@apache.org)
66  */

67 public class URLHashSet extends HashSet JavaDoc
68 {
69
70    /**
71     * Adds the specified URL to this set if it is not already present.
72     *
73     * @param url url to be added to this set.
74     * @return true if the set did not already contain the specified element.
75     */

76    public boolean add(URL JavaDoc url)
77    {
78       return super.add(normalize(url));
79    }
80
81    /**
82     * Removes the given URL from this set if it is present.
83     *
84     * @param url url to be removed from this set, if present.
85     * @return true if the set contained the specified element.
86     */

87    public boolean remove(URL JavaDoc url)
88    {
89       return super.remove(normalize(url));
90    }
91
92    /**
93     * Returns true if this set contains the specified element.
94     *
95     * @param url url whose presence in this set is to be tested.
96     * @return true if this set contains the specified element.
97     */

98    public boolean contains(URL JavaDoc url)
99    {
100       return super.contains(normalize(url));
101    }
102
103    /**
104     * if the url points to a file then make sure we cleanup ".." "." etc.
105     *
106     * @param url url to be normalized
107     * @return normalized url
108     */

109    public static URL JavaDoc normalize(URL JavaDoc url)
110    {
111       if (url.getProtocol().equals("file"))
112       {
113          try
114          {
115             File JavaDoc f = new File JavaDoc(cleanup(url.getFile()));
116             if (f.exists())
117                return f.toURL();
118          }
119          catch (Exception JavaDoc e)
120          {
121          }
122       }
123       return url;
124    }
125
126    /**
127     * Normalize a uri containing ../ and ./ paths.
128     *
129     * @param uri The uri path to normalize
130     * @return The normalized uri
131     */

132    private static String JavaDoc cleanup(String JavaDoc uri)
133    {
134       String JavaDoc[] dirty = tokenize(uri, "/\\", false);
135       int length = dirty.length;
136       String JavaDoc[] clean = new String JavaDoc[length];
137       boolean path;
138       boolean finished;
139
140       while (true)
141       {
142          path = false;
143          finished = true;
144          for (int i = 0, j = 0; (i < length) && (dirty[i] != null); i++)
145          {
146             if (".".equals(dirty[i]))
147             {
148                // ignore
149
}
150             else if ("..".equals(dirty[i]))
151             {
152                clean[j++] = dirty[i];
153                if (path)
154                {
155                   finished = false;
156                }
157             }
158             else
159             {
160                if ((i + 1 < length) && ("..".equals(dirty[i + 1])))
161                {
162                   i++;
163                }
164                else
165                {
166                   clean[j++] = dirty[i];
167                   path = true;
168                }
169             }
170          }
171          if (finished)
172          {
173             break;
174          }
175          else
176          {
177             dirty = clean;
178             clean = new String JavaDoc[length];
179          }
180       }
181       StringBuffer JavaDoc b = new StringBuffer JavaDoc(uri.length());
182
183       for (int i = 0; (i < length) && (clean[i] != null); i++)
184       {
185          b.append(clean[i]);
186          if ((i + 1 < length) && (clean[i + 1] != null))
187          {
188             b.append("/");
189          }
190       }
191       return b.toString();
192    }
193
194    /**
195     * Constructs a string tokenizer for the specified string. All characters
196     * in the delim argument are the delimiters for separating tokens.
197     * If the returnTokens flag is true, then the delimiter characters are
198     * also returned as tokens. Each delimiter is returned as a string of
199     * length one. If the flag is false, the delimiter characters are skipped
200     * and only serve as separators between tokens. Then tokenizes the str
201     * and return an String[] array with tokens.
202     *
203     * @param str a string to be parsed
204     * @param delim the delimiters
205     * @param returnTokens flag indicating whether to return the delimiters
206     * as tokens
207     * @return array with tokens
208     */

209    private static String JavaDoc[] tokenize(String JavaDoc str, String JavaDoc delim, boolean returnTokens)
210    {
211       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(str, delim, returnTokens);
212       String JavaDoc[] tokens = new String JavaDoc[tokenizer.countTokens()];
213       int i = 0;
214       while (tokenizer.hasMoreTokens())
215       {
216          tokens[i] = tokenizer.nextToken();
217          i++;
218       }
219       return tokens;
220    }
221 }
222
Popular Tags