KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DocumentBuilderImpl.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
60 package org.enhydra.apache.xerces.jaxp;
61
62 import java.io.IOException JavaDoc;
63 import java.util.Enumeration JavaDoc;
64 import java.util.Hashtable JavaDoc;
65
66 import javax.xml.parsers.DocumentBuilder JavaDoc;
67 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
68
69 import org.enhydra.apache.xerces.dom.DOMImplementationImpl;
70 import org.enhydra.apache.xerces.parsers.DOMParser;
71 import org.w3c.dom.DOMImplementation JavaDoc;
72 import org.w3c.dom.Document JavaDoc;
73 import org.xml.sax.EntityResolver JavaDoc;
74 import org.xml.sax.ErrorHandler JavaDoc;
75 import org.xml.sax.InputSource JavaDoc;
76 import org.xml.sax.SAXException JavaDoc;
77 import org.xml.sax.SAXNotRecognizedException JavaDoc;
78 import org.xml.sax.SAXNotSupportedException JavaDoc;
79 import org.xml.sax.helpers.DefaultHandler JavaDoc;
80
81 /**
82  * @author Rajiv Mordani
83  * @author Edwin Goei
84  * @version $Revision: 1.2 $
85  */

86 public class DocumentBuilderImpl extends DocumentBuilder JavaDoc {
87     /** Xerces features */
88     static final String JavaDoc XERCES_FEATURE_PREFIX =
89                                         "http://apache.org/xml/features/";
90     static final String JavaDoc CREATE_ENTITY_REF_NODES_FEATURE =
91                                         "dom/create-entity-ref-nodes";
92     static final String JavaDoc INCLUDE_IGNORABLE_WHITESPACE =
93                                         "dom/include-ignorable-whitespace";
94
95     private EntityResolver JavaDoc er = null;
96     private ErrorHandler JavaDoc eh = null;
97     private DOMParser domParser = null;
98
99     private boolean namespaceAware = false;
100     private boolean validating = false;
101
102     DocumentBuilderImpl(DocumentBuilderFactory JavaDoc dbf, Hashtable JavaDoc dbfAttrs)
103         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
104     {
105         domParser = new DOMParser();
106
107         // Validation
108
validating = dbf.isValidating();
109         String JavaDoc validation = "http://xml.org/sax/features/validation";
110         domParser.setFeature(validation, validating);
111
112         // If validating, provide a default ErrorHandler that prints
113
// validation errors with a warning telling the user to set an
114
// ErrorHandler
115
if (validating) {
116             setErrorHandler(new DefaultValidationErrorHandler());
117         }
118
119         // "namespaceAware" == SAX Namespaces feature
120
namespaceAware = dbf.isNamespaceAware();
121         domParser.setFeature("http://xml.org/sax/features/namespaces",
122                              namespaceAware);
123
124         // Set various parameters obtained from DocumentBuilderFactory
125
domParser.setFeature(XERCES_FEATURE_PREFIX +
126                              INCLUDE_IGNORABLE_WHITESPACE,
127                              !dbf.isIgnoringElementContentWhitespace());
128         domParser.setFeature(XERCES_FEATURE_PREFIX +
129                              CREATE_ENTITY_REF_NODES_FEATURE,
130                              !dbf.isExpandEntityReferences());
131
132         // XXX No way to control dbf.isIgnoringComments() or
133
// dbf.isCoalescing()
134

135         setDocumentBuilderFactoryAttributes(dbfAttrs);
136     }
137
138     /**
139      * Set any DocumentBuilderFactory attributes of our underlying DOMParser
140      *
141      * Note: code does not handle possible conflicts between DOMParser
142      * attribute names and JAXP specific attribute names,
143      * eg. DocumentBuilderFactory.setValidating()
144      */

145     private void setDocumentBuilderFactoryAttributes(Hashtable JavaDoc dbfAttrs)
146         throws SAXNotSupportedException JavaDoc, SAXNotRecognizedException JavaDoc
147     {
148         if (dbfAttrs != null) {
149             for (Enumeration JavaDoc e = dbfAttrs.keys(); e.hasMoreElements();) {
150                 String JavaDoc name = (String JavaDoc)e.nextElement();
151                 Object JavaDoc val = dbfAttrs.get(name);
152                 if (val instanceof Boolean JavaDoc) {
153                     // Assume feature
154
domParser.setFeature(name, ((Boolean JavaDoc)val).booleanValue());
155                 } else {
156                     // Assume property
157
domParser.setProperty(name, val);
158                 }
159             }
160         }
161     }
162
163     /**
164      * Non-preferred: use the getDOMImplementation() method instead of this
165      * one to get a DOM Level 2 DOMImplementation object and then use DOM
166      * Level 2 methods to create a DOM Document object.
167      */

168     public Document JavaDoc newDocument() {
169         return new org.enhydra.apache.xerces.dom.DocumentImpl();
170     }
171
172     public DOMImplementation JavaDoc getDOMImplementation() {
173         return DOMImplementationImpl.getDOMImplementation();
174     }
175
176     public Document JavaDoc parse(InputSource JavaDoc is) throws SAXException JavaDoc, IOException JavaDoc {
177         if (is == null) {
178             throw new IllegalArgumentException JavaDoc("InputSource cannot be null");
179         }
180
181         if (er != null) {
182             domParser.setEntityResolver(er);
183         }
184
185         if (eh != null) {
186             domParser.setErrorHandler(eh);
187         }
188
189         domParser.parse(is);
190         return domParser.getDocument();
191     }
192
193     public boolean isNamespaceAware() {
194         return namespaceAware;
195     }
196
197     public boolean isValidating() {
198         return validating;
199     }
200
201     public void setEntityResolver(org.xml.sax.EntityResolver JavaDoc er) {
202         this.er = er;
203     }
204
205     public void setErrorHandler(org.xml.sax.ErrorHandler JavaDoc eh) {
206         // If app passes in a ErrorHandler of null, then ignore all errors
207
// and warnings
208
this.eh = (eh == null) ? new DefaultHandler JavaDoc() : eh;
209     }
210
211     // package private
212
DOMParser getDOMParser() {
213         return domParser;
214     }
215 }
216
Popular Tags