1 21 22 27 28 package net.walend.somnifugi.sql92; 29 30 35 public class RegularExpression 36 { 37 38 String expression = null; 39 Character escape = null; 40 41 public RegularExpression(String expression, String escape) 42 { 43 this.expression = expression; 44 45 if (escape != null) 46 { 47 this.escape = new Character (escape.charAt(0)); 48 } 49 } 50 51 public boolean equals(Object o) 52 { 53 if (this == o) return true; 54 55 if (!(o instanceof RegularExpression)) 56 { 57 return false; 58 } 59 RegularExpression obj = (RegularExpression)o; 60 return (expression.equals(obj.expression) && 61 escape.equals(obj.escape)); 62 } 63 64 public String toString() 65 { 66 return ("{re=" + expression + ", esc=" + escape + "}"); 67 } 68 69 public String getExpression() 70 { 71 return expression; 72 } 73 74 public Character getEscape() 75 { 76 return escape; 77 } 78 79 public int hashCode() 80 { 81 return expression.hashCode(); 82 } 83 84 public boolean match(String string) 85 { 86 return match(expression, 0, string, 0); 87 } 88 89 private boolean match(String re, int reStart, String value, int valStart) 90 { 91 92 int reLen = re.length(); 93 int vLen = value.length(); 94 95 int i = reStart; 96 int j = valStart; 97 98 char esc = 0; 99 if (escape != null) 100 { 101 esc = escape.charValue(); 102 } 103 char c; 104 105 boolean escaped = false; 106 107 do 108 { 109 110 c = re.charAt(i); 111 112 if (escape != null && c == esc) 114 { 115 escaped = true; 116 i++; 117 continue; 118 } 119 120 switch (c) 121 { 122 123 case '_': 125 if (escaped) 126 { 127 escaped = false; 128 if (c == value.charAt(j)) 130 { 131 i++; 133 j++; 134 } 135 else 136 { 137 return false; 139 } 140 } 141 else 142 { 143 i++; 145 j++; 146 } 147 break; 148 149 case '%': 150 if (escaped) 151 { 152 escaped = false; 153 if (c == value.charAt(j)) 155 { 156 i++; 158 j++; 159 } 160 else 161 { 162 return false; 164 } 165 } 166 else 167 { 168 i++; 171 if (i == reLen) 172 { 173 return true; 176 } 177 do 178 { 179 if (match(re, i, value, j)) 181 { 182 return true; 183 } 184 j++; 187 } while (j < vLen); 188 return false; 190 } 191 break; 192 193 default: 194 if (c == value.charAt(j)) 195 { 196 i++; 198 j++; 199 escaped = false; 200 } 201 else 202 { 203 return false; 205 } 206 break; 207 } 208 } while (j < vLen && i < reLen); 209 210 while (i < reLen && re.charAt(i) == '%') 212 { 213 i++; 214 } 215 216 if (j == vLen && i == reLen) 217 { 218 return true; 219 } 220 else 221 { 222 return false; 223 } 224 225 } 226 227 } 228 | Popular Tags |