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