KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > SAXParserFactoryImpl


1 /*
2  * Copyright 2000-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.jaxp;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.xml.XMLConstants JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import javax.xml.parsers.SAXParserFactory JavaDoc;
25 import javax.xml.validation.Schema JavaDoc;
26
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.SAXNotRecognizedException JavaDoc;
29 import org.xml.sax.SAXNotSupportedException JavaDoc;
30
31 /**
32  * This is the implementation specific class for the
33  * <code>javax.xml.parsers.SAXParserFactory</code>. This is the platform
34  * default implementation for the platform.
35  *
36  * @author Rajiv Mordani
37  * @author Edwin Goei
38  *
39  * @version $Id: SAXParserFactoryImpl.java,v 1.12 2005/06/10 03:20:41 mrglavas Exp $
40  */

41 public class SAXParserFactoryImpl extends SAXParserFactory JavaDoc {
42     private Hashtable JavaDoc features;
43     private Schema JavaDoc grammar;
44     private boolean isXIncludeAware;
45     
46     /**
47      * State of the secure processing feature, initially <code>false</code>
48      */

49     private boolean fSecureProcess = false;
50
51     /**
52      * Creates a new instance of <code>SAXParser</code> using the currently
53      * configured factory parameters.
54      * @return javax.xml.parsers.SAXParser
55      */

56     public SAXParser JavaDoc newSAXParser()
57         throws ParserConfigurationException JavaDoc
58     {
59         SAXParser JavaDoc saxParserImpl;
60         try {
61             saxParserImpl = new SAXParserImpl(this, features, fSecureProcess);
62         } catch (SAXException JavaDoc se) {
63             // Translate to ParserConfigurationException
64
throw new ParserConfigurationException JavaDoc(se.getMessage());
65         }
66     return saxParserImpl;
67     }
68
69     /**
70      * Common code for translating exceptions
71      */

72     private SAXParserImpl newSAXParserImpl()
73         throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc,
74         SAXNotSupportedException JavaDoc
75     {
76         SAXParserImpl saxParserImpl;
77         try {
78             saxParserImpl = new SAXParserImpl(this, features);
79         } catch (SAXNotSupportedException JavaDoc e) {
80             throw e;
81         } catch (SAXNotRecognizedException JavaDoc e) {
82             throw e;
83         } catch (SAXException JavaDoc se) {
84             throw new ParserConfigurationException JavaDoc(se.getMessage());
85         }
86         return saxParserImpl;
87     }
88
89     /**
90      * Sets the particular feature in the underlying implementation of
91      * org.xml.sax.XMLReader.
92      */

93     public void setFeature(String JavaDoc name, boolean value)
94         throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc,
95         SAXNotSupportedException JavaDoc {
96         if (name == null) {
97             throw new NullPointerException JavaDoc();
98         }
99         // If this is the secure processing feature, save it then return.
100
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
101             fSecureProcess = value;
102             return;
103         }
104         
105         // XXX This is ugly. We have to collect the features and then
106
// later create an XMLReader to verify the features.
107
if (features == null) {
108             features = new Hashtable JavaDoc();
109         }
110         features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
111
112         // Test the feature by possibly throwing SAX exceptions
113
try {
114             newSAXParserImpl();
115         } catch (SAXNotSupportedException JavaDoc e) {
116             features.remove(name);
117             throw e;
118         } catch (SAXNotRecognizedException JavaDoc e) {
119             features.remove(name);
120             throw e;
121         }
122     }
123
124     /**
125      * returns the particular property requested for in the underlying
126      * implementation of org.xml.sax.XMLReader.
127      */

128     public boolean getFeature(String JavaDoc name)
129         throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc,
130         SAXNotSupportedException JavaDoc {
131         if (name == null) {
132             throw new NullPointerException JavaDoc();
133         }
134         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
135             return fSecureProcess;
136         }
137         // Check for valid name by creating a dummy XMLReader to get
138
// feature value
139
return newSAXParserImpl().getXMLReader().getFeature(name);
140     }
141     
142     public Schema JavaDoc getSchema() {
143         return grammar;
144     }
145
146     public void setSchema(Schema JavaDoc grammar) {
147         this.grammar = grammar;
148     }
149
150     public boolean isXIncludeAware() {
151         return this.isXIncludeAware;
152     }
153
154     public void setXIncludeAware(boolean state) {
155         this.isXIncludeAware = state;
156     }
157 }
158
Popular Tags