1 46 package org.mr.api.jms.selector.syntax; 47 48 import javax.jms.Message ; 49 import java.util.regex.Pattern ; 50 import java.util.regex.Matcher ; 51 52 58 class LikeExpression extends IdentifierExpression { 59 60 63 private final Pattern regexp; 64 65 68 private Matcher matcher; 69 70 73 private final String pattern; 74 75 78 private final String escape; 79 80 81 89 public LikeExpression(final Identifier identifier, final String pattern, final String escape) throws SelectorException { 90 super(identifier); 91 this.pattern = pattern; 92 this.escape = escape; 93 regexp = getRegexp(pattern, escape); 94 matcher = null; } 97 98 108 public final MantaObject evaluate(final Message msg) throws TypeMismatchException { 109 110 MantaBoolean result = null; 111 MantaString value = TypeCaster.castToString(identifier().evaluate(msg),"like expression"); 112 113 if (value != null) { 114 115 matcher =regexp.matcher((String ) value.getObject()); 117 if (matcher.find()){ 118 result = MantaBoolean.TRUE; 119 } else { 121 result = MantaBoolean.FALSE; 122 } } return result; 125 } 127 128 133 public final String toString() { 134 StringBuffer result = new StringBuffer (); 135 result.append('('); 136 result.append(identifier().toString()); 137 result.append(" like '"); 138 result.append(pattern); 139 result.append('\''); 140 if (escape != null) { 141 result.append(" escape '"); 142 result.append(escape); 143 result.append('\''); 144 } result.append(')'); 146 return result.toString(); 147 } 149 150 158 private Pattern getRegexp(final String pattern, final String escape) throws SelectorException { 159 Pattern result = null; 160 Character esc = null; 161 162 if (escape != null) { 163 if (escape.length() != 1) { 164 throw new SelectorException("Invalid escape: " + escape); 165 } esc = new Character (escape.charAt(0)); 167 } 169 try { 170 result = RegexpFactory.create(pattern, esc); 171 } catch (InvalidRegexpException err) { 173 throw new SelectorException("Invalid pattern: " + pattern); 174 } return result; 176 }} | Popular Tags |