1 55 package org.jboss.axis.utils; 56 57 import java.io.File ; 58 import java.net.URL ; 59 import java.util.HashSet ; 60 import java.util.StringTokenizer ; 61 62 67 public class URLHashSet extends HashSet 68 { 69 70 76 public boolean add(URL url) 77 { 78 return super.add(normalize(url)); 79 } 80 81 87 public boolean remove(URL url) 88 { 89 return super.remove(normalize(url)); 90 } 91 92 98 public boolean contains(URL url) 99 { 100 return super.contains(normalize(url)); 101 } 102 103 109 public static URL normalize(URL url) 110 { 111 if (url.getProtocol().equals("file")) 112 { 113 try 114 { 115 File f = new File (cleanup(url.getFile())); 116 if (f.exists()) 117 return f.toURL(); 118 } 119 catch (Exception e) 120 { 121 } 122 } 123 return url; 124 } 125 126 132 private static String cleanup(String uri) 133 { 134 String [] dirty = tokenize(uri, "/\\", false); 135 int length = dirty.length; 136 String [] clean = new String [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 } 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 [length]; 179 } 180 } 181 StringBuffer b = new StringBuffer (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 209 private static String [] tokenize(String str, String delim, boolean returnTokens) 210 { 211 StringTokenizer tokenizer = new StringTokenizer (str, delim, returnTokens); 212 String [] tokens = new String [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 |