001: package org.josql.functions.regexp;
002:
003: import java.util.Map;
004: import java.util.HashMap;
005:
006: import org.josql.QueryExecutionException;
007:
008: public class RegExpFactory {
009:
010: /**
011: * The instance name to use for the Java 1.4 (java.util.regex) regular expression library.
012: * Whilst this is the default it will not be available on version of Java < 1.4.
013: */
014: public static final String JAVA_INST = "java";
015:
016: /**
017: * The instance name to use for the ORO Apache regular expression library.
018: */
019: public static final String ORO_INST = "oro";
020:
021: /**
022: * The instance name to use for the GNU regular expression library.
023: */
024: public static final String GNU_INST = "gnu";
025:
026: /**
027: * The instance name to use for the Apache RegExp regular expression library.
028: */
029: public static final String APACHE_REGEXP_INST = "apache.regexp";
030:
031: private static String defInst = RegExpFactory.JAVA_INST;
032:
033: private static Map mappings = new HashMap();
034: private static Map versions = new HashMap();
035:
036: static {
037:
038: StandardJavaRegExpWrapper j = new StandardJavaRegExpWrapper();
039:
040: if (j.isAvailable()) {
041:
042: RegExpFactory.mappings.put(RegExpFactory.JAVA_INST,
043: StandardJavaRegExpWrapper.class);
044: RegExpFactory.versions.put(RegExpFactory.JAVA_INST, j
045: .getSupportedVersion());
046:
047: }
048:
049: OroApacheRegExpWrapper o = new OroApacheRegExpWrapper();
050:
051: if (o.isAvailable()) {
052:
053: RegExpFactory.mappings.put(RegExpFactory.ORO_INST,
054: OroApacheRegExpWrapper.class);
055: RegExpFactory.versions.put(RegExpFactory.ORO_INST, o
056: .getSupportedVersion());
057:
058: }
059:
060: GNURegExpWrapper g = new GNURegExpWrapper();
061:
062: if (g.isAvailable()) {
063:
064: RegExpFactory.mappings.put(RegExpFactory.GNU_INST,
065: GNURegExpWrapper.class);
066: RegExpFactory.versions.put(RegExpFactory.GNU_INST, g
067: .getSupportedVersion());
068:
069: }
070:
071: ApacheRegExpWrapper a = new ApacheRegExpWrapper();
072:
073: if (a.isAvailable()) {
074:
075: RegExpFactory.mappings.put(
076: RegExpFactory.APACHE_REGEXP_INST,
077: ApacheRegExpWrapper.class);
078: RegExpFactory.versions.put(
079: RegExpFactory.APACHE_REGEXP_INST, a
080: .getSupportedVersion());
081:
082: }
083:
084: }
085:
086: private RegExpFactory() {
087:
088: }
089:
090: public static String getSupportedVersion(String instName) {
091:
092: return (String) RegExpFactory.versions.get(instName);
093:
094: }
095:
096: public static String getDefaultInstanceName() {
097:
098: return RegExpFactory.defInst;
099:
100: }
101:
102: public static void addInstance(String name, RegExp re, boolean def) {
103:
104: RegExpFactory.mappings.put(name, re);
105:
106: if (def) {
107:
108: RegExpFactory.defInst = name;
109:
110: }
111:
112: }
113:
114: public static void setDefaultInstanceName(String n) {
115:
116: if (!RegExpFactory.mappings.containsKey(n)) {
117:
118: throw new IllegalArgumentException(
119: "No appropriate wrapper class found for instance name: "
120: + n);
121:
122: }
123:
124: RegExpFactory.defInst = n;
125:
126: }
127:
128: public static RegExp getDefaultInstance()
129: throws QueryExecutionException {
130:
131: return RegExpFactory.getInstance(RegExpFactory.defInst);
132:
133: }
134:
135: public static RegExp getInstance(String type)
136: throws QueryExecutionException {
137:
138: Object o = RegExpFactory.mappings.get(type);
139:
140: if (o == null) {
141:
142: return null;
143:
144: }
145:
146: if (o instanceof RegExp) {
147:
148: // Already inited...
149: return (RegExp) o;
150:
151: }
152:
153: Class c = (Class) o;
154:
155: try {
156:
157: RegExp re = (RegExp) c.newInstance();
158:
159: re.init();
160:
161: RegExpFactory.mappings.put(type, re);
162:
163: return re;
164:
165: } catch (Exception e) {
166:
167: throw new QueryExecutionException(
168: "Unable to init RegExp instance: " + c.getName(), e);
169:
170: }
171:
172: }
173:
174: }
|