1 16 package org.apache.axis.utils; 17 18 import java.io.File ; 19 import java.net.URL ; 20 import java.util.HashSet ; 21 import java.util.StringTokenizer ; 22 23 28 public class URLHashSet extends HashSet { 29 30 36 public boolean add(URL url) { 37 return super.add(normalize(url)); 38 } 39 40 46 public boolean remove(URL url) { 47 return super.remove(normalize(url)); 48 } 49 50 56 public boolean contains(URL url) { 57 return super.contains(normalize(url)); 58 } 59 60 66 public static URL normalize(URL url) { 67 if (url.getProtocol().equals("file")) { 68 try { 69 File f = new File (cleanup(url.getFile())); 70 if(f.exists()) 71 return f.toURL(); 72 } catch (Exception e) {} 73 } 74 return url; 75 } 76 77 83 private static String cleanup(String uri) { 84 String [] dirty = tokenize(uri, "/\\", false); 85 int length = dirty.length; 86 String [] clean = new String [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 } 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 [length]; 115 } 116 } 117 StringBuffer b = new StringBuffer (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 144 private static String [] tokenize(String str, String delim, boolean returnTokens) { 145 StringTokenizer tokenizer = new StringTokenizer (str, delim, returnTokens); 146 String [] tokens = new String [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 |