Pattern.java

00001 
00027 package org.objectweb.jonas_web.deployment.api;
00028 
00029 
00038 public class Pattern implements Comparable {
00039 
00044     private static final int EXACT = 0;
00045 
00050     private static final int PATH_PREFIX = 1;
00051 
00056     private static final int EXTENSION = 2;
00057 
00062     private static final int DEFAULT = 3;
00063 
00067     private String pattern = null;
00068 
00072     private int type;
00073 
00074 
00075 
00080     public Pattern(String pattern) {
00081         this.pattern = pattern;
00082         defineTypePattern();
00083     }
00084 
00089     private void defineTypePattern() {
00090         if (pattern.startsWith("/") && pattern.endsWith("/*")) {
00091             // Path prefix
00092             type = PATH_PREFIX;
00093         } else if (pattern.startsWith("*.")) {
00094             // Extension
00095             type = EXTENSION;
00096         } else if (pattern.equals("/")) {
00097             // Default
00098             type = DEFAULT;
00099         } else {
00100             // else it is EXACT
00101             type = EXACT;
00102         }
00103     }
00104 
00105 
00111     public boolean isPathPrefix() {
00112         return (type == PATH_PREFIX);
00113     }
00114 
00115 
00121     public boolean isExtensionPattern() {
00122         return (type == EXTENSION);
00123     }
00124 
00125 
00131     public boolean isDefaultPattern() {
00132         return (type == DEFAULT);
00133     }
00134 
00135 
00141     public boolean isExactPattern() {
00142         return (type == EXACT);
00143     }
00144 
00145 
00155     public boolean isSubstringPattern(String substring) {
00156         int size = substring.length();
00157         if (size == 0) {
00158             return true;
00159         } else {
00160             // true if next character = '/' if any or true if equals (no last char)
00161             return (pattern.startsWith(substring)
00162                     && (pattern.length() == size || pattern.substring(size).charAt(0) == '/'));
00163         }
00164     }
00165 
00166 
00186     public boolean isMatching(Pattern otherPattern) {
00187         if (pattern.equals(otherPattern)) {
00188             // case 1
00189             return true;
00190         } else if ((pattern.length() == 2) && isPathPrefix()) {
00191             // case 2
00192             return true;
00193         } else if (isPathPrefix() && otherPattern.isSubstringPattern(pattern.substring(0, pattern.length() - 2))) {
00194             // case 3
00195             return true;
00196         } else if (isExtensionPattern() && otherPattern.getValue().endsWith(pattern.substring(1))) {
00197             // case 4
00198             return true;
00199          } else {
00200              // case 5 or no match
00201              return isDefaultPattern();
00202          }
00203     }
00204 
00209     public String getValue() {
00210         return pattern;
00211     }
00212 
00213 
00218     public String toString() {
00219         return getValue();
00220     }
00221 
00227     public boolean equals(Object o) {
00228         if (!(o instanceof Pattern)) {
00229             return false;
00230         }
00231         return pattern.equals(((Pattern) o).getValue());
00232     }
00233 
00238     public int hashCode() {
00239         return pattern.hashCode();
00240     }
00241 
00249     public int compareTo(Object o) {
00250         if (!(o instanceof Pattern)) {
00251             return -1;
00252         }
00253         return pattern.compareTo(((Pattern) o).getValue());
00254     }
00255 
00256 }

Generated on Tue Feb 15 15:05:57 2005 for JOnAS by  doxygen 1.3.9.1