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. package com.sun.org.apache.xpath.internal.jaxp;
  18. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  19. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  20. import javax.xml.XMLConstants;
  21. import javax.xml.xpath.XPathFactory;
  22. import javax.xml.xpath.XPathFactoryConfigurationException;
  23. import javax.xml.xpath.XPathFunctionResolver;
  24. import javax.xml.xpath.XPathVariableResolver;
  25. /**
  26. * The XPathFactory builds XPaths.
  27. *
  28. * @version $Revision: 1.9 $
  29. * @author Ramesh Mandava
  30. */
  31. public class XPathFactoryImpl extends XPathFactory {
  32. /**
  33. * <p>Name of class as a constant to use for debugging.</p>
  34. */
  35. private static final String CLASS_NAME = "XPathFactoryImpl";
  36. /**
  37. *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p>
  38. */
  39. private XPathFunctionResolver xPathFunctionResolver = null;
  40. /**
  41. * <p>XPathVariableResolver for this XPathFactory and created XPaths</p>
  42. */
  43. private XPathVariableResolver xPathVariableResolver = null;
  44. /**
  45. * <p>State of secure processing feature.</p>
  46. */
  47. private boolean featureSecureProcessing = false;
  48. /**
  49. * <p>Is specified object model supported by this
  50. * <code>XPathFactory</code>?</p>
  51. *
  52. * @param objectModel Specifies the object model which the returned
  53. * <code>XPathFactory</code> will understand.
  54. *
  55. * @return <code>true</code> if <code>XPathFactory</code> supports
  56. * <code>objectModel</code>, else <code>false</code>.
  57. *
  58. * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
  59. * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
  60. */
  61. public boolean isObjectModelSupported(String objectModel) {
  62. if (objectModel == null) {
  63. String fmsg = XSLMessages.createXPATHMessage(
  64. XPATHErrorResources.ER_OBJECT_MODEL_NULL,
  65. new Object[] { this.getClass().getName() } );
  66. throw new NullPointerException( fmsg );
  67. }
  68. if (objectModel.length() == 0) {
  69. String fmsg = XSLMessages.createXPATHMessage(
  70. XPATHErrorResources.ER_OBJECT_MODEL_EMPTY,
  71. new Object[] { this.getClass().getName() } );
  72. throw new IllegalArgumentException( fmsg );
  73. }
  74. // know how to support default object model, W3C DOM
  75. if (objectModel.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
  76. return true;
  77. }
  78. // don't know how to support anything else
  79. return false;
  80. }
  81. /**
  82. * <p>Returns a new <code>XPath</code> object using the underlying
  83. * object model determined when the factory was instantiated.</p>
  84. *
  85. * @return New <code>XPath</code>
  86. */
  87. public javax.xml.xpath.XPath newXPath() {
  88. return new com.sun.org.apache.xpath.internal.jaxp.XPathImpl(
  89. xPathVariableResolver, xPathFunctionResolver,
  90. featureSecureProcessing );
  91. }
  92. /**
  93. * <p>Set a feature for this <code>XPathFactory</code> and
  94. * <code>XPath</code>s created by this factory.</p>
  95. *
  96. * <p>
  97. * Feature names are fully qualified {@link java.net.URI}s.
  98. * Implementations may define their own features.
  99. * An {@link XPathFactoryConfigurationException} is thrown if this
  100. * <code>XPathFactory</code> or the <code>XPath</code>s
  101. * it creates cannot support the feature.
  102. * It is possible for an <code>XPathFactory</code> to expose a feature
  103. * value but be unable to change its state.
  104. * </p>
  105. *
  106. * <p>See {@link javax.xml.xpath.XPathFactory} for full documentation
  107. * of specific features.</p>
  108. *
  109. * @param name Feature name.
  110. * @param value Is feature state <code>true</code> or <code>false</code>.
  111. *
  112. * @throws XPathFactoryConfigurationException if this
  113. * <code>XPathFactory</code> or the <code>XPath</code>s
  114. * it creates cannot support this feature.
  115. * @throws NullPointerException if <code>name</code> is
  116. * <code>null</code>.
  117. */
  118. public void setFeature(String name, boolean value)
  119. throws XPathFactoryConfigurationException {
  120. // feature name cannot be null
  121. if (name == null) {
  122. String fmsg = XSLMessages.createXPATHMessage(
  123. XPATHErrorResources.ER_FEATURE_NAME_NULL,
  124. new Object[] { CLASS_NAME, new Boolean( value) } );
  125. throw new NullPointerException( fmsg );
  126. }
  127. // secure processing?
  128. if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
  129. featureSecureProcessing = value;
  130. // all done processing feature
  131. return;
  132. }
  133. // unknown feature
  134. String fmsg = XSLMessages.createXPATHMessage(
  135. XPATHErrorResources.ER_FEATURE_UNKNOWN,
  136. new Object[] { name, CLASS_NAME, new Boolean(value) } );
  137. throw new XPathFactoryConfigurationException( fmsg );
  138. }
  139. /**
  140. * <p>Get the state of the named feature.</p>
  141. *
  142. * <p>
  143. * Feature names are fully qualified {@link java.net.URI}s.
  144. * Implementations may define their own features.
  145. * An {@link XPathFactoryConfigurationException} is thrown if this
  146. * <code>XPathFactory</code> or the <code>XPath</code>s
  147. * it creates cannot support the feature.
  148. * It is possible for an <code>XPathFactory</code> to expose a feature
  149. * value but be unable to change its state.
  150. * </p>
  151. *
  152. * @param name Feature name.
  153. *
  154. * @return State of the named feature.
  155. *
  156. * @throws XPathFactoryConfigurationException if this
  157. * <code>XPathFactory</code> or the <code>XPath</code>s
  158. * it creates cannot support this feature.
  159. * @throws NullPointerException if <code>name</code> is
  160. * <code>null</code>.
  161. */
  162. public boolean getFeature(String name)
  163. throws XPathFactoryConfigurationException {
  164. // feature name cannot be null
  165. if (name == null) {
  166. String fmsg = XSLMessages.createXPATHMessage(
  167. XPATHErrorResources.ER_GETTING_NULL_FEATURE,
  168. new Object[] { CLASS_NAME } );
  169. throw new NullPointerException( fmsg );
  170. }
  171. // secure processing?
  172. if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
  173. return featureSecureProcessing;
  174. }
  175. // unknown feature
  176. String fmsg = XSLMessages.createXPATHMessage(
  177. XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE,
  178. new Object[] { name, CLASS_NAME } );
  179. throw new XPathFactoryConfigurationException( fmsg );
  180. }
  181. /**
  182. * <p>Establish a default function resolver.</p>
  183. *
  184. * <p>Any <code>XPath</code> objects constructed from this factory will use
  185. * the specified resolver by default.</p>
  186. *
  187. * <p>A <code>NullPointerException</code> is thrown if
  188. * <code>resolver</code> is <code>null</code>.</p>
  189. *
  190. * @param resolver XPath function resolver.
  191. *
  192. * @throws NullPointerException If <code>resolver</code> is
  193. * <code>null</code>.
  194. */
  195. public void setXPathFunctionResolver(XPathFunctionResolver resolver) {
  196. // resolver cannot be null
  197. if (resolver == null) {
  198. String fmsg = XSLMessages.createXPATHMessage(
  199. XPATHErrorResources.ER_NULL_XPATH_FUNCTION_RESOLVER,
  200. new Object[] { CLASS_NAME } );
  201. throw new NullPointerException( fmsg );
  202. }
  203. xPathFunctionResolver = resolver;
  204. }
  205. /**
  206. * <p>Establish a default variable resolver.</p>
  207. *
  208. * <p>Any <code>XPath</code> objects constructed from this factory will use
  209. * the specified resolver by default.</p>
  210. *
  211. * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  212. *
  213. * @param resolver Variable resolver.
  214. *
  215. * @throws NullPointerException If <code>resolver</code> is
  216. * <code>null</code>.
  217. */
  218. public void setXPathVariableResolver(XPathVariableResolver resolver) {
  219. // resolver cannot be null
  220. if (resolver == null) {
  221. String fmsg = XSLMessages.createXPATHMessage(
  222. XPATHErrorResources.ER_NULL_XPATH_VARIABLE_RESOLVER,
  223. new Object[] { CLASS_NAME } );
  224. throw new NullPointerException( fmsg );
  225. }
  226. xPathVariableResolver = resolver;
  227. }
  228. }