KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > selectors > RegExp


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.selectors;
23
24 import java.util.regex.Pattern JavaDoc;
25
26 /**
27  * Regular expressions to support the selector LIKE operator.
28  *
29  * @version <tt>$Revision: 42933 $</tt>
30  *
31  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
32  * @author droy@boostmyscore.com
33  * @author Scott.Stark@jboss.org
34  * @author Loren Rosen
35  */

36 public class RegExp
37 {
38    protected Pattern JavaDoc re;
39          
40    public RegExp (String JavaDoc pattern, Character JavaDoc escapeChar)
41       throws Exception JavaDoc
42    {
43       String JavaDoc pat = adjustPattern(pattern, escapeChar);
44       re = Pattern.compile(pat);
45    }
46     
47    public boolean isMatch (Object JavaDoc target)
48    {
49       String JavaDoc str = target != null ? target.toString() : "";
50       return re.matcher(str).matches();
51    }
52     
53    protected String JavaDoc adjustPattern (String JavaDoc pattern, Character JavaDoc escapeChar)
54       throws Exception JavaDoc
55    {
56       
57       // StringBuffer patternBuf = new StringBuffer( pattern );
58
int patternLen = pattern.length();
59       StringBuffer JavaDoc REpattern = new StringBuffer JavaDoc(patternLen + 10);
60       boolean useEscape = (escapeChar != null);
61       char escape = Character.UNASSIGNED;
62       if (useEscape) {
63          escape = escapeChar.charValue();
64       }
65       
66       REpattern.append ('^');
67
68       for ( int i = 0; i < patternLen; i++ ) {
69          boolean escaped = false;
70          char c = pattern.charAt( i );
71
72          if ( useEscape && escape == c ) {
73             i++;
74             if ( i < patternLen ) {
75                escaped = true;
76                c = pattern.charAt( i );
77             } else {
78                throw new Exception JavaDoc( "LIKE ESCAPE: Bad use of escape character" );
79             }
80          }
81
82          // Match characters, or escape ones special to the underlying
83
// regex engine
84
switch ( c ) {
85          case '_':
86             if ( escaped ) {
87                REpattern.append( c );
88             } else {
89                REpattern.append( '.' );
90             }
91             break;
92          case '%':
93             if ( escaped ) {
94                REpattern.append( c );
95             } else {
96                REpattern.append( ".*" );
97             }
98             break;
99          case '*':
100          case '.':
101          case '\\':
102          case '^':
103          case '$':
104          case '[':
105          case ']':
106          case '(':
107          case ')':
108          case '+':
109          case '?':
110          case '{':
111          case '}':
112          case '|':
113             REpattern.append( "\\");
114             REpattern.append ( c );
115             break;
116          default:
117             REpattern.append( c );
118             break;
119          }
120       }
121
122       REpattern.append( '$' );
123       return REpattern.toString();
124    }
125 }
126  
127
Popular Tags