KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > jaxp > SAXParserImpl


1 /*
2  * $Id: SAXParserImpl.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
3  *
4  * The Apache Software License, Version 1.1
5  *
6  *
7  * Copyright (c) 2000 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Xerces" and "Apache Software Foundation" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * nor may "Apache" appear in their name, without prior written
36  * permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation and was
54  * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
55  * http://www.sun.com. For more information on the Apache Software
56  * Foundation, please see <http://www.apache.org/>.
57  */

58
59 package org.enhydra.apache.xerces.jaxp;
60
61 import java.util.Enumeration JavaDoc;
62 import java.util.Hashtable JavaDoc;
63
64 import javax.xml.parsers.SAXParserFactory JavaDoc;
65
66 import org.xml.sax.HandlerBase JavaDoc;
67 import org.xml.sax.Parser JavaDoc;
68 import org.xml.sax.SAXException JavaDoc;
69 import org.xml.sax.SAXNotRecognizedException JavaDoc;
70 import org.xml.sax.SAXNotSupportedException JavaDoc;
71 import org.xml.sax.XMLReader JavaDoc;
72 import org.xml.sax.helpers.XMLReaderAdapter JavaDoc;
73
74 /**
75  * @author Rajiv Mordani
76  * @author Edwin Goei
77  * @version $Revision: 1.2 $
78  */

79
80 /**
81  * This is the implementation specific class for the
82  * <code>javax.xml.parsers.SAXParser</code>.
83  */

84 public class SAXParserImpl extends javax.xml.parsers.SAXParser JavaDoc {
85     private XMLReader JavaDoc xmlReader;
86     private Parser JavaDoc parser = null;
87
88     private boolean validating = false;
89     private boolean namespaceAware = false;
90     
91     /**
92      * Create a SAX parser with the associated features
93      * @param features Hashtable of SAX features, may be null
94      */

95     SAXParserImpl(SAXParserFactory JavaDoc spf, Hashtable JavaDoc features)
96         throws SAXException JavaDoc
97     {
98         // Instantiate a SAXParser directly and not through SAX so that we
99
// use the right ClassLoader
100
xmlReader = new org.enhydra.apache.xerces.parsers.SAXParser();
101
102         // Validation
103
validating = spf.isValidating();
104         String JavaDoc validation = "http://xml.org/sax/features/validation";
105
106         // If validating, provide a default ErrorHandler that prints
107
// validation errors with a warning telling the user to set an
108
// ErrorHandler. Note: this does not handle all cases.
109
if (validating) {
110             xmlReader.setErrorHandler(new DefaultValidationErrorHandler());
111         }
112
113         // Allow SAX parser to use a different ErrorHandler if it wants to
114
xmlReader.setFeature(validation, validating);
115
116         // "namespaceAware" == SAX Namespaces feature
117
// Note: there is a compatibility problem here with default values:
118
// JAXP default is false while SAX 2 default is true!
119
namespaceAware = spf.isNamespaceAware();
120         String JavaDoc namespaces = "http://xml.org/sax/features/namespaces";
121         xmlReader.setFeature(namespaces, namespaceAware);
122
123         // SAX "namespaces" and "namespace-prefixes" features must not both
124
// be false as specified by SAX
125
if (namespaceAware == false) {
126             String JavaDoc prefixes = "http://xml.org/sax/features/namespace-prefixes";
127             xmlReader.setFeature(prefixes, true);
128         }
129
130         setFeatures(features);
131     }
132
133     /**
134      * Set any features of our XMLReader based on any features set on the
135      * SAXParserFactory.
136      *
137      * XXX Does not handle possible conflicts between SAX feature names and
138      * JAXP specific feature names, eg. SAXParserFactory.isValidating()
139      */

140     private void setFeatures(Hashtable JavaDoc features)
141         throws SAXNotSupportedException JavaDoc, SAXNotRecognizedException JavaDoc
142     {
143         if (features != null) {
144             for (Enumeration JavaDoc e = features.keys(); e.hasMoreElements();) {
145                 String JavaDoc feature = (String JavaDoc)e.nextElement();
146                 boolean value = ((Boolean JavaDoc)features.get(feature)).booleanValue();
147                 xmlReader.setFeature(feature, value);
148             }
149         }
150     }
151
152     public Parser JavaDoc getParser() throws SAXException JavaDoc {
153         if (parser == null) {
154             // Adapt a SAX2 XMLReader into a SAX1 Parser
155
parser = new XMLReaderAdapter JavaDoc(xmlReader);
156
157             // Set a DocumentHandler that does nothing to avoid getting
158
// exceptions if no DocumentHandler is set by the app
159
parser.setDocumentHandler(new HandlerBase JavaDoc());
160         }
161         return parser;
162     }
163
164     /**
165      * Returns the XMLReader that is encapsulated by the implementation of
166      * this class.
167      */

168     public XMLReader JavaDoc getXMLReader() {
169         return xmlReader;
170     }
171
172     public boolean isNamespaceAware() {
173         return namespaceAware;
174     }
175
176     public boolean isValidating() {
177         return validating;
178     }
179
180     /**
181      * Sets the particular property in the underlying implementation of
182      * org.xml.sax.XMLReader.
183      */

184     public void setProperty(String JavaDoc name, Object JavaDoc value)
185         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
186     {
187         xmlReader.setProperty(name, value);
188     }
189
190     /**
191      * returns the particular property requested for in the underlying
192      * implementation of org.xml.sax.XMLReader.
193      */

194     public Object JavaDoc getProperty(String JavaDoc name)
195         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
196     {
197         return xmlReader.getProperty(name);
198     }
199 }
200
Popular Tags