001: package org.josql.functions.regexp;
002:
003: import java.util.Map;
004: import java.util.HashMap;
005:
006: import org.josql.QueryExecutionException;
007:
008: import java.lang.reflect.Method;
009:
010: public class ApacheRegExpWrapper extends AbstractRegExpWrapper
011: implements RegExp {
012:
013: public static final String SUPPORTED_VERSION = "1.3";
014:
015: private final String compilerClassName = "org.apache.regexp.RECompiler";
016: private final String matcherClassName = "org.apache.regexp.RE";
017: private final String programClassName = "org.apache.regexp.REProgram";
018: private final String compileMethName = "compile";
019: private final String setProgramMethName = "setProgram";
020: private final String matchMethName = "match";
021:
022: private Method compileMeth = null;
023: private Method setProgramMeth = null;
024: private Method matchMeth = null;
025: private Class compilerClass = null;
026: private Class matcherClass = null;
027:
028: private Map patterns = new HashMap();
029:
030: public ApacheRegExpWrapper() {
031:
032: }
033:
034: public String getSupportedVersion() {
035:
036: return ApacheRegExpWrapper.SUPPORTED_VERSION;
037:
038: }
039:
040: public boolean isAvailable() {
041:
042: try {
043:
044: Class.forName(this .compilerClassName);
045:
046: return true;
047:
048: } catch (Exception e) {
049:
050: return false;
051:
052: }
053:
054: }
055:
056: public void init() throws QueryExecutionException {
057:
058: try {
059:
060: this .compilerClass = Class.forName(this .compilerClassName);
061:
062: Class argTypes[] = { String.class };
063:
064: this .compileMeth = this .compilerClass.getMethod(
065: this .compileMethName, argTypes);
066:
067: this .matcherClass = Class.forName(this .matcherClassName);
068:
069: Class pc = Class.forName(this .programClassName);
070:
071: Class argTypes2[] = { pc };
072:
073: this .setProgramMeth = this .matcherClass.getMethod(
074: this .setProgramMethName, argTypes2);
075:
076: this .matchMeth = this .matcherClass.getMethod(
077: this .matchMethName, argTypes);
078:
079: } catch (Exception e) {
080:
081: throw new QueryExecutionException("Unable to init", e);
082:
083: }
084:
085: }
086:
087: public boolean match(String pattern, String val)
088: throws QueryExecutionException {
089:
090: try {
091:
092: // See if we already have the pattern.
093: Object o = this .patterns.get(pattern);
094:
095: if (o == null) {
096:
097: // Create a new compiler.
098: Object c = this .compilerClass.newInstance();
099:
100: Object args[] = { pattern };
101:
102: // Compile the pattern.
103: Object program = this .compileMeth.invoke(c, args);
104:
105: // Create a new RE.
106: Object re = this .matcherClass.newInstance();
107:
108: Object args2[] = { program };
109:
110: // Apply the program.
111: this .setProgramMeth.invoke(re, args2);
112:
113: this .patterns.put(pattern, re);
114:
115: o = re;
116:
117: }
118:
119: Object args[] = { val };
120:
121: // Now try and match the value.
122: return ((Boolean) this .matchMeth.invoke(o, args))
123: .booleanValue();
124:
125: } catch (Exception e) {
126:
127: throw new QueryExecutionException("Unable to match value: "
128: + val + " against pattern: " + pattern, e);
129:
130: }
131:
132: }
133:
134: }
|