KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > jaxp > XPathFactoryImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 // $Id: XPathFactoryImpl.java,v 1.9 2004/07/10 21:39:19 rameshm Exp $
17

18 package com.sun.org.apache.xpath.internal.jaxp;
19
20 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
21 import com.sun.org.apache.xalan.internal.res.XSLMessages;
22
23 import javax.xml.XMLConstants JavaDoc;
24 import javax.xml.xpath.XPathFactory JavaDoc;
25 import javax.xml.xpath.XPathFactoryConfigurationException JavaDoc;
26 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
27 import javax.xml.xpath.XPathVariableResolver JavaDoc;
28
29 /**
30  * The XPathFactory builds XPaths.
31  *
32  * @version $Revision: 1.9 $
33  * @author Ramesh Mandava
34  */

35 public class XPathFactoryImpl extends XPathFactory JavaDoc {
36     
37     /**
38      * <p>Name of class as a constant to use for debugging.</p>
39      */

40     private static final String JavaDoc CLASS_NAME = "XPathFactoryImpl";
41     
42     /**
43      *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p>
44      */

45     private XPathFunctionResolver JavaDoc xPathFunctionResolver = null;
46     
47     /**
48      * <p>XPathVariableResolver for this XPathFactory and created XPaths</p>
49      */

50     private XPathVariableResolver JavaDoc xPathVariableResolver = null;
51
52     /**
53      * <p>State of secure processing feature.</p>
54      */

55     private boolean featureSecureProcessing = false;
56         
57     /**
58      * <p>Is specified object model supported by this
59          * <code>XPathFactory</code>?</p>
60      *
61      * @param objectModel Specifies the object model which the returned
62          * <code>XPathFactory</code> will understand.
63      *
64      * @return <code>true</code> if <code>XPathFactory</code> supports
65          * <code>objectModel</code>, else <code>false</code>.
66      *
67      * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
68      * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
69      */

70     public boolean isObjectModelSupported(String JavaDoc objectModel) {
71         
72             if (objectModel == null) {
73                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
74                         XPATHErrorResources.ER_OBJECT_MODEL_NULL,
75                         new Object JavaDoc[] { this.getClass().getName() } );
76
77                 throw new NullPointerException JavaDoc( fmsg );
78             }
79         
80             if (objectModel.length() == 0) {
81                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
82                         XPATHErrorResources.ER_OBJECT_MODEL_EMPTY,
83                         new Object JavaDoc[] { this.getClass().getName() } );
84                 throw new IllegalArgumentException JavaDoc( fmsg );
85             }
86         
87         // know how to support default object model, W3C DOM
88
if (objectModel.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
89                 return true;
90             }
91         
92             // don't know how to support anything else
93
return false;
94     }
95
96         /**
97          * <p>Returns a new <code>XPath</code> object using the underlying
98          * object model determined when the factory was instantiated.</p>
99      *
100      * @return New <code>XPath</code>
101      */

102     public javax.xml.xpath.XPath JavaDoc newXPath() {
103         return new com.sun.org.apache.xpath.internal.jaxp.XPathImpl(
104                     xPathVariableResolver, xPathFunctionResolver,
105                     featureSecureProcessing );
106     }
107         
108     /**
109      * <p>Set a feature for this <code>XPathFactory</code> and
110          * <code>XPath</code>s created by this factory.</p>
111      *
112      * <p>
113      * Feature names are fully qualified {@link java.net.URI}s.
114      * Implementations may define their own features.
115      * An {@link XPathFactoryConfigurationException} is thrown if this
116          * <code>XPathFactory</code> or the <code>XPath</code>s
117      * it creates cannot support the feature.
118      * It is possible for an <code>XPathFactory</code> to expose a feature
119          * value but be unable to change its state.
120      * </p>
121      *
122      * <p>See {@link javax.xml.xpath.XPathFactory} for full documentation
123          * of specific features.</p>
124      *
125      * @param name Feature name.
126      * @param value Is feature state <code>true</code> or <code>false</code>.
127      *
128      * @throws XPathFactoryConfigurationException if this
129          * <code>XPathFactory</code> or the <code>XPath</code>s
130      * it creates cannot support this feature.
131          * @throws NullPointerException if <code>name</code> is
132          * <code>null</code>.
133      */

