KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > DocumentBuilderImpl


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

57
58
59 package com.sun.org.apache.xerces.internal.jaxp;
60
61 import java.io.IOException JavaDoc;
62 import java.util.Enumeration JavaDoc;
63 import java.util.Hashtable JavaDoc;
64
65 import javax.xml.parsers.DocumentBuilder JavaDoc;
66 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
67 import javax.xml.validation.Schema JavaDoc;
68
69 import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl;
70 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
71 import com.sun.org.apache.xerces.internal.impl.Constants;
72 import com.sun.org.apache.xerces.internal.parsers.DOMParser;
73 import com.sun.org.apache.xerces.internal.parsers.JAXPConfiguration;
74 import com.sun.org.apache.xerces.internal.util.SecurityManager;
75 import com.sun.org.apache.xerces.internal.xni.XNIException;
76 import org.w3c.dom.DOMImplementation JavaDoc;
77 import org.w3c.dom.Document JavaDoc;
78 import org.xml.sax.EntityResolver JavaDoc;
79 import org.xml.sax.ErrorHandler JavaDoc;
80 import org.xml.sax.InputSource JavaDoc;
81 import org.xml.sax.SAXException JavaDoc;
82 import org.xml.sax.SAXNotRecognizedException JavaDoc;
83 import org.xml.sax.SAXNotSupportedException JavaDoc;
84 import org.xml.sax.helpers.DefaultHandler JavaDoc;
85 /**
86  * @author Rajiv Mordani
87  * @author Edwin Goei
88  * @version $Id: DocumentBuilderImpl.java,v 1.24 2004/02/24 23:15:58 mrglavas Exp $
89  */

90 public class DocumentBuilderImpl extends DocumentBuilder JavaDoc
91         implements JAXPConstants
92 {
93     private EntityResolver JavaDoc er = null;
94     private ErrorHandler JavaDoc eh = null;
95     private final DOMParser domParser;
96     private boolean enableSP = true;
97     private final Schema JavaDoc grammar;
98     
99     /**
100      * null if the secure processing is disabled.
101      * otherwise a valid {@link SecureProcessing} object.
102      */

103     private final SecurityManager JavaDoc secureProcessing ;
104     private final boolean xincludeAware;
105  
106
107     protected DocumentBuilderImpl(DocumentBuilderFactory JavaDoc dbf, Hashtable JavaDoc dbfAttrs)
108           throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
109       {
110         grammar = dbf.getSchema();
111         secureProcessing = new SecurityManager JavaDoc();
112         this.domParser = new DOMParser(new JAXPConfiguration(grammar));
113         this.xincludeAware = dbf.isXIncludeAware();
114         
115         domParser.setFeature(
116                 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE,
117                 xincludeAware);
118
119         // If validating, provide a default ErrorHandler that prints
120
// validation errors with a warning telling the user to set an
121
// ErrorHandler
122
if (dbf.isValidating()) {
123             setErrorHandler(new DefaultValidationErrorHandler());
124         }
125
126         domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
127                              Constants.VALIDATION_FEATURE, dbf.isValidating());
128
129         // "namespaceAware" == SAX Namespaces feature
130
domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
131                              Constants.NAMESPACES_FEATURE,
132                              dbf.isNamespaceAware());
133
134         // Set various parameters obtained from DocumentBuilderFactory
135
domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
136                              Constants.INCLUDE_IGNORABLE_WHITESPACE,
137                              !dbf.isIgnoringElementContentWhitespace());
138         domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
139                              Constants.CREATE_ENTITY_REF_NODES_FEATURE,
140                              !dbf.isExpandEntityReferences());
141         domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
142                              Constants.INCLUDE_COMMENTS_FEATURE,
143                              !dbf.isIgnoringComments());
144         domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
145                              Constants.CREATE_CDATA_NODES_FEATURE,
146                              !dbf.isCoalescing());
147
148         setDocumentBuilderFactoryAttributes(dbfAttrs);
149         if( enableSP)
150             domParser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing);
151     }
152     
153     /**
154       * <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>
155       *
156       * <p><code>DocumentBuilder</code> is reset to the same state as when it was created with
157       * {@link DocumentBuilderFactory#newDocumentBuilder()}.
158       * <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s
159       * thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>
160       *
161       * <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}
162       * <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal
163       * <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>
164       *
165       * @since 1.5
166       */

167
168     public void reset(){
169         if(domParser != null){
170             try{
171                 //we dont need to worry about any properties being set on this object because
172
//DocumentBuilder doesn't provide any way to set the properties
173
//once it is created.
174
domParser.reset();
175             }
176             //xxx: underlying implementation reset throws XNIException what should we do in this case ?
177
//other question is why underlying implementation should throw an exception is it because
178
//of properties being set.. if there was any propery that is not being supported
179
//exception would have been thrown when setting it on the underlying implementation.
180
catch(XNIException ex){
181                 //coninue.
182
}
183         }
184     }
185     
186     /**
187      * Set any DocumentBuilderFactory attributes of our underlying DOMParser
188      *
189      * Note: code does not handle possible conflicts between DOMParser
190      * attribute names and JAXP specific attribute names,
191      * eg. DocumentBuilderFactory.setValidating()
192      */

