001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jms.selector;
030:
031: import com.caucho.util.CharBuffer;
032:
033: import javax.jms.JMSException;
034: import javax.jms.Message;
035: import java.util.regex.Pattern;
036:
037: /**
038: * The like selector.
039: */
040: public class LikeSelector extends Selector {
041: private Selector _expr;
042:
043: private String _likeString;
044: private Pattern _pattern;
045:
046: LikeSelector(Selector expr, String likeString, char escape)
047: throws JMSException {
048: _expr = expr;
049:
050: _likeString = likeString;
051:
052: CharBuffer cb = new CharBuffer();
053: cb.append("^");
054:
055: for (int i = 0; i < likeString.length(); i++) {
056: char ch = likeString.charAt(i);
057:
058: if (ch == escape && i + 1 < likeString.length()) {
059: ch = likeString.charAt(i + 1);
060:
061: switch (ch) {
062: case '.':
063: case '[':
064: case ']':
065: case '(':
066: case ')':
067: case '|':
068: case '{':
069: case '}':
070: case '+':
071: case '*':
072: case '?':
073: case '\\':
074: case '$':
075: case '^':
076: cb.append('\\');
077: cb.append(ch);
078: break;
079:
080: default:
081: cb.append(ch);
082: }
083:
084: i++;
085: continue;
086: }
087:
088: switch (ch) {
089: case '_':
090: cb.append(".");
091: break;
092: case '%':
093: cb.append(".*");
094: break;
095: case '.':
096: case '[':
097: case ']':
098: case '(':
099: case ')':
100: case '|':
101: case '{':
102: case '}':
103: case '+':
104: case '*':
105: case '?':
106: case '\\':
107: case '$':
108: case '^':
109: cb.append('\\');
110: cb.append(ch);
111: break;
112:
113: default:
114: cb.append(ch);
115: break;
116: }
117: }
118:
119: cb.append("$");
120:
121: _pattern = Pattern.compile(cb.toString());
122: }
123:
124: /**
125: * Returns true since the value is a boolean.
126: */
127: boolean isBoolean() {
128: return true;
129: }
130:
131: /**
132: * Returns false, since the type is known.
133: */
134: boolean isUnknown() {
135: return false;
136: }
137:
138: /**
139: * Evaluate the message. The boolean literal selector returns
140: * the value of the boolean.
141: */
142: public Object evaluate(Message message) throws JMSException {
143: Object value = _expr.evaluate(message);
144:
145: if (value == null)
146: return null;
147:
148: // jms607l
149: if (!(value instanceof String))
150: return Boolean.FALSE;
151:
152: String s = (String) value;
153:
154: return _pattern.matcher(s).find() ? Boolean.TRUE
155: : Boolean.FALSE;
156: }
157:
158: public String toString() {
159: return _expr + " LIKE " + _likeString;
160: }
161: }
|