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 GNURegExpWrapper extends AbstractRegExpWrapper implements RegExp 34 { 35 36 public static final String SUPPORTED_VERSION = "1.1.4"; 37 38 private final String reClassName = "gnu.regexp.RE"; 39 private final String isMatchMethName = "isMatch"; 40 41 private Map patterns = new HashMap (); 42 43 private Constructor cons = null; 44 private Method isMatchMeth = null; 45 46 public GNURegExpWrapper () 47 { 48 49 } 50 51 public String getSupportedVersion () 52 { 53 54 return GNURegExpWrapper.SUPPORTED_VERSION; 55 56 } 57 58 public boolean isAvailable () 59 { 60 61 try 62 { 63 64 Class.forName (this.reClassName); 65 66 return true; 67 68 } catch (Exception e) { 69 70 return false; 71 72 } 73 74 } 75 76 public boolean match (String pattern, 77 String val) 78 throws QueryExecutionException 79 { 80 81 try 82 { 83 84 Object o = this.patterns.get (pattern); 85 86 if (o == null) 87 { 88 89 Object args[] = {pattern}; 90 91 o = this.cons.newInstance (args); 92 93 this.patterns.put (pattern, 94 o); 95 96 } 97 98 Object args[] = {val}; 99 100 return ((Boolean ) this.isMatchMeth.invoke (o, 101 args)).booleanValue (); 102 103 } catch (Exception e) { 104 105 throw new QueryExecutionException ("Unable to match value: " + 106 val + 107 " against pattern: " + 108 pattern, 109 e); 110 111 } 112 113 } 114 115 public void init () 116 throws QueryExecutionException 117 { 118 119 try 120 { 121 122 Class reClass = Class.forName (reClassName); 124 125 Class argTypes[] = {Object .class}; 126 127 this.cons = reClass.getConstructor (argTypes); 128 129 this.isMatchMeth = reClass.getMethod (this.isMatchMethName, 130 argTypes); 131 132 } catch (Exception e) { 133 134 throw new QueryExecutionException ("Unable to init", 135 e); 136 137 } 138 139 } 140 141 } 142 | Popular Tags |