001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.selectors;
023:
024: import java.util.regex.Pattern;
025:
026: /**
027: * Regular expressions to support the selector LIKE operator.
028: *
029: * @version <tt>$Revision: 57198 $</tt>
030: *
031: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
032: * @author droy@boostmyscore.com
033: * @author Scott.Stark@jboss.org
034: * @author Loren Rosen
035: */
036: public class RegExp {
037: protected Pattern re;
038:
039: public RegExp(String pattern, Character escapeChar)
040: throws Exception {
041: String pat = adjustPattern(pattern, escapeChar);
042: re = Pattern.compile(pat);
043: }
044:
045: public boolean isMatch(Object target) {
046: String str = target != null ? target.toString() : "";
047: return re.matcher(str).matches();
048: }
049:
050: protected String adjustPattern(String pattern, Character escapeChar)
051: throws Exception {
052:
053: // StringBuffer patternBuf = new StringBuffer( pattern );
054: int patternLen = pattern.length();
055: StringBuffer REpattern = new StringBuffer(patternLen + 10);
056: boolean useEscape = (escapeChar != null);
057: char escape = Character.UNASSIGNED;
058: if (useEscape) {
059: escape = escapeChar.charValue();
060: }
061:
062: REpattern.append('^');
063:
064: for (int i = 0; i < patternLen; i++) {
065: boolean escaped = false;
066: char c = pattern.charAt(i);
067:
068: if (useEscape && escape == c) {
069: i++;
070: if (i < patternLen) {
071: escaped = true;
072: c = pattern.charAt(i);
073: } else {
074: throw new Exception(
075: "LIKE ESCAPE: Bad use of escape character");
076: }
077: }
078:
079: // Match characters, or escape ones special to the underlying
080: // regex engine
081: switch (c) {
082: case '_':
083: if (escaped) {
084: REpattern.append(c);
085: } else {
086: REpattern.append('.');
087: }
088: break;
089: case '%':
090: if (escaped) {
091: REpattern.append(c);
092: } else {
093: REpattern.append(".*");
094: }
095: break;
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: case '|':
110: REpattern.append("\\");
111: REpattern.append(c);
112: break;
113: default:
114: REpattern.append(c);
115: break;
116: }
117: }
118:
119: REpattern.append('$');
120: return REpattern.toString();
121: }
122: }
|