KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > selector > syntax > LikeExpression


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms.selector.syntax;
47
48 import javax.jms.Message JavaDoc;
49 import java.util.regex.Pattern JavaDoc;
50 import java.util.regex.Matcher JavaDoc;
51
52 /**
53  * This class implements a 'like' expression.
54  * This is an expression that returns true if a identifier's value matches
55  * a pattern
56  *
57  */

58 class LikeExpression extends IdentifierExpression {
59
60     /**
61      * The regular expression
62      */

63     private final Pattern JavaDoc regexp;
64
65     /**
66      * The pattern matcher
67      */

68     private Matcher JavaDoc matcher;
69
70     /**
71      * The pattern
72      */

73     private final String JavaDoc pattern;
74
75     /**
76      * The escape character. May be null.
77      */

78     private final String JavaDoc escape;
79
80
81     /**
82      * Construct a new LikeExpression
83      *
84      * @param identifier the identifier
85      * @param pattern the pattern
86      * @param escape the escape character. May be null.
87      * @throws SelectorException if pattern or escape are invalid
88      */

89     public LikeExpression(final Identifier identifier, final String JavaDoc pattern, final String JavaDoc escape) throws SelectorException {
90         super(identifier);
91         this.pattern = pattern;
92         this.escape = escape;
93         regexp = getRegexp(pattern, escape);
94         matcher = null;//regexp.matcher();
95
}//LikeExpression
96

97
98     /**
99      * Evaluate the expression
100      *
101      * @param msg the message to use to obtain any header identifier and
102      * property values
103      * @return the evaluated result, or null if the value of the
104      * expression is unknown
105      * @throws TypeMismatchException if the expression tries to evaluate
106      * mismatched types.
107      */

108     public final MantaObject evaluate(final Message JavaDoc 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             //if (matcher.matches((String) value.getObject(), regexp)) {
116
matcher =regexp.matcher((String JavaDoc) value.getObject());
117             if (matcher.find()){
118                 result = MantaBoolean.TRUE;
119             }//if
120
else {
121                 result = MantaBoolean.FALSE;
122             }//else
123
}//if
124
return result;
125     }//evaluate
126

127
128     /**
129      * Return a string representation of this expression.
130      *
131      * @return a string representation of this expression
132      */

133     public final String JavaDoc toString() {
134         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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         }//if
145
result.append(')');
146         return result.toString();
147     }//toString
148

149
150     /**
151      * Converts the pattern and escape to an ORO Pattern
152      *
153      * @param pattern the pattern
154      * @param escape the escape character. May be null
155      * @return an ORO Pattern corresponding to pattern and escape
156      * @throws SelectorException if pattern or escape are invalid
157      */

158     private Pattern JavaDoc getRegexp(final String JavaDoc pattern, final String JavaDoc escape) throws SelectorException {
159         Pattern JavaDoc result = null;
160         Character JavaDoc esc = null;
161
162         if (escape != null) {
163             if (escape.length() != 1) {
164                 throw new SelectorException("Invalid escape: " + escape);
165             }//if
166
esc = new Character JavaDoc(escape.charAt(0));
167         }//if
168

169         try {
170             result = RegexpFactory.create(pattern, esc);
171         }//try
172
catch (InvalidRegexpException err) {
173             throw new SelectorException("Invalid pattern: " + pattern);
174         }//catch
175
return result;
176     }//getRegexp
177
}//LikeExpression
178
Popular Tags