KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > parsers > SAXParserFactory


1 // $Id: SAXParserFactory.java,v 1.39 2004/04/20 00:22:02 kk122374 Exp $
2

3 /*
4  * @(#)SAXParserFactory.java 1.41 04/07/26
5  *
6  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package javax.xml.parsers;
11
12 import javax.xml.validation.Schema JavaDoc;
13
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.SAXNotRecognizedException JavaDoc;
16 import org.xml.sax.SAXNotSupportedException JavaDoc;
17
18 /**
19  * Defines a factory API that enables applications to configure and
20  * obtain a SAX based parser to parse XML documents.
21  *
22  * @author <a HREF="Jeff.Suttor@Sun.com">Jeff Suttor</a>
23  * @version $Revision: 1.39 $, $Date: 2004/04/20 00:22:02 $
24  */

25 public abstract class SAXParserFactory {
26     /** The default property name according to the JAXP spec */
27     private static final String JavaDoc DEFAULT_PROPERTY_NAME = "javax.xml.parsers.SAXParserFactory";
28
29     /**
30      * <p>Should Parsers be validating?</p>
31      */

32     private boolean validating = false;
33     
34     /**
35      * <p>Should Parsers be namespace aware?</p>
36      */

37     private boolean namespaceAware = false;
38     
39     /**
40      * <p>Protected constructor to force use of {@link #newInstance()}.</p>
41      */

42     protected SAXParserFactory () {
43     
44     }
45
46     /**
47      * Obtain a new instance of a <code>SAXParserFactory</code>. This
48      * static method creates a new factory instance
49      * This method uses the following ordered lookup procedure to determine
50      * the <code>SAXParserFactory</code> implementation class to
51      * load:
52      * <ul>
53      * <li>
54      * Use the <code>javax.xml.parsers.SAXParserFactory</code> system
55      * property.
56      * </li>
57      * <li>
58      * Use the properties file "lib/jaxp.properties" in the JRE directory.
59      * This configuration file is in standard <code>java.util.Properties
60      * </code> format and contains the fully qualified name of the
61      * implementation class with the key being the system property defined
62      * above.
63      *
64      * The jaxp.properties file is read only once by the JAXP implementation
65      * and it's values are then cached for future use. If the file does not exist
66      * when the first attempt is made to read from it, no further attempts are
67      * made to check for its existence. It is not possible to change the value
68      * of any property in jaxp.properties after it has been read for the first time.
69      * </li>
70      * <li>
71      * Use the Services API (as detailed in the JAR specification), if
72      * available, to determine the classname. The Services API will look
73      * for a classname in the file
74      * <code>META-INF/services/javax.xml.parsers.SAXParserFactory</code>
75      * in jars available to the runtime.
76      * </li>
77      * <li>
78      * Platform default <code>SAXParserFactory</code> instance.
79      * </li>
80      * </ul>
81      *
82      * Once an application has obtained a reference to a
83      * <code>SAXParserFactory</code> it can use the factory to
84      * configure and obtain parser instances.
85      *
86      *
87      *
88      * <h2>Tip for Trouble-shooting</h2>
89      * <p>Setting the <code>jaxp.debug</code> system property will cause
90      * this method to print a lot of debug messages
91      * to <tt>System.err</tt> about what it is doing and where it is looking at.</p>
92      *
93      * <p> If you have problems loading {@link DocumentBuilder}s, try:</p>
94      * <pre>
95      * java -Djaxp.debug=1 YourProgram ....
96      * </pre>
97      *
98      *
99      * @return A new instance of a SAXParserFactory.
100      *
101      * @exception FactoryConfigurationError if the implementation is
102      * not available or cannot be instantiated.
103      */

104
105     public static SAXParserFactory JavaDoc newInstance() {
106         try {
107             return (SAXParserFactory JavaDoc) FactoryFinder.find(
108                 /* The default property name according to the JAXP spec */
109                 "javax.xml.parsers.SAXParserFactory",
110                 /* The fallback implementation class name */
111                 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
112         } catch (FactoryFinder.ConfigurationError JavaDoc e) {
113             throw new FactoryConfigurationError JavaDoc(e.getException(),
114                                                 e.getMessage());
115         }
116     }
117     
118     /**
119      * <p>Creates a new instance of a SAXParser using the currently
120      * configured factory parameters.</p>
121      *
122      * @return A new instance of a SAXParser.
123      *
124      * @exception ParserConfigurationException if a parser cannot
125      * be created which satisfies the requested configuration.
126      * @exception SAXException for SAX errors.
127      */

128     
129     public abstract SAXParser JavaDoc newSAXParser()
130         throws ParserConfigurationException JavaDoc, SAXException JavaDoc;
131
132     
133     /**
134      * Specifies that the parser produced by this code will
135      * provide support for XML namespaces. By default the value of this is set
136      * to <code>false</code>.
137      *
138      * @param awareness true if the parser produced by this code will
139      * provide support for XML namespaces; false otherwise.
140      */

141     
142     public void setNamespaceAware(boolean awareness) {
143         this.namespaceAware = awareness;
144     }
145
146     /**
147      * Specifies that the parser produced by this code will
148      * validate documents as they are parsed. By default the value of this is
149      * set to <code>false</code>.
150      *
151      * <p>
152      * Note that "the validation" here means
153      * <a HREF="http://www.w3.org/TR/REC-xml#proc-types">a validating
154      * parser</a> as defined in the XML recommendation.
155      * In other words, it essentially just controls the DTD validation.
156      * (except the legacy two properties defined in JAXP 1.2.
157      * See <a HREF="#validationCompatibility">here</a> for more details.)
158      * </p>
159      *
160      * <p>
161      * To use modern schema languages such as W3C XML Schema or
162      * RELAX NG instead of DTD, you can configure your parser to be
163      * a non-validating parser by leaving the {@link #setValidating(boolean)}
164      * method <tt>false</tt>, then use the {@link #setSchema(Schema)}
165      * method to associate a schema to a parser.
166      * </p>
167      *
168      * @param validating true if the parser produced by this code will
169      * validate documents as they are parsed; false otherwise.
170      */

171     
172     public void setValidating(boolean validating) {
173         this.validating = validating;
174     }
175
176     /**
177      * Indicates whether or not the factory is configured to produce
178      * parsers which are namespace aware.
179      *
180      * @return true if the factory is configured to produce
181      * parsers which are namespace aware; false otherwise.
182      */

183     
184     public boolean isNamespaceAware() {
185         return namespaceAware;
186     }
187
188     /**
189      * Indicates whether or not the factory is configured to produce
190      * parsers which validate the XML content during parse.
191      *
192      * @return true if the factory is configured to produce parsers which validate
193      * the XML content during parse; false otherwise.
194      */

195     
196     public boolean isValidating() {
197         return validating;
198     }
199
200     /**
201      *
202      * <p>Sets the particular feature in the underlying implementation of
203      * org.xml.sax.XMLReader.
204      * A list of the core features and properties can be found at
205      * <a HREF="http://www.saxproject.org/">http://www.saxproject.org/</a></p>
206      *
207      * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
208      * When the feature is</p>
209      * <ul>
210      * <li>
211      * <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
212      * Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.
213      * If XML processing is limited for security reasons, it will be reported via a call to the registered
214      * {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
215      * See {@link SAXParser} <code>parse</code> methods for handler specification.
216      * </li>
217      * <li>
218      * When the feature is <code>false</code>, the implementation will processing XML according to the XML specifications without
219      * regard to possible implementation limits.
220      * </li>
221      * </ul>
222      *
223      * @param name The name of the feature to be set.
224      * @param value The value of the feature to be set.
225      *
226      * @exception ParserConfigurationException if a parser cannot
227      * be created which satisfies the requested configuration.
228      * @exception SAXNotRecognizedException When the underlying XMLReader does
229      * not recognize the property name.
230      * @exception SAXNotSupportedException When the underlying XMLReader
231      * recognizes the property name but doesn't support the
232      * property.
233      * @throws NullPointerException If the <code>name</code> parameter is null.
234      *
235      * @see org.xml.sax.XMLReader#setFeature
236      */

237     public abstract void setFeature(String JavaDoc name, boolean value)
238         throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc,
239                 SAXNotSupportedException JavaDoc;
240
241     /**
242      *
243      * <p>Returns the particular property requested for in the underlying
244      * implementation of org.xml.sax.XMLReader.</p>
245      *
246      * @param name The name of the property to be retrieved.
247      *
248      * @return Value of the requested property.
249      *
250      * @exception ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
251      * @exception SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
252      * @exception SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.
253      *
254      * @see org.xml.sax.XMLReader#getProperty
255      */

256     public abstract boolean getFeature(String JavaDoc name)
257         throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc,
258                 SAXNotSupportedException JavaDoc;
259
260
261     
262     /* <p>Get current state of canonicalization.</p>
263      *
264      * @return current state canonicalization control
265      */

266     /*
267     public boolean getCanonicalization() {
268         return canonicalState;
269     }
270     */

271     
272     /**
273      * Gets the {@link Schema} object specified through
274      * the {@link #setSchema(Schema schema)} method.
275      *
276      *
277      * @throws UnsupportedOperationException
278      * For backward compatibility, when implementations for
279      * earlier versions of JAXP is used, this exception will be
280      * thrown.
281      *
282      * @return
283      * the {@link Schema} object that was last set through
284      * the {@link #setSchema(Schema)} method, or null
285      * if the method was not invoked since a {@link SAXParserFactory}
286      * is created.
287      *
288      * @since 1.5
289      */

290     public Schema JavaDoc getSchema() {
291         throw new UnsupportedOperationException JavaDoc(
292             "This parser does not support specification \""
293             + this.getClass().getPackage().getSpecificationTitle()
294             + "\" version \""
295             + this.getClass().getPackage().getSpecificationVersion()
296             + "\""
297             );
298     }
299     
300     /** <p>Set canonicalization control to <code>true</code> or
301      * </code>false</code>.</p>
302      *
303      * @param state of canonicalization
304      */

305     /*
306     public void setCanonicalization(boolean state) {
307         canonicalState = state;
308     }
309     */

310     
311     /**
312      * <p>Set the {@link Schema} to be used by parsers created
313      * from this factory.</p>
314      *
315      * <p>When a {@link Schema} is non-null, a parser will use a validator
316      * created from it to validate documents before it passes information
317      * down to the application.</p>
318      *
319      * <p>When warnings/errors/fatal errors are found by the validator, the parser must
320      * handle them as if those errors were found by the parser itself.
321      * In other words, if the user-specified {@link org.xml.sax.ErrorHandler}
322      * is set, it must receive those errors, and if not, they must be
323      * treated according to the implementation specific
324      * default error handling rules.
325      *
326      * <p>A validator may modify the SAX event stream (for example by
327      * adding default values that were missing in documents), and a parser
328      * is responsible to make sure that the application will receive
329      * those modified event stream.</p>
330      *
331      * <p>Initialy, <code>null</code> is set as the {@link Schema}.</p>
332      *
333      * <p>This processing will take effect even if
334      * the {@link #isValidating()} method returns <code>false</code>.
335      *
336      * <p>It is an error to use
337      * the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>
338      * property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>
339      * property in conjunction with a non-null {@link Schema} object.
340      * Such configuration will cause a {@link SAXException}
341      * exception when those properties are set on a {@link SAXParser}.</p>
342      *
343      * <h4>Note for implmentors</h4>
344      * <p>
345      * A parser must be able to work with any {@link Schema}
346      * implementation. However, parsers and schemas are allowed
347      * to use implementation-specific custom mechanisms
348      * as long as they yield the result described in the specification.
349      * </p>
350      *
351      * @param schema <code>Schema</code> to use, <code>null</code> to remove a schema.
352      *
353      * @throws UnsupportedOperationException
354      * For backward compatibility, when implementations for
355      * earlier versions of JAXP is used, this exception will be
356      * thrown.
357      *
358      * @since 1.5
359      */

360     public void setSchema(Schema JavaDoc schema) {
361         throw new UnsupportedOperationException JavaDoc(
362             "This parser does not support specification \""
363             + this.getClass().getPackage().getSpecificationTitle()
364             + "\" version \""
365             + this.getClass().getPackage().getSpecificationVersion()
366             + "\""
367             );
368     }
369
370     /**
371      * <p>Set state of XInclude processing.</p>
372      *
373      * <p>If XInclude markup is found in the document instance, should it be
374      * processed as specified in <a HREF="http://www.w3.org/TR/xinclude/">
375      * XML Inclusions (XInclude) Version 1.0</a>.</p>
376      *
377      * <p>XInclude processing defaults to <code>false</code>.</p>
378      *
379      * @param state Set XInclude processing to <code>true</code> or
380      * <code>false</code>
381      *
382      * @throws UnsupportedOperationException
383      * For backward compatibility, when implementations for
384      * earlier versions of JAXP is used, this exception will be
385      * thrown.
386      *
387      * @since 1.5
388      */

389     public void setXIncludeAware(final boolean state) {
390         throw new UnsupportedOperationException JavaDoc(
391             "This parser does not support specification \""
392             + this.getClass().getPackage().getSpecificationTitle()
393             + "\" version \""
394             + this.getClass().getPackage().getSpecificationVersion()
395             + "\""
396             );
397     }
398
399     /**
400      * <p>Get state of XInclude processing.</p>
401      *
402      * @return current state of XInclude processing
403      *
404      * @throws UnsupportedOperationException
405      * For backward compatibility, when implementations for
406      * earlier versions of JAXP is used, this exception will be
407      * thrown.
408      *
409      * @since 1.5
410      */

411     public boolean isXIncludeAware() {
412         throw new UnsupportedOperationException JavaDoc(
413             "This parser does not support specification \""
414             + this.getClass().getPackage().getSpecificationTitle()
415             + "\" version \""
416             + this.getClass().getPackage().getSpecificationVersion()
417             + "\""
418             );
419     }
420 }
421
422
Popular Tags