View Javadoc
1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   *
12   * Redistribution and use in source and binary forms, with or without
13   * modification, are permitted provided that the following conditions are
14   * met:
15   * 
16   *   * Redistributions of source code must retain the above copyright
17   *     notice, this list of conditions and the following disclaimer.
18   * 
19   *   * Redistributions in binary form must reproduce the above copyright
20   *     notice, this list of conditions and the following disclaimer in the
21   *     documentation and/or other materials provided with the distribution.
22   * 
23   *   * Neither the name of the Jaxen Project nor the names of its
24   *     contributors may be used to endorse or promote products derived 
25   *     from this software without specific prior written permission.
26   * 
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
31   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38   *
39   * ====================================================================
40   * This software consists of voluntary contributions made by many
41   * individuals on behalf of the Jaxen Project and was originally
42   * created by bob mcwhirter <bob@werken.com> and
43   * James Strachan <jstrachan@apache.org>.  For more information on the
44   * Jaxen Project, please see <http://www.jaxen.org/>.
45   *
46   * $Id$
47   */
48  
49  package org.jaxen.saxpath.helpers;
50  
51  import org.jaxen.saxpath.SAXPathException;
52  import org.jaxen.saxpath.XPathReader;
53  
54  /** Create an {@link org.jaxen.saxpath.XPathReader} from
55   *  either a system property, or a named class.
56   *
57   *  <p>
58   *  Similar to the SAX API, the <code>XPathReaderFactory</code>
59   *  can create an <code>XPathReader</code> from a name of a
60   *  class passed in directly, or by inspecting the system
61   *  property <code>org.saxpath.driver</code>.
62   *
63   *  @author bob mcwhirter (bob@werken.com)
64   */
65  public class XPathReaderFactory
66  {
67      /** The <code>org.saxpath.driver</code> property name. */
68      public static final String DRIVER_PROPERTY = "org.saxpath.driver";
69  
70      /** The default driver to use if none is configured. */
71      protected static final String DEFAULT_DRIVER = "org.jaxen.saxpath.base.XPathReader";
72      
73      private XPathReaderFactory() {}
74      
75      
76      /** Create an <code>XPathReader</code> using the value of
77       *  the <code>org.saxpath.driver</code> system property.
78       *
79       *  @return an instance of the <code>XPathReader</code> specified
80       *          by the <code>org.saxpath.driver</code> property
81       *
82       *  @throws SAXPathException if the property is  not set, or if
83       *          the class can not be instantiated for some reason,
84       *          or if the class doesn't implement the <code>XPathReader</code>
85       *          interface
86       */
87      public static XPathReader createReader() throws SAXPathException
88      {
89          String className = null;
90  
91          try
92          {
93              className = System.getProperty( DRIVER_PROPERTY );
94          }
95          catch (SecurityException e)
96          {
97              // we'll use the default
98          }
99  
100         if ( className == null
101              ||
102              className.length() == 0 )
103         {
104             className = DEFAULT_DRIVER;
105         }
106 
107         return createReader( className );
108     }
109     
110     /** Create an <code>XPathReader</code> using the passed
111      *  in class name.
112      *
113      *  @param className the name of the class that implements
114      *         the <code>XPathReader</code> interface.
115      * 
116      *  @return an XPathReader
117      *
118      *  @throws SAXPathException if the class cannot be
119      *          instantiated for some reason, or if the
120      *          class doesn't implement the <code>XPathReader</code>
121      *          interface
122      */
123     public static XPathReader createReader(String className) throws SAXPathException
124     {
125         Class<?> readerClass  = null;
126         XPathReader reader = null;
127 
128         try
129         {
130             // Use the full version of Class.forName(), so as to
131             // work better in sandboxed environments, such as
132             // servlet containers and applets.
133 
134             readerClass = Class.forName( className,
135                                          true,
136                                          XPathReaderFactory.class.getClassLoader() );
137             
138             // Double-check that it's actually the right kind of class
139             // before attempting instantiation.
140             
141             if ( ! XPathReader.class.isAssignableFrom( readerClass ) )
142             {
143                 throw new SAXPathException( "Class [" + className 
144                   + "] does not implement the org.jaxen.saxpath.XPathReader interface." );
145             }
146         }
147         catch (ClassNotFoundException e)
148         {
149             throw new SAXPathException( e );
150         }
151 
152         try
153         {
154             reader = (XPathReader) readerClass.newInstance();
155         }
156         catch (IllegalAccessException e)
157         {
158             throw new SAXPathException( e );
159         }
160         catch (InstantiationException e)
161         {
162             throw new SAXPathException( e );
163         }
164         
165         return reader;
166     }
167 }