KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > dom > DOMParser


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

57
58 package com.sun.org.apache.xerces.internal.impl.xs.dom;
59
60 import com.sun.org.apache.xerces.internal.parsers.NonValidatingConfiguration;
61 import com.sun.org.apache.xerces.internal.impl.Constants;
62 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
63 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
64 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
65 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
66 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
67 import com.sun.org.apache.xerces.internal.xni.QName;
68 import com.sun.org.apache.xerces.internal.xni.Augmentations;
69 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
70 import com.sun.org.apache.xerces.internal.xni.XMLString;
71 import com.sun.org.apache.xerces.internal.xni.XNIException;
72 import com.sun.org.apache.xerces.internal.util.XMLChar;
73
74 import org.w3c.dom.Element JavaDoc;
75
76 /**
77  * A dom parser used to parse schema documents into DOM trees
78  *
79  * @author Sandy Gao, IBM
80  *
81  * @version $Id: DOMParser.java,v 1.9 2002/12/11 16:01:18 sandygao Exp $
82  */

83 public class DOMParser extends com.sun.org.apache.xerces.internal.parsers.DOMParser {
84
85     /** Property identifier: entity manager. */
86     protected static final String JavaDoc ENTITY_MANAGER =
87         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
88     
89     /** Property identifier: DOM document class name. */
90     protected static final String JavaDoc DOCUMENT_CLASS =
91         Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_CLASS_NAME_PROPERTY;
92     
93     /** Feature identifier: DOM Defer node expansion. */
94     protected static final String JavaDoc DEFER_EXPANSION =
95         Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE;
96     
97     /** Property identifier: error reporter. */
98     public static final String JavaDoc ERROR_REPORTER =
99     Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
100
101     // the locator containing line/column information
102
protected XMLLocator fLocator;
103     
104     // our own document implementation, which knows how to create Element
105
// with line/column information
106
public DocumentImpl fDocumentImpl;
107
108     private DOMNodePool fNodePool;
109     
110     //
111
// Constructors
112
//
113

114     /**
115      * Constructs a DOM parser using the dtd/xml schema parser configuration.
116      */

117     public DOMParser() {
118         // REVISIT: should we use a new configuration with scannerNS->dom API with
119
// no dtd scanners/valitors..?
120
//
121
super(new NonValidatingConfiguration());
122         try {
123             // use our own document implementation
124
setProperty(DOCUMENT_CLASS, "com.sun.org.apache.xerces.internal.impl.xs.dom.DocumentImpl");
125             // don't defer DOM expansion
126
setFeature(DEFER_EXPANSION, false);
127
128         }
129         catch (Exception JavaDoc e) {
130         }
131         fNodePool = new DOMNodePool();
132     } // <init>()
133

134     /**
135      * Resets the node pool.
136      */

137     public void resetNodePool() {
138         fNodePool.reset();
139     }
140
141     /**
142      * The start of the document.
143      *
144      * @param locator The system identifier of the entity if the entity
145      * is external, null otherwise.
146      * @param encoding The auto-detected IANA encoding name of the entity
147      * stream. This value will be null in those situations
148      * where the entity encoding is not auto-detected (e.g.
149      * internal entities or a document entity that is
150      * parsed from a java.io.Reader).
151      * @param namespaceContext
152      * The namespace context in effect at the
153      * start of this document.
154      * This object represents the current context.
155      * Implementors of this class are responsible
156      * for copying the namespace bindings from the
157      * the current context (and its parent contexts)
158      * if that information is important.
159      * @param augs Additional information that may include infoset augmentations
160      *
161      * @throws XNIException Thrown by handler to signal an error.
162      */

163     public void startDocument(XMLLocator locator, String JavaDoc encoding,
164                               NamespaceContext namespaceContext, Augmentations augs)
165         throws XNIException {
166
167         super.startDocument(locator, encoding, namespaceContext, augs);
168         // get a handle to the document created
169
fDocumentImpl = (DocumentImpl)super.fDocumentImpl;
170         fDocumentImpl.fNodePool=fNodePool;
171         fLocator = locator;
172
173     } // startDocument(XMLLocator,String,Augmentations)
174

175     // Where xs:appinfo or xs:documentation starts;
176
// -1 means not in the scope of either of the two elements.
177
private int fAnnotationDepth = -1;
178     // The current element depth
179
private int fDepth = -1;
180     // Use to report the error when characters are not allowed.
181
XMLErrorReporter fErrorReporter;
182
183     // override startElement method to check whether it's one of
184
// xs:appinfo or xs:documentation
185
public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
186         throws XNIException {
187         super.startElement(element, attributes, augs);
188         fDepth++;
189         // if it's not within either element, check whether it's one of them
190
// if so, record the current depth, so that any element with larger
191
// depth is allowed to have character data.
192
if (fAnnotationDepth == -1) {
193             if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
194                 (element.localpart == SchemaSymbols.ELT_APPINFO ||
195                  element.localpart == SchemaSymbols.ELT_DOCUMENTATION)) {
196                 fAnnotationDepth = fDepth;
197             }
198         }
199     }
200     
201     // override this method to check whether there are non-whitespace characters
202
public void characters(XMLString text, Augmentations augs) throws XNIException {
203         // when it's not within xs:appinfo or xs:documentation
204
if (fAnnotationDepth == -1) {
205             for (int i=text.offset; i<text.offset+text.length; i++) {
206                 // and there is a non-whitespace character
207
if (!XMLChar.isSpace(text.ch[i])) {
208                     // only get the error reporter when reporting an error
209
if (fErrorReporter == null) {
210                         try {
211                             fErrorReporter = (XMLErrorReporter)getProperty(ERROR_REPORTER);
212                         } catch (Exception JavaDoc e) {
213                             //ignore the excpetion
214
}
215                         if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
216                             XSMessageFormatter xmft = new XSMessageFormatter();
217                             fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
218                         }
219                     }
220                     // the string we saw: starting from the first non-whitespace character.
221
String JavaDoc txt = new String JavaDoc(text.ch, i, text.length+text.offset-i);
222                     // report an error
223
fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
224                                                "s4s-elt-character",
225                                                new Object JavaDoc[]{txt},
226                                                XMLErrorReporter.SEVERITY_ERROR);
227                     break;
228                 }
229             }
230             // don't call super.characters() when it's not within one of the 2
231
// annotation elements: the traversers ignore them anyway. We can
232
// save time/memory creating the text nodes.
233
}
234         // when it's not within either of the 2 elements, characters are allowed
235
// and we need to call super.characters().
236
else {
237             super.characters(text, augs);
238         }
239     }
240     
241     // override this method to update the depth variables
242
public void endElement(QName element, Augmentations augs) throws XNIException {
243         super.endElement(element, augs);
244         // when we reach the endElement of xs:appinfo or xs:documentation,
245
// change fAnnotationDepth to -1
246
if (fAnnotationDepth == fDepth)
247             fAnnotationDepth = -1;
248         fDepth--;
249     }
250
251     // override this method to store line/column information in Element created
252
protected Element JavaDoc createElementNode(QName element) {
253         // create an element containing line/column information
254
return fDocumentImpl.createElementNS(element.uri, element.rawname,
255                                              element.localpart,
256                                              fLocator.getLineNumber(),
257                                              fLocator.getColumnNumber());
258     }
259
260 } // class DOMParser
261
Popular Tags