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 24 32 public class StandardJavaRegExpWrapper extends AbstractRegExpWrapper implements RegExp 33 { 34 35 public static final String SUPPORTED_VERSION = "1.4"; 36 37 private final String compileMethName = "compile"; 38 private final String matcherMethName = "matcher"; 39 private final String matchesMethName = "matches"; 40 private final String matcherClassName = "java.util.regex.Matcher"; 41 private final String patternClassName = "java.util.regex.Pattern"; 42 43 private Method compileMeth = null; 44 private Method matcherMeth = null; 45 private Method matchesMeth = null; 46 47 private Map patterns = new HashMap (); 48 49 public StandardJavaRegExpWrapper () 50 { 51 52 } 53 54 public String getSupportedVersion () 55 { 56 57 return StandardJavaRegExpWrapper.SUPPORTED_VERSION; 58 59 } 60 61 public boolean isAvailable () 62 { 63 64 try 65 { 66 67 Class.forName (this.patternClassName); 68 69 return true; 70 71 } catch (Exception e) { 72 73 return false; 74 75 } 76 77 } 78 79 public boolean match (String pattern, 80 String val) 81 throws QueryExecutionException 82 { 83 84 try 85 { 86 87 Object o = this.patterns.get (pattern); 89 90 if (o == null) 91 { 92 93 Object args[] = {pattern}; 94 95 o = this.compileMeth.invoke (null, 97 args); 98 99 this.patterns.put (pattern, 100 o); 101 102 } 103 104 Object args[] = {val}; 105 106 Object matcher = this.matcherMeth.invoke (o, 108 args); 109 110 return ((Boolean ) this.matchesMeth.invoke (matcher, 112 null)).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 pc = Class.forName (this.patternClassName); 134 135 Class argTypes[] = {String .class}; 136 137 this.compileMeth = pc.getMethod (this.compileMethName, 139 argTypes); 140 141 Class argTypes2[] = {CharSequence .class}; 142 143 this.matcherMeth = pc.getMethod (this.matcherMethName, 145 argTypes2); 146 147 Class mc = Class.forName (this.matcherClassName); 148 149 this.matchesMeth = mc.getMethod (this.matchesMethName, 151 null); 152 153 } catch (Exception e) { 154 155 throw new QueryExecutionException ("Unable to init", 156 e); 157 158 } 159 160 } 161 162 } 163 | Popular Tags |