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 public class RegExpFactory 9 { 10 11 15 public static final String JAVA_INST = "java"; 16 17 20 public static final String ORO_INST = "oro"; 21 22 25 public static final String GNU_INST = "gnu"; 26 27 30 public static final String APACHE_REGEXP_INST = "apache.regexp"; 31 32 private static String defInst = RegExpFactory.JAVA_INST; 33 34 private static Map mappings = new HashMap (); 35 private static Map versions = new HashMap (); 36 37 static 38 { 39 40 StandardJavaRegExpWrapper j = new StandardJavaRegExpWrapper (); 41 42 if (j.isAvailable ()) 43 { 44 45 RegExpFactory.mappings.put (RegExpFactory.JAVA_INST, 46 StandardJavaRegExpWrapper.class); 47 RegExpFactory.versions.put (RegExpFactory.JAVA_INST, 48 j.getSupportedVersion ()); 49 50 } 51 52 OroApacheRegExpWrapper o = new OroApacheRegExpWrapper (); 53 54 if (o.isAvailable ()) 55 { 56 57 RegExpFactory.mappings.put (RegExpFactory.ORO_INST, 58 OroApacheRegExpWrapper.class); 59 RegExpFactory.versions.put (RegExpFactory.ORO_INST, 60 o.getSupportedVersion ()); 61 62 } 63 64 GNURegExpWrapper g = new GNURegExpWrapper (); 65 66 if (g.isAvailable ()) 67 { 68 69 RegExpFactory.mappings.put (RegExpFactory.GNU_INST, 70 GNURegExpWrapper.class); 71 RegExpFactory.versions.put (RegExpFactory.GNU_INST, 72 g.getSupportedVersion ()); 73 74 } 75 76 ApacheRegExpWrapper a = new ApacheRegExpWrapper (); 77 78 if (a.isAvailable ()) 79 { 80 81 RegExpFactory.mappings.put (RegExpFactory.APACHE_REGEXP_INST, 82 ApacheRegExpWrapper.class); 83 RegExpFactory.versions.put (RegExpFactory.APACHE_REGEXP_INST, 84 a.getSupportedVersion ()); 85 86 } 87 88 } 89 90 private RegExpFactory () 91 { 92 93 } 94 95 public static String getSupportedVersion (String instName) 96 { 97 98 return (String ) RegExpFactory.versions.get (instName); 99 100 } 101 102 public static String getDefaultInstanceName () 103 { 104 105 return RegExpFactory.defInst; 106 107 } 108 109 public static void addInstance (String name, 110 RegExp re, 111 boolean def) 112 { 113 114 RegExpFactory.mappings.put (name, 115 re); 116 117 if (def) 118 { 119 120 RegExpFactory.defInst = name; 121 122 } 123 124 } 125 126 public static void setDefaultInstanceName (String n) 127 { 128 129 if (!RegExpFactory.mappings.containsKey (n)) 130 { 131 132 throw new IllegalArgumentException ("No appropriate wrapper class found for instance name: " + 133 n); 134 135 } 136 137 RegExpFactory.defInst = n; 138 139 } 140 141 public static RegExp getDefaultInstance () 142 throws QueryExecutionException 143 { 144 145 return RegExpFactory.getInstance (RegExpFactory.defInst); 146 147 } 148 149 public static RegExp getInstance (String type) 150 throws QueryExecutionException 151 { 152 153 Object o = RegExpFactory.mappings.get (type); 154 155 if (o == null) 156 { 157 158 return null; 159 160 } 161 162 if (o instanceof RegExp) 163 { 164 165 return (RegExp) o; 167 168 } 169 170 Class c = (Class ) o; 171 172 try 173 { 174 175 Object oo = c.newInstance (); 176 177 RegExp re = (RegExp) c.newInstance (); 178 179 re.init (); 180 181 RegExpFactory.mappings.put (type, 182 re); 183 184 return re; 185 186 } catch (Exception e) { 187 188 throw new QueryExecutionException ("Unable to init RegExp instance: " + 189 c.getName (), 190 e); 191 192 } 193 194 } 195 196 } 197 | Popular Tags |