XPathFactory.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* XPathFactory.java --
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.xml.xpath;
  32. import java.io.BufferedReader;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.IOException;
  38. import java.util.Properties;
  39. /**
  40. * Factory for creating XPath environments.
  41. *
  42. * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
  43. * @since 1.3
  44. */
  45. public abstract class XPathFactory
  46. {
  47. /**
  48. * The default property name according to the JAXP specification.
  49. */
  50. public static final String DEFAULT_PROPERTY_NAME =
  51. "javax.xml.xpath.XPathFactory";
  52. /**
  53. * The default object model URI.
  54. */
  55. public static final String DEFAULT_OBJECT_MODEL_URI =
  56. XPathConstants.DOM_OBJECT_MODEL;
  57. protected XPathFactory()
  58. {
  59. }
  60. /**
  61. * Returns a new factory for the default (DOM) object model.
  62. */
  63. public static final XPathFactory newInstance()
  64. {
  65. try
  66. {
  67. return newInstance(DEFAULT_OBJECT_MODEL_URI);
  68. }
  69. catch (XPathFactoryConfigurationException e)
  70. {
  71. throw new RuntimeException(e.getMessage());
  72. }
  73. }
  74. /**
  75. * Returns a new factory for the given object model URI.
  76. * The implementation class to load is the first found in the following
  77. * locations that advertises support for the given model URI:
  78. * <ol>
  79. * <li>the <code>javax.xml.xpath.XPathFactory</code> system property</li>
  80. * <li>the above named property value in the
  81. * <code><i>$JAVA_HOME</i>/lib/jaxp.properties</code> file</li>
  82. * <li>the class name specified in the
  83. * <code>META-INF/services/javax.xml.xpath.XPathFactory</code> system
  84. * resource</li>
  85. * <li>the default factory class</li>
  86. * </ol>
  87. * @param uri the object model URI
  88. */
  89. public static final XPathFactory newInstance(String uri)
  90. throws XPathFactoryConfigurationException
  91. {
  92. ClassLoader loader = Thread.currentThread().getContextClassLoader();
  93. if (loader == null)
  94. {
  95. loader = XPathFactory.class.getClassLoader();
  96. }
  97. String className = null;
  98. int count = 0;
  99. do
  100. {
  101. className = getFactoryClassName(loader, count++);
  102. if (className != null)
  103. {
  104. try
  105. {
  106. Class<?> t = (loader != null) ? loader.loadClass(className) :
  107. Class.forName(className);
  108. XPathFactory ret = (XPathFactory) t.newInstance();
  109. if (ret.isObjectModelSupported(uri))
  110. {
  111. return ret;
  112. }
  113. className = null;
  114. }
  115. catch (ClassNotFoundException e)
  116. {
  117. className = null;
  118. }
  119. catch (Exception e)
  120. {
  121. throw new XPathFactoryConfigurationException(e);
  122. }
  123. }
  124. }
  125. while (className == null && count < 4);
  126. String msg = "no factories with support for " + uri;
  127. throw new XPathFactoryConfigurationException(msg);
  128. }
  129. private static String getFactoryClassName(ClassLoader loader, int attempt)
  130. {
  131. final String propertyName = DEFAULT_PROPERTY_NAME;
  132. switch (attempt)
  133. {
  134. case 0:
  135. return System.getProperty(propertyName);
  136. case 1:
  137. try
  138. {
  139. File file = new File(System.getProperty("java.home"));
  140. file = new File(file, "lib");
  141. file = new File(file, "jaxp.properties");
  142. InputStream in = new FileInputStream(file);
  143. Properties props = new Properties();
  144. props.load(in);
  145. in.close();
  146. return props.getProperty(propertyName);
  147. }
  148. catch (IOException e)
  149. {
  150. return null;
  151. }
  152. case 2:
  153. try
  154. {
  155. String serviceKey = "/META-INF/services/" + propertyName;
  156. InputStream in = (loader != null) ?
  157. loader.getResourceAsStream(serviceKey) :
  158. XPathFactory.class.getResourceAsStream(serviceKey);
  159. if (in != null)
  160. {
  161. BufferedReader r =
  162. new BufferedReader(new InputStreamReader(in));
  163. String ret = r.readLine();
  164. r.close();
  165. return ret;
  166. }
  167. }
  168. catch (IOException e)
  169. {
  170. }
  171. return null;
  172. case 3:
  173. return "gnu.xml.xpath.XPathFactoryImpl";
  174. default:
  175. return null;
  176. }
  177. }
  178. /**
  179. * Indicates whether the specified object model URI is supported by
  180. * this factory.
  181. */
  182. public abstract boolean isObjectModelSupported(String objectModel);
  183. /**
  184. * Sets the state of the named feature.
  185. */
  186. public abstract void setFeature(String name, boolean value)
  187. throws XPathFactoryConfigurationException;
  188. /**
  189. * Returns the state of the named feature.
  190. */
  191. public abstract boolean getFeature(String name)
  192. throws XPathFactoryConfigurationException;
  193. /**
  194. * Sets the XPath variable resolver calback.
  195. */
  196. public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
  197. /**
  198. * Sets the XPath extension function resolver calback.
  199. */
  200. public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
  201. /**
  202. * Returns a new XPath evaluation environment.
  203. */
  204. public abstract XPath newXPath();
  205. }