1 28 29 package com.caucho.jms.selector; 30 31 import com.caucho.util.CharBuffer; 32 33 import javax.jms.JMSException ; 34 import javax.jms.Message ; 35 import java.util.regex.Pattern ; 36 37 40 public class LikeSelector extends Selector { 41 private Selector _expr; 42 43 private String _likeString; 44 private Pattern _pattern; 45 46 LikeSelector(Selector expr, String likeString) 47 throws JMSException 48 { 49 _expr = expr; 50 51 _likeString = likeString; 52 53 char escape = '\\'; 54 55 CharBuffer cb = new CharBuffer(); 56 cb.append("^"); 57 58 for (int i = 0; i < likeString.length(); i++) { 59 char ch = likeString.charAt(i); 60 61 if (ch == escape && i + 1 < likeString.length()) { 62 ch = likeString.charAt(i + 1); 63 64 switch (ch) { 65 case '.': case '[': case ']': case '(': case ')': case '|': 66 case '{': case '}': 67 case '+': case '*': case '?': case '\\': case '$': case '^': 68 cb.append('\\'); 69 cb.append(ch); 70 break; 71 72 default: 73 cb.append(ch); 74 } 75 76 i++; 77 continue; 78 } 79 80 switch (ch) { 81 case '_': 82 cb.append(".?"); 83 break; 84 case '%': 85 cb.append(".*"); 86 break; 87 case '.': case '[': case ']': case '(': case ')': case '|': 88 case '{': case '}': 89 case '+': case '*': case '?': case '\\': case '$': case '^': 90 cb.append('\\'); 91 cb.append(ch); 92 break; 93 94 default: 95 cb.append(ch); 96 break; 97 } 98 } 99 100 cb.append("$"); 101 102 _pattern = Pattern.compile(cb.toString()); 103 } 104 105 108 boolean isBoolean() 109 { 110 return true; 111 } 112 113 116 boolean isUnknown() 117 { 118 return false; 119 } 120 121 125 public Object evaluate(Message message) 126 throws JMSException 127 { 128 Object value = _expr.evaluate(message); 129 130 if (value == null) 131 return null; 132 133 if (! (value instanceof String )) 134 value = String.valueOf(value); 135 136 String s = (String ) value; 137 138 return _pattern.matcher(s).find() ? Boolean.TRUE : Boolean.FALSE; 139 } 140 141 public String toString() 142 { 143 return _expr + " LIKE " + _likeString; 144 } 145 } 146 | Popular Tags |