KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > xml > apache > DocumentBuilderImpl


1 /*
2  * $Id: DocumentBuilderImpl.java,v 1.1 2002/02/15 23:44:28 skavish 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.openlaszlo.iv.flash.xml.apache;
61
62 import java.io.IOException JavaDoc;
63 import javax.xml.parsers.DocumentBuilder JavaDoc;
64 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
65 import javax.xml.parsers.ParserConfigurationException JavaDoc;
66
67 import org.w3c.dom.Document JavaDoc;
68 import org.w3c.dom.DOMImplementation JavaDoc;
69 import org.w3c.dom.DocumentType JavaDoc;
70
71 import org.xml.sax.XMLReader JavaDoc;
72 import org.xml.sax.InputSource JavaDoc;
73 import org.xml.sax.SAXException JavaDoc;
74 import org.xml.sax.SAXParseException JavaDoc;
75 import org.xml.sax.EntityResolver JavaDoc;
76 import org.xml.sax.ErrorHandler JavaDoc;
77 import org.xml.sax.helpers.DefaultHandler JavaDoc;
78
79 import org.apache.xerces.parsers.DOMParser;
80 import org.apache.xerces.dom.DOMImplementationImpl;
81
82 /**
83  * Standard DocumentBuilderImpl from apache.
84  * <p>
85  * Modifications for JGenerator:<br>
86  * - ignore fatal errors in parser<br>
87  * - pool of DOM parser with locking and releasing<br>
88  *
89  * @author Dmitry Skavish
90  * @author Rajiv Mordani
91  * @author Edwin Goei
92  */

93 public class DocumentBuilderImpl extends DocumentBuilder JavaDoc {
94
95     /** Size of parser's pool */
96     private static final int MAX_PARSERS = 5;
97
98     /* Xerces features */
99     private static final String JavaDoc XERCES_FEATURE_PREFIX = "http://apache.org/xml/features/";
100     private static final String JavaDoc CREATE_ENTITY_REF_NODES_FEATURE = "dom/create-entity-ref-nodes";
101     private static final String JavaDoc INCLUDE_IGNORABLE_WHITESPACE = "dom/include-ignorable-whitespace";
102     private static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR = "continue-after-fatal-error";
103     private static final String JavaDoc VALIDATING_PARSER = "http://xml.org/sax/features/validation";
104     private static final String JavaDoc NAMESPACE_AWARE = "http://xml.org/sax/features/namespaces";
105
106     private DocumentBuilderFactory JavaDoc dbf;
107
108     private EntityResolver JavaDoc er = null;
109     private ErrorHandler JavaDoc eh = null;
110
111     /* Parsers' pool */
112     private DOMParser[] parsers = new DOMParser[MAX_PARSERS];
113     private boolean[] locks = new boolean[MAX_PARSERS];
114
115     private boolean namespaceAware = false;
116     private boolean validating = false;
117
118     DocumentBuilderImpl(DocumentBuilderFactory JavaDoc dbf)
119         throws ParserConfigurationException JavaDoc
120     {
121         this.dbf = dbf;
122         this.validating = dbf.isValidating();
123         this.namespaceAware = dbf.isNamespaceAware();
124     }
125
126     /**
127      * Non-preferred: use the getDOMImplementation() method instead of this
128      * one to get a DOM Level 2 DOMImplementation object and then use DOM
129      * Level 2 methods to create a DOM Document object.
130      */

131     public Document JavaDoc newDocument() {
132         return new org.apache.xerces.dom.DocumentImpl();
133     }
134
135     public DOMImplementation JavaDoc getDOMImplementation() {
136         return DOMImplementationImpl.getDOMImplementation();
137     }
138
139     public Document JavaDoc parse(InputSource JavaDoc is) throws SAXException JavaDoc, IOException JavaDoc {
140         if( is == null ) {
141             throw new IllegalArgumentException JavaDoc("InputSource cannot be null");
142         }
143
144         DOMParser parser = lockDOMParser();
145         try {
146             parser.parse(is);
147             Document JavaDoc doc = parser.getDocument();
148             return doc;
149         } finally {
150             releaseDOMParser(parser);
151         }
152     }
153
154     public boolean isNamespaceAware() {
155         return namespaceAware;
156     }
157
158     public boolean isValidating() {
159         return validating;
160     }
161
162     public void setEntityResolver( org.xml.sax.EntityResolver JavaDoc er ) {
163         this.er = er;
164     }
165
166     public void setErrorHandler( org.xml.sax.ErrorHandler JavaDoc eh ) {
167         // If app passes in a ErrorHandler of null, then ignore all errors
168
// and warnings
169
this.eh = (eh == null) ? new DefaultHandler JavaDoc() : eh;
170     }
171
172     /**
173      * Create DOM parser
174      *
175      * @return created parser
176      * @exception ParserConfigurationException
177      */

178     private DOMParser createDOMParser() throws SAXException JavaDoc {
179         DOMParser domParser = new DOMParser();
180
181         domParser.setFeature(VALIDATING_PARSER, false);
182
183         // "namespaceAware" == SAX Namespaces feature
184
domParser.setFeature(NAMESPACE_AWARE, namespaceAware);
185
186         // Set various parameters obtained from DocumentBuilderFactory
187
domParser.setFeature(XERCES_FEATURE_PREFIX+INCLUDE_IGNORABLE_WHITESPACE,
188                              !dbf.isIgnoringElementContentWhitespace());
189         domParser.setFeature(XERCES_FEATURE_PREFIX+CREATE_ENTITY_REF_NODES_FEATURE,
190                              !dbf.isExpandEntityReferences());
191         domParser.setFeature(XERCES_FEATURE_PREFIX+CONTINUE_AFTER_FATAL_ERROR, true );
192
193         // XXX No way to control dbf.isIgnoringComments() or
194
// dbf.isCoalescing()
195

196         if( er != null ) domParser.setEntityResolver(er);
197         if( eh != null ) domParser.setErrorHandler(eh);
198
199         return domParser;
200     }
201
202     private synchronized DOMParser lockDOMParser() throws SAXException JavaDoc {
203         for(;;) {
204             for( int i=0; i<MAX_PARSERS; i++ ) {
205                 if( !locks[i] ) {
206                     if( parsers[i] == null ) {
207                         parsers[i] = createDOMParser();
208                     }
209                     locks[i] = true;
210                     return parsers[i];
211                 }
212             }
213             try {
214                 wait();
215             } catch( InterruptedException JavaDoc e ) {
216             }
217         }
218     }
219
220     private synchronized void releaseDOMParser( DOMParser p ) {
221         for( int i=0; i<MAX_PARSERS; i++ ) {
222             if( parsers[i] == p ) {
223                 locks[i] = false;
224                 notify();
225                 break;
226             }
227         }
228     }
229
230 }
231
Popular Tags