View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
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    * An implementation of the XercesRegExpEvaluator that uses the
57    * Regular Expression library in Xerces. For more information
58    * about the Xerces Regular Expression library please visit:
59    * <a href="
60  http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/utils/regex/RegularExpression.html
61  ">
62    * 
63  http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/utils/regex/RegularExpression.html
64  </a>
65    *
66    * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
67    * @author <a href="mailto:tora@debian.org">Takashi Okamoto</a>
68    * @version $Revision$ $Date: 2006-01-16 13:22:58 -0700 (Mon, 16 Jan 2006) $
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       * The Regular expression
83      **/
84      // RegularExpression _regexp = null;
85      Object _regexp = null;
86  
87      private Constructor<?> _constructor;
88      
89      /**
90       * Name of the actual class used for regular expression matching.
91       */
92      private String className;
93  
94      /**
95       * Creates a new XercesRegExpEvaluator
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     } //-- XercesRegExpEvaluator
129     
130     /**
131      * Sets the regular expression to match against during
132      * a call to #matches
133      *
134      * @param rexpr the regular expression
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     } //-- setExpression
153     
154     /**
155      * Returns true if the given String is matched by the 
156      * regular expression of this RegExpEvaluator
157      *
158      * @param value the String to check the production of
159      * @return true if the given string matches the regular
160      * expression of this RegExpEvaluator
161      * @see #setExpression
162     **/
163     public boolean matches(String value)
164     {
165         if (_regexp != null) {
166             // return _regexp.matches(value); 
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     } //-- matches
185     
186  } //-- XercesRegExpEvaluator