1 43 44 package org.exolab.jms.selector; 45 46 import javax.jms.Message ; 47 48 import org.apache.oro.text.regex.Pattern; 49 import org.apache.oro.text.regex.PatternMatcher; 50 import org.apache.oro.text.regex.Perl5Matcher; 51 52 53 64 class LikeExpression extends IdentifierExpression { 65 66 69 private final Pattern _regexp; 70 71 74 private final PatternMatcher _matcher; 75 76 79 private final String _pattern; 80 81 84 private final String _escape; 85 86 87 96 public LikeExpression(final Identifier identifier, final String pattern, 97 final String escape) throws SelectorException { 98 super(identifier); 99 _pattern = pattern; 100 _escape = escape; 101 _regexp = getRegexp(_pattern, _escape); 102 _matcher = new Perl5Matcher(); 103 } 104 105 115 public final SObject evaluate(final Message msg) 116 throws TypeMismatchException { 117 SBool result = null; 118 SString value = TypeCaster.castToString(identifier().evaluate(msg), 119 "like expression"); 120 if (value != null) { 121 if (_matcher.matches((String ) value.getObject(), _regexp)) { 122 result = SBool.TRUE; 123 } else { 124 result = SBool.FALSE; 125 } 126 } 127 return result; 128 } 129 130 135 public final String toString() { 136 StringBuffer result = new StringBuffer (); 137 result.append('('); 138 result.append(identifier().toString()); 139 result.append(" like '"); 140 result.append(_pattern); 141 result.append('\''); 142 if (_escape != null) { 143 result.append(" escape '"); 144 result.append(_escape); 145 result.append('\''); 146 } 147 result.append(')'); 148 return result.toString(); 149 } 150 151 161 private Pattern getRegexp(final String pattern, final String escape) 162 throws SelectorException { 163 Pattern result = null; 164 Character esc = null; 165 166 if (escape != null) { 167 if (escape.length() != 1) { 168 throw new SelectorException("Invalid escape: " + escape); 169 } 170 esc = new Character (escape.charAt(0)); 171 } 172 173 try { 174 result = RegexpFactory.create(pattern, esc); 175 } catch (InvalidRegexpException err) { 176 throw new SelectorException("Invalid pattern: " + pattern); 177 } 178 return result; 179 } 180 181 } | Popular Tags |