1 23 78 79 package javax.security.jacc; 80 81 89 90 91 class URLPattern extends Object implements Comparable 92 { 93 94 private static String DEFAULT_PATTERN = "/"; 95 96 private int patternType = -1; 97 98 private final String pattern; 99 100 public URLPattern () 101 { 102 this.pattern = DEFAULT_PATTERN; 103 this.patternType = PT_DEFAULT; 104 } 105 106 public URLPattern (String p) 108 { 109 if (p == null) { 110 this.pattern = DEFAULT_PATTERN; 111 this.patternType = PT_DEFAULT; 112 } 113 else this.pattern = p; 114 } 115 116 117 public static final int PT_DEFAULT = 0; 118 public static final int PT_EXTENSION = 1; 119 public static final int PT_PREFIX = 2; 120 public static final int PT_EXACT = 3; 121 122 public int patternType() { 123 if (this.patternType < 0) { 124 if (this.pattern.startsWith("*.")) 125 this.patternType = PT_EXTENSION; 126 else if (this.pattern.startsWith("/") && 127 this.pattern.endsWith("/*")) this.patternType = PT_PREFIX; 128 else if (this.pattern.equals(DEFAULT_PATTERN)) 129 this.patternType = PT_DEFAULT; 130 else this.patternType = PT_EXACT; 131 } 132 return this.patternType; 133 } 134 135 public int compareTo(Object o) { 136 137 if (!(o instanceof URLPattern )) 138 throw new ClassCastException ("argument must be URLPattern"); 139 140 URLPattern p = (URLPattern ) o; 141 142 if (p == null) p = new URLPattern (null); 143 144 int refPatternType = this.patternType(); 145 146 152 int result = refPatternType - p.patternType(); 153 154 if (result == 0) { 155 156 if (refPatternType == PT_PREFIX || refPatternType == PT_EXACT) { 157 158 result = this.getPatternDepth() - p.getPatternDepth(); 159 160 if (result == 0) result = this.pattern.compareTo(p.pattern); 161 162 } 163 164 else result = this.pattern.compareTo(p.pattern); 165 } 166 167 return (result > 0 ? 1 : (result < 0 ? -1 : 0)); 168 } 169 170 192 public boolean implies(URLPattern p) { 193 194 if (p == null) p = new URLPattern (null); 196 197 String path = p.pattern; 198 String pattern = this.pattern; 199 200 if (pattern.equals(path)) 202 return (true); 203 204 if (pattern.startsWith("/") && pattern.endsWith("/*")) { 206 pattern = pattern.substring(0, pattern.length() - 2); 207 208 int length = pattern.length(); 209 210 if (length == 0) return (true); 212 return (path.startsWith(pattern) && 213 (path.length() == length || 214 path.substring(length).startsWith("/"))); 215 } 216 217 if (pattern.startsWith("*.")) { 219 int slash = path.lastIndexOf('/'); 220 int period = path.lastIndexOf('.'); 221 if ((slash >= 0) && (period > slash) && 222 path.endsWith(pattern.substring(1))) { 223 return (true); 224 } 225 return (false); 226 } 227 228 if (pattern.equals(DEFAULT_PATTERN)) 230 return (true); 231 232 return (false); 233 } 234 235 public boolean equals(Object obj) { 236 if (! (obj instanceof URLPattern )) return false; 237 return this.pattern.equals(((URLPattern ) obj).pattern); 238 } 239 240 public String toString() { 241 return this.pattern; 242 } 243 244 public int getPatternDepth() { 245 246 int i = 0; 247 int depth = 1; 248 249 while (i >= 0) { 250 251 i = this.pattern.indexOf("/",i); 252 253 if (i >= 0 ) { 254 255 if (i == 0 && depth != 1) 256 throw new IllegalArgumentException ("// in pattern"); 257 258 i += 1; 259 } 260 } 261 262 return depth; 263 } 264 } 265 266 267 268 269 270 271 272 273 274 | Popular Tags |