1 13 package info.magnolia.cms.util; 14 15 import java.util.regex.Pattern ; 16 17 18 26 public final class SimpleUrlPattern implements UrlPattern { 27 28 31 private static final long serialVersionUID = 222L; 32 33 36 private static final String MULTIPLE_CHAR_PATTERN = "[\\p{L}[a-z[A-Z[0-9[!\"#$%&'()*+,-./:; <=>?@\\^_`{|}~\\[\\]]]]]]*"; 38 41 private static final String SINGLE_CHAR_PATTERN = "[\\p{L}[a-z[A-Z[0-9[!\"#$%&'()*+,-./:; <=>?@\\^_`{|}~\\[\\]]]]]]?"; 43 46 private Pattern pattern; 47 48 51 private int length; 52 53 58 public SimpleUrlPattern(String string) { 59 this.length = string.length(); 60 this.pattern = Pattern.compile(getEncodedString(string)); 61 } 62 63 69 private static String getEncodedString(String str) { 70 StringBuffer stringBuffer = new StringBuffer (); 71 char[] chars = str.toCharArray(); 72 int i = 0, last = 0; 73 while (i < chars.length) { 74 char c = chars[i]; 75 if (c == '*') { 76 stringBuffer.append(chars, last, i - last); 77 stringBuffer.append(MULTIPLE_CHAR_PATTERN); 78 last = i + 1; 79 } 80 else if (c == '?') { 81 stringBuffer.append(chars, last, i - last); 82 stringBuffer.append(SINGLE_CHAR_PATTERN); 83 last = i + 1; 84 } 85 i++; 86 } 87 stringBuffer.append(chars, last, i - last); 88 return stringBuffer.toString(); 89 } 90 91 94 public boolean match(String str) { 95 return this.pattern.matcher(str).matches(); 96 } 97 98 101 public int getLength() { 102 return this.length; 103 } 104 105 public String toString() { 106 return "SimpleUrlPattern{" + pattern + '}'; 107 } 108 } 109 | Popular Tags |