1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.exolab.castor.util;
37
38 import java.lang.reflect.Constructor;
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45
46
47
48
49
50
51
52
53
54
55
56 public class XercesRegExpEvaluator implements RegExpEvaluator {
57 private static final Log LOG = LogFactory.getLog(XercesRegExpEvaluator.class);
58
59 private static final String BOL = "^";
60 private static final String EOL = "$";
61
62 private static final String CLASS_NAME = "org.apache.xerces.impl.xpath.regex.RegularExpression";
63 private static final String OLD_CLASS_NAME = "org.apache.xerces.utils.regex.RegularExpression";
64
65
66
67
68
69 Object _regexp = null;
70
71 private Constructor<?> _constructor;
72
73
74
75
76 private String className;
77
78
79
80
81 public XercesRegExpEvaluator() {
82 super();
83
84 Class<?> regexpClass = null;
85 try {
86 regexpClass = Class.forName(CLASS_NAME);
87 _constructor = regexpClass.getConstructor(new Class[] {String.class});
88 className = CLASS_NAME;
89 } catch (ClassNotFoundException e) {
90 try {
91 regexpClass = Class.forName(OLD_CLASS_NAME);
92 _constructor = regexpClass.getConstructor(new Class[] {String.class});
93 className = OLD_CLASS_NAME;
94 } catch (ClassNotFoundException e2) {
95 LOG.error("Problem loading class " + this.className, e2);
96 throw new IllegalAccessError(
97 "Problem loading class " + this.className + ": " + e.getMessage());
98 } catch (SecurityException e2) {
99 LOG.error("Problem accessing constructor of class " + this.className, e2);
100 throw new IllegalAccessError(
101 "Problem accessing constructor of class " + this.className + ": " + e.getMessage());
102 } catch (NoSuchMethodException e2) {
103 LOG.error("Problem locating constructor of class " + this.className, e2);
104 throw new IllegalAccessError("class " + this.className + ": " + e.getMessage());
105 }
106 } catch (SecurityException e) {
107 LOG.error("Problem accessing constructor of class " + this.className, e);
108 throw new IllegalAccessError(
109 "Problem accessing constructor of class " + this.className + ": " + e.getMessage());
110 } catch (NoSuchMethodException e) {
111 LOG.error("Problem locating constructor of class " + this.className, e);
112 throw new IllegalAccessError("class " + this.className + ": " + e.getMessage());
113 }
114
115 }
116
117
118
119
120
121
122 public void setExpression(String rexpr) {
123
124 if (rexpr != null) {
125 try {
126 _regexp = _constructor.newInstance(new Object[] {BOL + rexpr + EOL});
127 } catch (Exception e) {
128 LOG.error("Problem invoking constructor on " + this.className, e);
129 String err = "XercesRegExp Syntax error: " + e.getMessage()
130 + " ; error occured with the following " + "regular expression: " + rexpr;
131 throw new IllegalArgumentException(err);
132 }
133 } else {
134 _regexp = null;
135 }
136 }
137
138
139
140
141
142
143
144
145 public boolean matches(String value) {
146 if (_regexp != null) {
147
148 Method method;
149 try {
150 method = _regexp.getClass().getMethod("matches", new Class[] {String.class});
151 return ((Boolean) method.invoke(_regexp, new Object[] {value})).booleanValue();
152 } catch (SecurityException e) {
153 LOG.error("Security problem accessing matches(String) method of class " + this.className,
154 e);
155 } catch (NoSuchMethodException e) {
156 LOG.error("Method matches(String) of class " + this.className + " could not be found.", e);
157 } catch (IllegalArgumentException e) {
158 LOG.error("Invalid argument provided to method matches(String) of class " + this.className,
159 e);
160 } catch (IllegalAccessException e) {
161 LOG.error("Illegal acces to method matches(String) of class " + this.className, e);
162 } catch (InvocationTargetException e) {
163 LOG.error("Invalid invocation of method matches(String) of class " + this.className, e);
164 }
165 }
166 return true;
167 }
168
169 }