View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
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   * An implementation of the XercesRegExpEvaluator that uses the Regular Expression library in
47   * Xerces. For more information about the Xerces Regular Expression library please visit: <a href="
48   * http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/utils/regex/RegularExpression.html ">
49   * 
50   * http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/utils/regex/RegularExpression.html </a>
51   *
52   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
53   * @author <a href="mailto:tora@debian.org">Takashi Okamoto</a>
54   * @version $Revision$ $Date: 2006-01-16 13:22:58 -0700 (Mon, 16 Jan 2006) $
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     * The Regular expression
67     **/
68    // RegularExpression _regexp = null;
69    Object _regexp = null;
70  
71    private Constructor<?> _constructor;
72  
73    /**
74     * Name of the actual class used for regular expression matching.
75     */
76    private String className;
77  
78    /**
79     * Creates a new XercesRegExpEvaluator
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   } // -- XercesRegExpEvaluator
116 
117   /**
118    * Sets the regular expression to match against during a call to #matches
119    *
120    * @param rexpr the regular expression
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   } // -- setExpression
137 
138   /**
139    * Returns true if the given String is matched by the regular expression of this RegExpEvaluator
140    *
141    * @param value the String to check the production of
142    * @return true if the given string matches the regular expression of this RegExpEvaluator
143    * @see #setExpression
144    **/
145   public boolean matches(String value) {
146     if (_regexp != null) {
147       // return _regexp.matches(value);
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   } // -- matches
168 
169 } // -- XercesRegExpEvaluator