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 Java 1.4 regular expression matching (java.util.regex).
026: * See: {@link java.util.regex}.
027: */
028: public class StandardJavaRegExpWrapper extends AbstractRegExpWrapper
029: implements RegExp {
030:
031: public static final String SUPPORTED_VERSION = "1.4";
032:
033: private final String compileMethName = "compile";
034: private final String matcherMethName = "matcher";
035: private final String matchesMethName = "matches";
036: private final String matcherClassName = "java.util.regex.Matcher";
037: private final String patternClassName = "java.util.regex.Pattern";
038:
039: private Method compileMeth = null;
040: private Method matcherMeth = null;
041: private Method matchesMeth = null;
042:
043: private Map patterns = new HashMap();
044:
045: public StandardJavaRegExpWrapper() {
046:
047: }
048:
049: public String getSupportedVersion() {
050:
051: return StandardJavaRegExpWrapper.SUPPORTED_VERSION;
052:
053: }
054:
055: public boolean isAvailable() {
056:
057: try {
058:
059: Class.forName(this .patternClassName);
060:
061: return true;
062:
063: } catch (Exception e) {
064:
065: return false;
066:
067: }
068:
069: }
070:
071: public boolean match(String pattern, String val)
072: throws QueryExecutionException {
073:
074: try {
075:
076: // See if we already have a Pattern for this pattern.
077: Object o = this .patterns.get(pattern);
078:
079: if (o == null) {
080:
081: Object args[] = { pattern };
082:
083: // Create a new one. Static method.
084: o = this .compileMeth.invoke(null, args);
085:
086: this .patterns.put(pattern, o);
087:
088: }
089:
090: Object args[] = { val };
091:
092: // Now create the matcher.
093: Object matcher = this .matcherMeth.invoke(o, args);
094:
095: // Now invoke the "matches" method.
096: return ((Boolean) this .matchesMeth.invoke(matcher, null))
097: .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 pc = Class.forName(this .patternClassName);
113:
114: Class argTypes[] = { String.class };
115:
116: // Get the "compile" method.
117: this .compileMeth = pc.getMethod(this .compileMethName,
118: argTypes);
119:
120: Class argTypes2[] = { CharSequence.class };
121:
122: // Get the "matcher" method.
123: this .matcherMeth = pc.getMethod(this .matcherMethName,
124: argTypes2);
125:
126: Class mc = Class.forName(this .matcherClassName);
127:
128: // Get the matches method.
129: this .matchesMeth = mc.getMethod(this .matchesMethName, null);
130:
131: } catch (Exception e) {
132:
133: throw new QueryExecutionException("Unable to init", e);
134:
135: }
136:
137: }
138:
139: }
|