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:
024: /**
025: * The wrapper implementation for the Apache ORO implementation of regular expression matching.
026: * See: <a href="http://jakarta.apache.org/oro/">http://jakarta.apache.org/oro/</a> for details.
027: */
028: public class OroApacheRegExpWrapper extends AbstractRegExpWrapper
029: implements RegExp {
030:
031: public static final String SUPPORTED_VERSION = "2.0.8";
032:
033: private final String compilerClassName = "org.apache.oro.text.regex.Perl5Compiler";
034: private final String matcherClassName = "org.apache.oro.text.regex.Perl5Matcher";
035: private final String patternClassName = "org.apache.oro.text.regex.Pattern";
036:
037: private final String matchesMethName = "matches";
038: private final String compileMethName = "compile";
039:
040: private Method compileMeth = null;
041: private Method matchesMeth = null;
042:
043: private Object compiler = null;
044: private Object matcher = null;
045:
046: private Map patterns = new HashMap();
047:
048: public OroApacheRegExpWrapper() {
049:
050: }
051:
052: public String getSupportedVersion() {
053:
054: return OroApacheRegExpWrapper.SUPPORTED_VERSION;
055:
056: }
057:
058: public boolean isAvailable() {
059:
060: try {
061:
062: Class.forName(this .patternClassName);
063:
064: return true;
065:
066: } catch (Exception e) {
067:
068: return false;
069:
070: }
071:
072: }
073:
074: public boolean match(String pattern, String val)
075: throws QueryExecutionException {
076:
077: try {
078:
079: // See if we already have the pattern.
080: Object o = this .patterns.get(pattern);
081:
082: if (o == null) {
083:
084: Object args[] = { pattern };
085:
086: // Create a new Pattern.
087: o = this .compileMeth.invoke(this .compiler, args);
088:
089: this .patterns.put(pattern, o);
090:
091: }
092:
093: Object args[] = { val, o };
094:
095: // Match them!
096: return ((Boolean) this .matchesMeth.invoke(this .matcher,
097: args)).booleanValue();
098:
099: } catch (Exception e) {
100:
101: throw new QueryExecutionException("Unable to match value: "
102: + val + " against pattern: " + pattern, e);
103:
104: }
105:
106: }
107:
108: public void init() throws QueryExecutionException {
109:
110: try {
111:
112: Class compClass = Class.forName(this .compilerClassName);
113:
114: // Create a new Compiler instance.
115: this .compiler = compClass.newInstance();
116:
117: // Get the compile(String) method.
118: Class argTypes[] = { String.class };
119:
120: this .compileMeth = compClass.getMethod(
121: this .compileMethName, argTypes);
122:
123: Class matchClass = Class.forName(this .matcherClassName);
124:
125: // Create a new matcher.
126: this .matcher = matchClass.newInstance();
127:
128: Class patternClass = Class.forName(this .patternClassName);
129:
130: Class argTypes2[] = { String.class, patternClass };
131:
132: // Get the matches(String,Pattern) method...
133: this .matchesMeth = matchClass.getMethod(
134: this .matchesMethName, argTypes2);
135:
136: } catch (Exception e) {
137:
138: throw new QueryExecutionException("Unable to init", e);
139:
140: }
141:
142: }
143:
144: }
|