1 package org.apache.oro.text; 2 3 59 60 import org.apache.oro.text.regex.*; 61 62 116 public final class GlobCompiler implements PatternCompiler { 117 123 public static final int DEFAULT_MASK = 0; 124 125 129 public static final int CASE_INSENSITIVE_MASK = 0x0001; 130 131 138 public static final int STAR_CANNOT_MATCH_NULL_MASK = 0x0002; 139 140 146 public static final int QUESTION_MATCHES_ZERO_OR_ONE_MASK = 0x0004; 147 148 private Perl5Compiler __perl5Compiler; 149 150 private static boolean __isPerl5MetaCharacter(char ch) { 151 return (ch == '*' || ch == '?' || ch == '+' || ch == '[' || ch == ']' || 152 ch == '(' || ch == ')' || ch == '|' || ch == '^' || ch == '$' || 153 ch == '.' || ch == '{' || ch == '}' || ch == '\\'); 154 } 155 156 private static boolean __isGlobMetaCharacter(char ch) { 157 return (ch == '*' || ch == '?' || ch == '[' || ch == ']'); 158 } 159 160 174 public static String globToPerl5(char[] pattern, int options) { 175 boolean inCharSet, starCannotMatchNull = false, questionMatchesZero; 176 int ch; 177 StringBuffer buffer; 178 179 buffer = new StringBuffer (2*pattern.length); 180 inCharSet = false; 181 182 questionMatchesZero = ((options & QUESTION_MATCHES_ZERO_OR_ONE_MASK) != 0); 183 starCannotMatchNull = ((options & STAR_CANNOT_MATCH_NULL_MASK) != 0); 184 185 for(ch=0; ch < pattern.length; ch++) { 186 switch(pattern[ch]) { 187 case '*': 188 if(inCharSet) 189 buffer.append('*'); 190 else { 191 if(starCannotMatchNull) 192 buffer.append(".+"); 193 else 194 buffer.append(".*"); 195 } 196 break; 197 case '?': 198 if(inCharSet) 199 buffer.append('?'); 200 else { 201 if(questionMatchesZero) 202 buffer.append(".?"); 203 else 204 buffer.append('.'); 205 } 206 break; 207 case '[': 208 inCharSet = true; 209 buffer.append(pattern[ch]); 210 211 if(ch + 1 < pattern.length) { 212 switch(pattern[ch + 1]) { 213 case '!': 214 case '^': 215 buffer.append('^'); 216 ++ch; 217 continue; 218 case ']': 219 buffer.append(']'); 220 ++ch; 221 continue; 222 } 223 } 224 break; 225 case ']': 226 inCharSet = false; 227 buffer.append(pattern[ch]); 228 break; 229 case '\\': 230 buffer.append('\\'); 231 if(ch == pattern.length - 1) { 232 buffer.append('\\'); 233 } else if(__isGlobMetaCharacter(pattern[ch + 1])) 234 buffer.append(pattern[++ch]); 235 else 236 buffer.append('\\'); 237 break; 238 default: 239 if(!inCharSet && __isPerl5MetaCharacter(pattern[ch])) 240 buffer.append('\\'); 241 buffer.append(pattern[ch]); 242 break; 243 } 244 } 245 246 return buffer.toString(); 247 } 248 249 250 254 public GlobCompiler() { 255 __perl5Compiler = new Perl5Compiler(); 256 } 257 258 259 283 public Pattern compile(char[] pattern, int options) 284 throws MalformedPatternException 285 { 286 int perlOptions = 0; 287 if((options & CASE_INSENSITIVE_MASK) != 0) 288 perlOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK; 289 return __perl5Compiler.compile(globToPerl5(pattern, options), perlOptions); 290 } 291 292 302 public Pattern compile(char[] pattern) throws MalformedPatternException { 303 return compile(pattern, DEFAULT_MASK); 304 } 305 306 316 public Pattern compile(String pattern) throws MalformedPatternException { 317 return compile(pattern.toCharArray(), DEFAULT_MASK); 318 } 319 320 344 public Pattern compile(String pattern, int options) 345 throws MalformedPatternException 346 { 347 return compile(pattern.toCharArray(), options); 348 } 349 350 } 351 352 | Popular Tags |