134     public void setFeature(String JavaDoc name, boolean value)
135         throws XPathFactoryConfigurationException JavaDoc {
136             
137             // feature name cannot be null
138
if (name == null) {
139                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
140                         XPATHErrorResources.ER_FEATURE_NAME_NULL,
141                         new Object JavaDoc[] { CLASS_NAME, new Boolean JavaDoc( value) } );
142                 throw new NullPointerException JavaDoc( fmsg );
143              }
144         
145             // secure processing?
146
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
147
148                 featureSecureProcessing = value;
149                         
150                 // all done processing feature
151
return;
152             }
153         
154             // unknown feature
155
String JavaDoc fmsg = XSLMessages.createXPATHMessage(
156                     XPATHErrorResources.ER_FEATURE_UNKNOWN,
157                     new Object JavaDoc[] { name, CLASS_NAME, new Boolean JavaDoc(value) } );
158             throw new XPathFactoryConfigurationException JavaDoc( fmsg );
159     }
160
161     /**
162      * <p>Get the state of the named feature.</p>
163      *
164      * <p>
165      * Feature names are fully qualified {@link java.net.URI}s.
166      * Implementations may define their own features.
167      * An {@link XPathFactoryConfigurationException} is thrown if this
168          * <code>XPathFactory</code> or the <code>XPath</code>s
169      * it creates cannot support the feature.
170      * It is possible for an <code>XPathFactory</code> to expose a feature
171          * value but be unable to change its state.
172      * </p>
173      *
174      * @param name Feature name.
175      *
176      * @return State of the named feature.
177      *
178      * @throws XPathFactoryConfigurationException if this
179          * <code>XPathFactory</code> or the <code>XPath</code>s
180      * it creates cannot support this feature.
181          * @throws NullPointerException if <code>name</code> is
182          * <code>null</code>.
183      */

184     public boolean getFeature(String JavaDoc name)
185         throws XPathFactoryConfigurationException JavaDoc {
186
187             // feature name cannot be null
188
if (name == null) {
189                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
190                         XPATHErrorResources.ER_GETTING_NULL_FEATURE,
191                         new Object JavaDoc[] { CLASS_NAME } );
192                 throw new NullPointerException JavaDoc( fmsg );
193             }
194         
195             // secure processing?
196
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
197                 return featureSecureProcessing;
198             }
199         
200             // unknown feature
201
String JavaDoc fmsg = XSLMessages.createXPATHMessage(
202                     XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE,
203                     new Object JavaDoc[] { name, CLASS_NAME } );
204
205             throw new XPathFactoryConfigurationException JavaDoc( fmsg );
206         }
207         
208     /**
209          * <p>Establish a default function resolver.</p>
210          *
211      * <p>Any <code>XPath</code> objects constructed from this factory will use
212      * the specified resolver by default.</p>
213      *
214      * <p>A <code>NullPointerException</code> is thrown if
215          * <code>resolver</code> is <code>null</code>.</p>
216          *
217      * @param resolver XPath function resolver.
218      *
219      * @throws NullPointerException If <code>resolver</code> is
220          * <code>null</code>.
221      */

222         public void setXPathFunctionResolver(XPathFunctionResolver JavaDoc resolver) {
223             
224             // resolver cannot be null
225
if (resolver == null) {
226                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
227                         XPATHErrorResources.ER_NULL_XPATH_FUNCTION_RESOLVER,
228                         new Object JavaDoc[] { CLASS_NAME } );
229                 throw new NullPointerException JavaDoc( fmsg );
230             }
231             
232             xPathFunctionResolver = resolver;
233         }
234         
235     /**
236      * <p>Establish a default variable resolver.</p>
237      *
238      * <p>Any <code>XPath</code> objects constructed from this factory will use
239      * the specified resolver by default.</p>
240      *
241      * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
242      *
243      * @param resolver Variable resolver.
244      *
245      * @throws NullPointerException If <code>resolver</code> is
246          * <code>null</code>.
247      */

248     public void setXPathVariableResolver(XPathVariableResolver JavaDoc resolver) {
249
250         // resolver cannot be null
251
if (resolver == null) {
252                     String JavaDoc fmsg = XSLMessages.createXPATHMessage(
253                             XPATHErrorResources.ER_NULL_XPATH_VARIABLE_RESOLVER,
254                             new Object JavaDoc[] { CLASS_NAME } );
255             throw new NullPointerException JavaDoc( fmsg );
256         }
257             
258         xPathVariableResolver = resolver;
259     }
260 }
261
262
263
264
Popular Tags