1 package org.josql.functions.regexp; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 6 import org.josql.QueryExecutionException; 7 8 import java.lang.reflect.Method ; 9 10 public class ApacheRegExpWrapper extends AbstractRegExpWrapper implements RegExp 11 { 12 13 public static final String SUPPORTED_VERSION = "1.3"; 14 15 private final String compilerClassName = "org.apache.regexp.RECompiler"; 16 private final String matcherClassName = "org.apache.regexp.RE"; 17 private final String programClassName = "org.apache.regexp.REProgram"; 18 private final String compileMethName = "compile"; 19 private final String setProgramMethName = "setProgram"; 20 private final String matchMethName = "match"; 21 22 private Method compileMeth = null; 23 private Method setProgramMeth = null; 24 private Method matchMeth = null; 25 private Class compilerClass = null; 26 private Class matcherClass = null; 27 28 private Map patterns = new HashMap (); 29 30 public ApacheRegExpWrapper () 31 { 32 33 } 34 35 public String getSupportedVersion () 36 { 37 38 return ApacheRegExpWrapper.SUPPORTED_VERSION; 39 40 } 41 42 public boolean isAvailable () 43 { 44 45 try 46 { 47 48 Class.forName (this.compilerClassName); 49 50 return true; 51 52 } catch (Exception e) { 53 54 return false; 55 56 } 57 58 } 59 60 public void init () 61 throws QueryExecutionException 62 { 63 64 try 65 { 66 67 this.compilerClass = Class.forName (this.compilerClassName); 68 69 Class argTypes[] = {String .class}; 70 71 this.compileMeth = this.compilerClass.getMethod (this.compileMethName, 72 argTypes); 73 74 this.matcherClass = Class.forName (this.matcherClassName); 75 76 Class pc = Class.forName (this.programClassName); 77 78 Class argTypes2[] = {pc}; 79 80 this.setProgramMeth = this.matcherClass.getMethod (this.setProgramMethName, 81 argTypes2); 82 83 this.matchMeth = this.matcherClass.getMethod (this.matchMethName, 84 argTypes); 85 86 } catch (Exception e) { 87 88 throw new QueryExecutionException ("Unable to init", 89 e); 90 91 } 92 93 } 94 95 public boolean match (String pattern, 96 String val) 97 throws QueryExecutionException 98 { 99 100 try 101 { 102 103 Object o = this.patterns.get (pattern); 105 106 if (o == null) 107 { 108 109 Object c = this.compilerClass.newInstance (); 111 112 Object args[] = {pattern}; 113 114 Object program = this.compileMeth.invoke (c, 116 args); 117 118 Object re = this.matcherClass.newInstance (); 120 121 Object args2[] = {program}; 122 123 this.setProgramMeth.invoke (re, 125 args2); 126 127 this.patterns.put (pattern, 128 re); 129 130 o = re; 131 132 } 133 134 Object args[] = {val}; 135 136 return ((Boolean ) this.matchMeth.invoke (o, 138 args)).booleanValue (); 139 140 } catch (Exception e) { 141 142 throw new QueryExecutionException ("Unable to match value: " + 143 val + 144 " against pattern: " + 145 pattern, 146 e); 147 148 } 149 150 } 151 152 } 153 | Popular Tags |