Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XPathReaderFactory |
|
| 4.333333333333333;4.333 |
1 | /* | |
2 | * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/saxpath/helpers/XPathReaderFactory.java,v 1.9 2006/09/15 20:41:38 elharo Exp $ | |
3 | * $Revision: 1.9 $ | |
4 | * $Date: 2006/09/15 20:41:38 $ | |
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: XPathReaderFactory.java,v 1.9 2006/09/15 20:41:38 elharo Exp $ | |
47 | */ | |
48 | ||
49 | ||
50 | ||
51 | ||
52 | package org.jaxen.saxpath.helpers; | |
53 | ||
54 | import org.jaxen.saxpath.SAXPathException; | |
55 | import org.jaxen.saxpath.XPathReader; | |
56 | ||
57 | /** Create an {@link org.jaxen.saxpath.XPathReader} from | |
58 | * either a system property, or a named class. | |
59 | * | |
60 | * <p> | |
61 | * Similar to the SAX API, the <code>XPathReaderFactory</code> | |
62 | * can create an <code>XPathReader</code> from a name of a | |
63 | * class passed in directly, or by inspecting the system | |
64 | * property <code>org.saxpath.driver</code>. | |
65 | * | |
66 | * @author bob mcwhirter (bob@werken.com) | |
67 | */ | |
68 | public class XPathReaderFactory | |
69 | { | |
70 | /** The <code>org.saxpath.driver</code> property name. */ | |
71 | public static final String DRIVER_PROPERTY = "org.saxpath.driver"; | |
72 | ||
73 | /** The default driver to use if none is configured. */ | |
74 | protected static final String DEFAULT_DRIVER = "org.jaxen.saxpath.base.XPathReader"; | |
75 | ||
76 | 0 | private XPathReaderFactory() {} |
77 | ||
78 | ||
79 | /** Create an <code>XPathReader</code> using the value of | |
80 | * the <code>org.saxpath.driver</code> system property. | |
81 | * | |
82 | * @return an instance of the <code>XPathReader</code> specified | |
83 | * by the <code>org.saxpath.driver</code> property | |
84 | * | |
85 | * @throws SAXPathException if the property is not set, or if | |
86 | * the class can not be instantiated for some reason, | |
87 | * or if the class doesn't implement the <code>XPathReader</code> | |
88 | * interface | |
89 | */ | |
90 | public static XPathReader createReader() throws SAXPathException | |
91 | { | |
92 | 6128 | String className = null; |
93 | ||
94 | try | |
95 | { | |
96 | 6128 | className = System.getProperty( DRIVER_PROPERTY ); |
97 | } | |
98 | 0 | catch (SecurityException e) |
99 | { | |
100 | // we'll use the default | |
101 | 6128 | } |
102 | ||
103 | 6128 | if ( className == null |
104 | || | |
105 | className.length() == 0 ) | |
106 | { | |
107 | 6120 | className = DEFAULT_DRIVER; |
108 | } | |
109 | ||
110 | 6128 | return createReader( className ); |
111 | } | |
112 | ||
113 | /** Create an <code>XPathReader</code> using the passed | |
114 | * in class name. | |
115 | * | |
116 | * @param className the name of the class that implements | |
117 | * the <code>XPathReader</code> interface. | |
118 | * | |
119 | * @return an XPathReader | |
120 | * | |
121 | * @throws SAXPathException if the class cannot be | |
122 | * instantiated for some reason, or if the | |
123 | * class doesn't implement the <code>XPathReader</code> | |
124 | * interface | |
125 | */ | |
126 | public static XPathReader createReader(String className) throws SAXPathException | |
127 | { | |
128 | 6134 | Class readerClass = null; |
129 | 6134 | XPathReader reader = null; |
130 | ||
131 | try | |
132 | { | |
133 | // Use the full version of Class.forName(), so as to | |
134 | // work better in sandboxed environments, such as | |
135 | // servlet containers and applets. | |
136 | ||
137 | 6134 | readerClass = Class.forName( className, |
138 | true, | |
139 | 236 | XPathReaderFactory.class.getClassLoader() ); |
140 | ||
141 | // Double-check that it's actually the right kind of class | |
142 | // before attempting instantiation. | |
143 | ||
144 | 6130 | if ( ! XPathReader.class.isAssignableFrom( readerClass ) ) |
145 | { | |
146 | 6 | throw new SAXPathException( "Class [" + className |
147 | + "] does not implement the org.jaxen.saxpath.XPathReader interface." ); | |
148 | } | |
149 | } | |
150 | 4 | catch (ClassNotFoundException e) |
151 | { | |
152 | 4 | throw new SAXPathException( e ); |
153 | 6124 | } |
154 | ||
155 | try | |
156 | { | |
157 | 6124 | reader = (XPathReader) readerClass.newInstance(); |
158 | } | |
159 | 0 | catch (IllegalAccessException e) |
160 | { | |
161 | 0 | throw new SAXPathException( e ); |
162 | } | |
163 | 0 | catch (InstantiationException e) |
164 | { | |
165 | 0 | throw new SAXPathException( e ); |
166 | 6124 | } |
167 | ||
168 | 6124 | return reader; |
169 | } | |
170 | } |