1 26 27 package org.objectweb.jonas_web.deployment.api; 28 29 30 38 public class Pattern implements Comparable { 39 40 44 private static final int EXACT = 0; 45 46 50 private static final int PATH_PREFIX = 1; 51 52 56 private static final int EXTENSION = 2; 57 58 62 private static final int DEFAULT = 3; 63 64 67 private String pattern = null; 68 69 72 private int type; 73 74 75 76 80 public Pattern(String pattern) { 81 this.pattern = pattern; 82 defineTypePattern(); 83 } 84 85 89 private void defineTypePattern() { 90 if (pattern.startsWith("/") && pattern.endsWith("/*")) { 91 type = PATH_PREFIX; 93 } else if (pattern.startsWith("*.")) { 94 type = EXTENSION; 96 } else if (pattern.equals("/")) { 97 type = DEFAULT; 99 } else { 100 type = EXACT; 102 } 103 } 104 105 106 111 public boolean isPathPrefix() { 112 return (type == PATH_PREFIX); 113 } 114 115 116 121 public boolean isExtensionPattern() { 122 return (type == EXTENSION); 123 } 124 125 126 131 public boolean isDefaultPattern() { 132 return (type == DEFAULT); 133 } 134 135 136 141 public boolean isExactPattern() { 142 return (type == EXACT); 143 } 144 145 146 155 public boolean isSubstringPattern(String substring) { 156 int size = substring.length(); 157 if (size == 0) { 158 return true; 159 } else { 160 return (pattern.startsWith(substring) 162 && (pattern.length() == size || pattern.substring(size).charAt(0) == '/')); 163 } 164 } 165 166 167 186 public boolean isMatching(Pattern otherPattern) { 187 if (pattern.equals(otherPattern)) { 188 return true; 190 } else if ((pattern.length() == 2) && isPathPrefix()) { 191 return true; 193 } else if (isPathPrefix() && otherPattern.isSubstringPattern(pattern.substring(0, pattern.length() - 2))) { 194 return true; 196 } else if (isExtensionPattern() && otherPattern.getValue().endsWith(pattern.substring(1))) { 197 return true; 199 } else { 200 return isDefaultPattern(); 202 } 203 } 204 205 209 public String getValue() { 210 return pattern; 211 } 212 213 214 218 public String toString() { 219 return getValue(); 220 } 221 222 227 public boolean equals(Object o) { 228 if (!(o instanceof Pattern)) { 229 return false; 230 } 231 return pattern.equals(((Pattern) o).getValue()); 232 } 233 234 238 public int hashCode() { 239 return pattern.hashCode(); 240 } 241 242 249 public int compareTo(Object o) { 250 if (!(o instanceof Pattern)) { 251 return -1; 252 } 253 return pattern.compareTo(((Pattern) o).getValue()); 254 } 255 256 } 257 | Popular Tags |