001: /*
002: * $Id: LikeToRegexpFunction.java,v 1.15 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functions;
042:
043: import org.axiondb.AxionException;
044: import org.axiondb.DataType;
045: import org.axiondb.FunctionFactory;
046: import org.axiondb.RowDecorator;
047: import org.axiondb.types.StringType;
048:
049: /**
050: * @version $Revision: 1.15 $ $Date: 2005/12/20 18:32:28 $
051: * @author Chuck Burdick
052: * @author Ritesh Adval
053: */
054: public class LikeToRegexpFunction extends BaseRegExpFunction implements
055: ScalarFunction, FunctionFactory {
056:
057: public LikeToRegexpFunction() {
058: super ("LIKE_TO_REGEXP");
059: }
060:
061: public ConcreteFunction makeNewInstance() {
062: return new LikeToRegexpFunction();
063: }
064:
065: public DataType getDataType() {
066: return STRING_TYPE;
067: }
068:
069: public Object evaluate(RowDecorator row) throws AxionException {
070: Object arg = getArgument(0).evaluate(row);
071: String likePattern = (String) STRING_TYPE.convert(arg);
072: if (likePattern == null) {
073: return null;
074: }
075:
076: char escChar = 0;
077: if (getArgumentCount() == 2) {
078: Object arg1 = getArgument(1).evaluate(row);
079: String origEscChar = (String) STRING_TYPE.convert(arg1);
080: if (origEscChar != null) {
081: if (origEscChar.length() != 1) {
082: throw new AxionException(22019);
083: }
084: escChar = origEscChar.charAt(0);
085: } else {
086: return null;
087: }
088: }
089:
090: String processedLikePattern = (String) getFromCache(likePattern
091: + escChar);
092:
093: if (processedLikePattern == null) {
094: processedLikePattern = convertLike(likePattern, escChar);
095: ;
096: putInCache(likePattern + escChar, processedLikePattern);
097: }
098: return processedLikePattern;
099: }
100:
101: protected String convertLike(String orig) throws AxionException {
102: char ch = 0;
103: return convertLike(orig, ch);
104: }
105:
106: // Per ANSI 92, 99, 2003 specs:
107: // If escape character is specified then check if we have escape char not followed by
108: // escape character itself or '%' or '_' ; if true then throw exception
109: protected String convertLike(String orig, char skipChar)
110: throws AxionException {
111: StringBuffer _buf = new StringBuffer(2 * orig.length());
112:
113: if (orig.length() == 0 || orig.charAt(0) != ESCAPE_PERCENTAGE) {
114: _buf.append("^");
115: }
116:
117: for (int i = 0, I = orig.length(); i < I; i++) {
118: char next = orig.charAt(i);
119: if (next == skipChar) {
120: int j = i + 1;
121: if (j < orig.length()) {
122: // what is the next next char
123: char nextNext = orig.charAt(j);
124:
125: // if next character is a valid SQL escape character then write out the escaped
126: // character.
127: if (isValidSQLEscapedCharacter(nextNext)) {
128: _buf.append(nextNext);
129: i++;
130: // if next character is escape character then write out the escape character
131: } else if (nextNext == skipChar) {
132: i++;
133: _buf.append(escapeCharForRegex(nextNext));
134: } else {
135: // next character is neither a valid SQL escape character nor a
136: // escape character, this is an error condition
137: throw new AxionException(
138: "character following the escape character '"
139: + skipChar
140: + "' should be either a valid SQL escape character or the escape character itself; problem at "
141: + orig.substring(i), 22025);
142: }
143: continue;
144: } else {
145: // Cannot have a single escape character at the end of a like pattern.
146: throw new AxionException(22025);
147: }
148: }
149:
150: String processed = escapeCharForRegex(next);
151: _buf.append(processed);
152: }
153:
154: if (orig.length() < 2
155: || (orig.charAt(orig.length() - 1) != ESCAPE_PERCENTAGE && orig
156: .charAt(orig.length() - 2) != '\\')) {
157: _buf.append("$");
158: }
159: return _buf.toString();
160: }
161:
162: private String escapeCharForRegex(char ch) {
163: String repl = null;
164: switch (ch) {
165: case '%':
166: repl = ".*";
167: break;
168: case '_':
169: repl = ".";
170: break;
171: case '?':
172: repl = "\\?";
173: break;
174: case '*':
175: repl = "\\*";
176: break;
177: case '.':
178: repl = "\\.";
179: break;
180: case '$':
181: repl = "\\$";
182: break;
183: case '\\':
184: repl = "\\\\";
185: break;
186: case '+':
187: repl = "\\+";
188: break;
189: case '^':
190: repl = "\\^";
191: break;
192: case '{':
193: repl = "\\{";
194: break;
195: case '}':
196: repl = "\\}";
197: break;
198: case '[':
199: repl = "\\[";
200: break;
201: case ']':
202: repl = "\\]";
203: break;
204: case '(':
205: repl = "\\(";
206: break;
207: case ')':
208: repl = "\\)";
209: break;
210: case '|':
211: repl = "\\|";
212: break;
213: default:
214: repl = String.valueOf(ch);
215: }
216:
217: return repl;
218: }
219:
220: private boolean isValidSQLEscapedCharacter(char ch) {
221: boolean result = false;
222: if (ch == ESCAPE_PERCENTAGE || ch == ESCAPE_UNDERSCORE) {
223: result = true;
224: }
225: return result;
226: }
227:
228: public boolean isValid() {
229: return (getArgumentCount() == 1) || (getArgumentCount() == 2);
230: }
231:
232: private static final DataType STRING_TYPE = new StringType();
233:
234: private static char ESCAPE_PERCENTAGE = '%';
235: private static char ESCAPE_UNDERSCORE = '_';
236: }
|