1 /* SAXParserFactory.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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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
.parsers
;
40 import java
.io
.BufferedReader
;
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
;
47 import javax
.xml
.validation
.Schema
;
48 import org
.xml
.sax
.SAXException
;
49 import org
.xml
.sax
.SAXNotRecognizedException
;
50 import org
.xml
.sax
.SAXNotSupportedException
;
53 * Factory for obtaining SAX parsers.
54 * Instances of this class are <em>not</em> guaranteed to be thread safe.
56 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
58 public abstract class SAXParserFactory
61 private boolean validating
;
62 private boolean namespaceAware
;
63 private Schema schema
;
64 private boolean xIncludeAware
;
66 protected SAXParserFactory()
71 * Creates a new factory instance.
72 * The implementation class to load is the first found in the following
75 * <li>the <code>javax.xml.parsers.SAXParserFactory</code> system
77 * <li>the above named property value in the
78 * <code><i>$JAVA_HOME</i>/lib/jaxp.properties</code> file</li>
79 * <li>the class name specified in the
80 * <code>META-INF/services/javax.xml.parsers.SAXParserFactory</code>
81 * system resource</li>
82 * <li>the default factory class</li>
85 public static SAXParserFactory
newInstance()
86 throws FactoryConfigurationError
88 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
91 loader
= SAXParserFactory
.class.getClassLoader();
93 String className
= null;
97 className
= getFactoryClassName(loader
, count
++);
98 if (className
!= null)
102 Class t
= (loader
!= null) ? loader
.loadClass(className
) :
103 Class
.forName(className
);
104 return (SAXParserFactory
) t
.newInstance();
106 catch (ClassNotFoundException e
)
112 throw new FactoryConfigurationError(e
,
113 "error instantiating class " + className
);
117 while (className
== null && count
< 3);
118 return new gnu
.xml
.stream
.SAXParserFactory();
121 private static String
getFactoryClassName(ClassLoader loader
, int attempt
)
123 final String propertyName
= "javax.xml.parsers.SAXParserFactory";
127 return System
.getProperty(propertyName
);
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();
138 return props
.getProperty(propertyName
);
140 catch (IOException e
)
147 String serviceKey
= "/META-INF/services/" + propertyName
;
148 InputStream in
= (loader
!= null) ?
149 loader
.getResourceAsStream(serviceKey
) :
150 SAXParserFactory
.class.getResourceAsStream(serviceKey
);
154 new BufferedReader(new InputStreamReader(in
));
155 String ret
= r
.readLine();
160 catch (IOException e
)
170 * Creates a new parser instance using the currently specified factory
172 * @exception ParserConfigurationException if the specified configuration
175 public abstract SAXParser
newSAXParser()
176 throws ParserConfigurationException
, SAXException
;
179 * Sets whether parsers obtained from this factory will be XML Namespace
182 public void setNamespaceAware(boolean awareness
)
184 namespaceAware
= awareness
;
188 * Sets whether parsers obtained from this factory will validate their
191 public void setValidating(boolean validating
)
193 this.validating
= validating
;
197 * Indicates whether parsers obtained from this factory will be XML
200 public boolean isNamespaceAware()
202 return namespaceAware
;
206 * Indicates whether parsers obtained from this factory will validate
209 public boolean isValidating()
215 * Sets the specified feature for SAX2 parsers obtained from this factory.
216 * @param name the feature name
217 * @param value the featurevalue
219 public abstract void setFeature(String name
, boolean value
)
220 throws ParserConfigurationException
, SAXNotRecognizedException
,
221 SAXNotSupportedException
;
224 * Returns the specified feature for SAX2 parsers obtained from this
226 * @param name the feature name
228 public abstract boolean getFeature(String name
)
229 throws ParserConfigurationException
, SAXNotRecognizedException
,
230 SAXNotSupportedException
;
232 // -- JAXP 1.3 methods --
235 * Returns the schema.
239 public Schema
getSchema()
249 public void setSchema(Schema schema
)
251 this.schema
= schema
;
255 * Indicates whether parsers obtained from this factory will be XInclude
259 public boolean isXIncludeAware()
261 return xIncludeAware
;
265 * Sets whether parsers obtained from this factory will be XInclude aware.
268 public void setXIncludeAware(boolean state
)
270 xIncludeAware
= state
;