KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > selector > LikeSelector


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.selector;
30
31 import com.caucho.util.CharBuffer;
32
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 /**
38  * The like selector.
39  */

40 public class LikeSelector extends Selector {
41   private Selector _expr;
42   
43   private String JavaDoc _likeString;
44   private Pattern JavaDoc _pattern;
45
46   LikeSelector(Selector expr, String JavaDoc likeString)
47     throws JMSException JavaDoc
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   /**
106    * Returns true since the value is a boolean.
107    */

108   boolean isBoolean()
109   {
110     return true;
111   }
112
113   /**
114    * Returns false, since the type is known.
115    */

116   boolean isUnknown()
117   {
118     return false;
119   }
120
121   /**
122    * Evaluate the message. The boolean literal selector returns
123    * the value of the boolean.
124    */

125   public Object JavaDoc evaluate(Message JavaDoc message)
126     throws JMSException JavaDoc
127   {
128     Object JavaDoc value = _expr.evaluate(message);
129
130     if (value == null)
131       return null;
132
133     if (! (value instanceof String JavaDoc))
134       value = String.valueOf(value);
135
136     String JavaDoc s = (String JavaDoc) value;
137
138     return _pattern.matcher(s).find() ? Boolean.TRUE : Boolean.FALSE;
139   }
140
141   public String JavaDoc toString()
142   {
143     return _expr + " LIKE " + _likeString;
144   }
145 }
146
Popular Tags