1 46 package org.mr.api.jms.selector.syntax; 47 48 49 import java.util.regex.Pattern ; 50 import java.util.regex.PatternSyntaxException ; 51 52 53 58 final class RegexpFactory { 59 60 63 private RegexpFactory() { 64 } 66 67 75 public static Pattern create(final String pattern, final Character escape) throws InvalidRegexpException { 76 77 StringBuffer buff = new StringBuffer (); 78 buff.append('^'); 79 80 for (int i = 0; i < pattern.length(); ++i) { 81 boolean escaped = false; 82 char ch = pattern.charAt(i); 83 if (escape != null && ch == escape.charValue()) { 84 if (++i < pattern.length()) { 85 ch = pattern.charAt(i); 86 escaped = true; 87 } else { 89 throw new InvalidRegexpException(pattern); 90 } } 93 switch (ch) { 94 case '_': 95 if (escaped) { 96 buff.append(ch); 97 } else { 99 buff.append('.'); 100 } break; 102 case '%': 103 if (escaped) { 104 buff.append(ch); 105 } else { 107 buff.append(".*"); 108 } break; 110 case '.': 111 case '[': 112 case ']': 113 case '\\': 114 case '+': 115 case '?': 116 case '{': 117 case '}': 118 case '$': 119 case '^': 120 case '|': 121 case '(': 122 case ')': 123 buff.append('\\'); 126 buff.append(ch); 127 break; 128 default: 129 buff.append(ch); 130 } } buff.append('$'); 133 134 Pattern result = null; 135 try { 136 result = Pattern.compile(buff.toString()); 139 } catch (PatternSyntaxException exception) { 141 throw new InvalidRegexpException(pattern); 143 } return result; 145 }} | Popular Tags |