193     private void setDocumentBuilderFactoryAttributes(Hashtable JavaDoc dbfAttrs)
194         throws SAXNotSupportedException JavaDoc, SAXNotRecognizedException JavaDoc
195     {
196         if (dbfAttrs == null) {
197             // Nothing to do
198
return;
199         }
200
201         // TODO: reroute those properties to use new JAXP1.3 API. -KK
202

203         for (Enumeration JavaDoc e = dbfAttrs.keys(); e.hasMoreElements();) {
204             String JavaDoc name = (String JavaDoc)e.nextElement();
205             Object JavaDoc val = dbfAttrs.get(name);
206             if (val instanceof Boolean JavaDoc) {
207                 // Assume feature
208
if (Constants.FEATURE_SECURE_PROCESSING.equals(name)){
209                     enableSP = ((Boolean JavaDoc)val).booleanValue();
210                 }else
211                     domParser.setFeature(name, ((Boolean JavaDoc)val).booleanValue());
212             } else {
213                 // Assume property
214
if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
215                     // JAXP 1.2 support
216
//None of the properties will take effect till the setValidating(true) has been called
217
if ( W3C_XML_SCHEMA.equals(val) ) {
218                         if( isValidating() ) {
219                             domParser.setFeature(
220                                 Constants.XERCES_FEATURE_PREFIX +
221                                 Constants.SCHEMA_VALIDATION_FEATURE, true);
222                             //also set the schema full checking to true.
223
domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
224                                      Constants.SCHEMA_FULL_CHECKING,
225                                      true);
226                             // this should allow us not to emit DTD errors, as expected by the
227
// spec when schema validation is enabled
228
domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
229                         }
230                     }
231                 } else if(JAXP_SCHEMA_SOURCE.equals(name)){
232                     if( isValidating() ) {
233                         String JavaDoc value=(String JavaDoc)dbfAttrs.get(JAXP_SCHEMA_LANGUAGE);
234                         if(value !=null && W3C_XML_SCHEMA.equals(value)){
235                             domParser.setProperty(name, val);
236                         }else{
237                             throw new IllegalArgumentException JavaDoc(
238                                 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
239                                 "jaxp-order-not-supported",
240                                 new Object JavaDoc[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
241                         }
242                     }
243                 }
244                 /*else if(name.equals(Constants.ENTITY_EXPANSION_LIMIT)){
245                     String elimit = (String)value;
246                     if(elimit != null && elimit != ""){
247                         int val = Integer.parseInt(elimit);
248                         secureProcessing.setEntityExpansionLimit(val);
249                     }
250                 }else if(name.equals(Constants.MAX_OCCUR_LIMIT)){
251                     String mlimit = (String)value;
252                     if(mlimit != null && mlimit != ""){
253                         int val = Integer.parseInt(mlimit);
254                         secureProcessing.setMaxOccurNodeLimit(val);
255                     }
256                 }*/
else {
257                     // Let Xerces code handle the property
258
domParser.setProperty(name, val);
259                 }
260             }
261         }
262     }
263
264     /**
265      * Non-preferred: use the getDOMImplementation() method instead of this
266      * one to get a DOM Level 2 DOMImplementation object and then use DOM
267      * Level 2 methods to create a DOM Document object.
268      */

269     public Document JavaDoc newDocument() {
270         return new com.sun.org.apache.xerces.internal.dom.DocumentImpl();
271     }
272
273     public DOMImplementation JavaDoc getDOMImplementation() {
274         return DOMImplementationImpl.getDOMImplementation();
275     }
276
277     public Document JavaDoc parse(InputSource JavaDoc is) throws SAXException JavaDoc, IOException JavaDoc {
278         if (is == null) {
279             throw new IllegalArgumentException JavaDoc(
280                 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
281                 "jaxp-null-input-source", null));
282         }
283
284         if (er != null) {
285             domParser.setEntityResolver(er);
286         }
287
288         if (eh != null) {
289             domParser.setErrorHandler(eh);
290         }
291
292         domParser.parse(is);
293         return domParser.getDocument();
294     }
295
296     public boolean isNamespaceAware() {
297         try {
298             return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
299                                         Constants.NAMESPACES_FEATURE);
300         } catch (SAXException JavaDoc x) {
301             throw new IllegalStateException JavaDoc(x.getMessage());
302         }
303     }
304
305     public boolean isValidating() {
306         try {
307             return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
308                                         Constants.VALIDATION_FEATURE);
309         } catch (SAXException JavaDoc x) {
310             throw new IllegalStateException JavaDoc(x.getMessage());
311         }
312     }
313
314     public void setEntityResolver(org.xml.sax.EntityResolver JavaDoc er) {
315         this.er = er;
316     }
317
318     public void setErrorHandler(org.xml.sax.ErrorHandler JavaDoc eh) {
319         // If app passes in a ErrorHandler of null, then ignore all errors
320
// and warnings
321
this.eh = (eh == null) ? new DefaultHandler JavaDoc() : eh;
322     }
323
324     /** <p>Get a reference to the the <code>GrammarCache</code> being used by
325      * the XML processor.</p>
326      *
327      * <p>If no cache is being used, <code>null</code> is returned.</p>
328      *
329      * @return <code>GrammarCache</code> being used or <code>null</code>
330      * if none in use
331      */

332     
333     public Schema JavaDoc getSchema(){
334         return grammar;
335     }
336     
337     public boolean isXIncludeAware() {
338          return xincludeAware;
339     }
340
341     final DOMParser getDOMParser() {
342           return domParser;
343     }
344     
345 }
346
Popular Tags