1 15 package org.josql.functions.regexp; 16 17 import java.util.Map ; 18 import java.util.HashMap ; 19 20 import org.josql.QueryExecutionException; 21 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Constructor ; 24 25 33 public class OroApacheRegExpWrapper extends AbstractRegExpWrapper implements RegExp 34 { 35 36 public static final String SUPPORTED_VERSION = "2.0.8"; 37 38 private final String compilerClassName = "org.apache.oro.text.regex.Perl5Compiler"; 39 private final String matcherClassName = "org.apache.oro.text.regex.Perl5Matcher"; 40 private final String patternClassName = "org.apache.oro.text.regex.Pattern"; 41 42 private final String matchesMethName = "matches"; 43 private final String compileMethName = "compile"; 44 45 private Method compileMeth = null; 46 private Method matchesMeth = null; 47 48 private Object compiler = null; 49 private Object matcher = null; 50 51 private Map patterns = new HashMap (); 52 53 public OroApacheRegExpWrapper () 54 { 55 56 } 57 58 public String getSupportedVersion () 59 { 60 61 return OroApacheRegExpWrapper.SUPPORTED_VERSION; 62 63 } 64 65 public boolean isAvailable () 66 { 67 68 try 69 { 70 71 Class.forName (this.patternClassName); 72 73 return true; 74 75 } catch (Exception e) { 76 77 return false; 78 79 } 80 81 } 82 83 public boolean match (String pattern, 84 String val) 85 throws QueryExecutionException 86 { 87 88 try 89 { 90 91 Object o = this.patterns.get (pattern); 93 94 if (o == null) 95 { 96 97 Object args[] = {pattern}; 98 99 o = this.compileMeth.invoke (this.compiler, 101 args); 102 103 this.patterns.put (pattern, 104 o); 105 106 } 107 108 Object args[] = {val, o}; 109 110 return ((Boolean ) this.matchesMeth.invoke (this.matcher, 112 args)).booleanValue (); 113 114 } catch (Exception e) { 115 116 throw new QueryExecutionException ("Unable to match value: " + 117 val + 118 " against pattern: " + 119 pattern, 120 e); 121 122 } 123 124 } 125 126 public void init () 127 throws QueryExecutionException 128 { 129 130 try 131 { 132 133 Class compClass = Class.forName (this.compilerClassName); 134 135 this.compiler = compClass.newInstance (); 137 138 Class argTypes[] = {String .class}; 140 141 this.compileMeth = compClass.getMethod (this.compileMethName, 142 argTypes); 143 144 Class matchClass = Class.forName (this.matcherClassName); 145 146 this.matcher = matchClass.newInstance (); 148 149 Class patternClass = Class.forName (this.patternClassName); 150 151 Class argTypes2[] = {String .class, patternClass}; 152 153 this.matchesMeth = matchClass.getMethod (this.matchesMethName, 155 argTypes2); 156 157 } catch (Exception e) { 158 159 throw new QueryExecutionException ("Unable to init", 160 e); 161 162 } 163 164 } 165 166 } 167 | Popular Tags |