001 /* SAXParserFactory.java --
002 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.xml.parsers;
039
040 import java.io.BufferedReader;
041 import java.io.File;
042 import java.io.FileInputStream;
043 import java.io.InputStream;
044 import java.io.InputStreamReader;
045 import java.io.IOException;
046 import java.util.Properties;
047 import javax.xml.validation.Schema;
048 import org.xml.sax.SAXException;
049 import org.xml.sax.SAXNotRecognizedException;
050 import org.xml.sax.SAXNotSupportedException;
051
052 /**
053 * Factory for obtaining SAX parsers.
054 * Instances of this class are <em>not</em> guaranteed to be thread safe.
055 *
056 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
057 */
058 public abstract class SAXParserFactory
059 {
060
061 private boolean validating;
062 private boolean namespaceAware;
063 private Schema schema;
064 private boolean xIncludeAware;
065
066 protected SAXParserFactory()
067 {
068 }
069
070 /**
071 * Creates a new factory instance.
072 * The implementation class to load is the first found in the following
073 * locations:
074 * <ol>
075 * <li>the <code>javax.xml.parsers.SAXParserFactory</code> system
076 * property</li>
077 * <li>the above named property value in the
078 * <code><i>$JAVA_HOME</i>/lib/jaxp.properties</code> file</li>
079 * <li>the class name specified in the
080 * <code>META-INF/services/javax.xml.parsers.SAXParserFactory</code>
081 * system resource</li>
082 * <li>the default factory class</li>
083 * </ol>
084 */
085 public static SAXParserFactory newInstance()
086 throws FactoryConfigurationError
087 {
088 ClassLoader loader = Thread.currentThread().getContextClassLoader();
089 if (loader == null)
090 {
091 loader = SAXParserFactory.class.getClassLoader();
092 }
093 String className = null;
094 int count = 0;
095 do
096 {
097 className = getFactoryClassName(loader, count++);
098 if (className != null)
099 {
100 try
101 {
102 Class<?> t = (loader != null) ? loader.loadClass(className) :
103 Class.forName(className);
104 return (SAXParserFactory) t.newInstance();
105 }
106 catch (ClassNotFoundException e)
107 {
108 className = null;
109 }
110 catch (Exception e)
111 {
112 throw new FactoryConfigurationError(e,
113 "error instantiating class " + className);
114 }
115 }
116 }
117 while (className == null && count < 3);
118 return new gnu.xml.stream.SAXParserFactory();
119 }
120
121 private static String getFactoryClassName(ClassLoader loader, int attempt)
122 {
123 final String propertyName = "javax.xml.parsers.SAXParserFactory";
124 switch (attempt)
125 {
126 case 0:
127 return System.getProperty(propertyName);
128 case 1:
129 try
130 {
131 File file = new File(System.getProperty("java.home"));
132 file = new File(file, "lib");
133 file = new File(file, "jaxp.properties");
134 InputStream in = new FileInputStream(file);
135 Properties props = new Properties();
136 props.load(in);
137 in.close();
138 return props.getProperty(propertyName);
139 }
140 catch (IOException e)
141 {
142 return null;
143 }
144 case 2:
145 try
146 {
147 String serviceKey = "/META-INF/services/" + propertyName;
148 InputStream in = (loader != null) ?
149 loader.getResourceAsStream(serviceKey) :
150 SAXParserFactory.class.getResourceAsStream(serviceKey);
151 if (in != null)
152 {
153 BufferedReader r =
154 new BufferedReader(new InputStreamReader(in));
155 String ret = r.readLine();
156 r.close();
157 return ret;
158 }
159 }
160 catch (IOException e)
161 {
162 }
163 return null;
164 default:
165 return null;
166 }
167 }
168
169 /**
170 * Creates a new parser instance using the currently specified factory
171 * configuration.
172 * @exception ParserConfigurationException if the specified configuration
173 * is not supported
174 */
175 public abstract SAXParser newSAXParser()
176 throws ParserConfigurationException, SAXException;
177
178 /**
179 * Sets whether parsers obtained from this factory will be XML Namespace
180 * aware.
181 */
182 public void setNamespaceAware(boolean awareness)
183 {
184 namespaceAware = awareness;
185 }
186
187 /**
188 * Sets whether parsers obtained from this factory will validate their
189 * input.
190 */
191 public void setValidating(boolean validating)
192 {
193 this.validating = validating;
194 }
195
196 /**
197 * Indicates whether parsers obtained from this factory will be XML
198 * Namespace aware.
199 */
200 public boolean isNamespaceAware()
201 {
202 return namespaceAware;
203 }
204
205 /**
206 * Indicates whether parsers obtained from this factory will validate
207 * their input.
208 */
209 public boolean isValidating()
210 {
211 return validating;
212 }
213
214 /**
215 * Sets the specified feature for SAX2 parsers obtained from this factory.
216 * @param name the feature name
217 * @param value the featurevalue
218 */
219 public abstract void setFeature(String name, boolean value)
220 throws ParserConfigurationException, SAXNotRecognizedException,
221 SAXNotSupportedException;
222
223 /**
224 * Returns the specified feature for SAX2 parsers obtained from this
225 * factory.
226 * @param name the feature name
227 */
228 public abstract boolean getFeature(String name)
229 throws ParserConfigurationException, SAXNotRecognizedException,
230 SAXNotSupportedException;
231
232 // -- JAXP 1.3 methods --
233
234 /**
235 * Returns the schema.
236 * @since 1.3
237 * @see #setSchema
238 */
239 public Schema getSchema()
240 {
241 return schema;
242 }
243
244 /**
245 * Sets the schema.
246 * @since 1.3
247 * @see #getSchema
248 */
249 public void setSchema(Schema schema)
250 {
251 this.schema = schema;
252 }
253
254 /**
255 * Indicates whether parsers obtained from this factory will be XInclude
256 * aware.
257 * @since 1.3
258 */
259 public boolean isXIncludeAware()
260 {
261 return xIncludeAware;
262 }
263
264 /**
265 * Sets whether parsers obtained from this factory will be XInclude aware.
266 * @since 1.3
267 */
268 public void setXIncludeAware(boolean state)
269 {
270 xIncludeAware = state;
271 }
272
273 }