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