001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.functions.regexp;
016:
017: import java.util.Map;
018: import java.util.HashMap;
019:
020: import org.josql.QueryExecutionException;
021:
022: import java.lang.reflect.Method;
023: import java.lang.reflect.Constructor;
024:
025: /**
026: * The wrapper implementation for the GNU implementation of regular expression matching.
027: * See: <a href="http://www.cacas.org/java/gnu/regexp/">http://www.cacas.org/java/gnu/regexp/</a> for details.
028: */
029: public class GNURegExpWrapper extends AbstractRegExpWrapper implements
030: RegExp {
031:
032: public static final String SUPPORTED_VERSION = "1.1.4";
033:
034: private final String reClassName = "gnu.regexp.RE";
035: private final String isMatchMethName = "isMatch";
036:
037: private Map patterns = new HashMap();
038:
039: private Constructor cons = null;
040: private Method isMatchMeth = null;
041:
042: public GNURegExpWrapper() {
043:
044: }
045:
046: public String getSupportedVersion() {
047:
048: return GNURegExpWrapper.SUPPORTED_VERSION;
049:
050: }
051:
052: public boolean isAvailable() {
053:
054: try {
055:
056: Class.forName(this .reClassName);
057:
058: return true;
059:
060: } catch (Exception e) {
061:
062: return false;
063:
064: }
065:
066: }
067:
068: public boolean match(String pattern, String val)
069: throws QueryExecutionException {
070:
071: try {
072:
073: Object o = this .patterns.get(pattern);
074:
075: if (o == null) {
076:
077: Object args[] = { pattern };
078:
079: o = this .cons.newInstance(args);
080:
081: this .patterns.put(pattern, o);
082:
083: }
084:
085: Object args[] = { val };
086:
087: return ((Boolean) this .isMatchMeth.invoke(o, args))
088: .booleanValue();
089:
090: } catch (Exception e) {
091:
092: throw new QueryExecutionException("Unable to match value: "
093: + val + " against pattern: " + pattern, e);
094:
095: }
096:
097: }
098:
099: public void init() throws QueryExecutionException {
100:
101: try {
102:
103: // Bit easier this one!
104: Class reClass = Class.forName(reClassName);
105:
106: Class argTypes[] = { Object.class };
107:
108: this .cons = reClass.getConstructor(argTypes);
109:
110: this .isMatchMeth = reClass.getMethod(this .isMatchMethName,
111: argTypes);
112:
113: } catch (Exception e) {
114:
115: throw new QueryExecutionException("Unable to init", e);
116:
117: }
118:
119: }
120:
121: }
|