KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > opti > SchemaDOMParser


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 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) 2001, 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.opti;
59
60
61 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
62 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
63 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
64 import com.sun.org.apache.xerces.internal.util.XMLChar;
65 import com.sun.org.apache.xerces.internal.xni.Augmentations;
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.XMLAttributes;
69 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
70 import com.sun.org.apache.xerces.internal.xni.XMLString;
71 import com.sun.org.apache.xerces.internal.xni.XNIException;
72 import org.w3c.dom.Document JavaDoc;
73
74 /**
75  * {@link XMLDocumentHandler} that
76  * builds DOM representation for XML Schema documents.
77  *
78  * <p>
79  * TODO: this class should be better renamed to
80  * SchemaDOMBuilder since this class is not a "parser", which is used
81  * to imply software that reads raw Unicode and angle brackets.
82  *
83  * @author Rahul Srivastava, Sun Microsystems Inc.
84  * @author Sandy Gao, IBM
85  *
86  * @version $Id: SchemaDOMParser.java,v 1.8 2004/04/30 02:42:43 mrglavas Exp $
87  */

88 public class SchemaDOMParser extends DefaultXMLDocumentHandler {
89
90     //
91
// Data
92
//
93

94     // the locator containing line/column information
95
protected XMLLocator fLocator;
96
97     // namespace context, needed for producing
98
// representations of annotations
99
protected NamespaceContext fNamespaceContext = null;
100
101     SchemaDOM schemaDOM;
102    
103
104     //
105
// Constructors
106
//
107

108     public SchemaDOMParser(XMLErrorReporter errorReporter) {
109         fErrorReporter = errorReporter;
110     }
111     
112     // where an annotation element itself begins
113
// -1 means not in an annotation's scope
114
private int fAnnotationDepth = -1;
115     // Where xs:appinfo or xs:documentation starts;
116
// -1 means not in the scope of either of the two elements.
117
private int fInnerAnnotationDepth = -1;
118     // The current element depth
119
private int fDepth = -1;
120     // Use to report the error when characters are not allowed.
121
XMLErrorReporter fErrorReporter;
122
123
124     //
125
// XMLDocumentHandler methods
126
//
127

128     public void startDocument(XMLLocator locator, String JavaDoc encoding,
129                               NamespaceContext namespaceContext, Augmentations augs)
130         throws XNIException {
131         schemaDOM = new SchemaDOM();
132         fAnnotationDepth = -1;
133         fInnerAnnotationDepth = -1;
134         fDepth = -1;
135         fLocator = locator;
136         fNamespaceContext = namespaceContext;
137     } // startDocument(XMLLocator,String,NamespaceContext, Augmentations)
138

139     /**
140      * The end of the document.
141      * @param augs Additional information that may include infoset augmentations
142      *
143      * @throws XNIException Thrown by handler to signal an error.
144      */

145     public void endDocument(Augmentations augs) throws XNIException {
146         // To debug the DOM created uncomment the line below
147
// schemaDOM.printDOM();
148
} // endDocument()
149

150
151     /**
152      * A comment.
153      *
154      * @param text The text in the comment.
155      * @param augs Additional information that may include infoset augmentations
156      *
157      * @exception XNIException
158      * Thrown by application to signal an error.
159      */

160     public void comment(XMLString text, Augmentations augs) throws XNIException {
161         if(fAnnotationDepth > -1) {
162             schemaDOM.comment(text);
163         }
164     }
165
166     /**
167      * A processing instruction. Processing instructions consist of a
168      * target name and, optionally, text data. The data is only meaningful
169      * to the application.
170      * <p>
171      * Typically, a processing instruction's data will contain a series
172      * of pseudo-attributes. These pseudo-attributes follow the form of
173      * element attributes but are <strong>not</strong> parsed or presented
174      * to the application as anything other than text. The application is
175      * responsible for parsing the data.
176      *
177      * @param target The target.
178      * @param data The data or null if none specified.
179      * @param augs Additional information that may include infoset augmentations
180      *
181      * @exception XNIException
182      * Thrown by handler to signal an error.
183      */

184     public void processingInstruction(String JavaDoc target, XMLString data, Augmentations augs)
185         throws XNIException {
186         if(fAnnotationDepth > -1) {
187             schemaDOM.processingInstruction(target, data.toString());
188         }
189     }
190
191     /**
192      * Character content.
193      *
194      * @param text The content.
195      * @param augs Additional information that may include infoset augmentations
196      *
197      * @exception XNIException
198      * Thrown by handler to signal an error.
199      */

200     public void characters(XMLString text, Augmentations augs) throws XNIException {
201         // when it's not within xs:appinfo or xs:documentation
202
if (fInnerAnnotationDepth == -1 ) {
203             for (int i=text.offset; i<text.offset+text.length; i++) {
204                 // and there is a non-whitespace character
205
if (!XMLChar.isSpace(text.ch[i])) {
206                     // the string we saw: starting from the first non-whitespace character.
207
String JavaDoc txt = new String JavaDoc(text.ch, i, text.length+text.offset-i);
208                     // report an error
209
fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
210                                                "s4s-elt-character",
211                                                new Object JavaDoc[]{txt},
212                                                XMLErrorReporter.SEVERITY_ERROR);
213                     break;
214                 }
215             }
216             // don't call super.characters() when it's not within one of the 2
217
// annotation elements: the traversers ignore them anyway. We can
218
// save time/memory creating the text nodes.
219
}
220         // when it's within either of the 2 elements, characters are allowed
221
// and we need to store them.
222
else {
223             schemaDOM.characters(text);
224         }
225
226     }
227
228
229     /**
230      * The start of an element.
231      *
232      * @param element The name of the element.
233      * @param attributes The element attributes.
234      * @param augs Additional information that may include infoset augmentations
235      *
236      * @exception XNIException
237      * Thrown by handler to signal an error.
238      */

239     public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
240         throws XNIException {
241
242         fDepth++;
243         // while it is true that non-whitespace character data
244
// may only occur in appInfo or documentation
245
// elements, it's certainly legal for comments and PI's to
246
// occur as children of annotation; we need
247
// to account for these here.
248
if (fAnnotationDepth == -1) {
249             if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
250                     element.localpart == SchemaSymbols.ELT_ANNOTATION) {
251                 fAnnotationDepth = fDepth;
252                 schemaDOM.startAnnotation(element, attributes, fNamespaceContext);
253             }
254         } else if(fDepth == fAnnotationDepth+1) {
255             fInnerAnnotationDepth = fDepth;
256             schemaDOM.startAnnotationElement(element, attributes);
257         } else {
258             schemaDOM.startAnnotationElement(element, attributes);
259             // avoid falling through; don't call startElement in this case
260
return;
261         }
262         schemaDOM.startElement(element, attributes,
263                                fLocator.getLineNumber(),
264                                fLocator.getColumnNumber());
265
266     }
267
268
269     /**
270      * An empty element.
271      *
272      * @param element The name of the element.
273      * @param attributes The element attributes.
274      * @param augs Additional information that may include infoset augmentations
275      *
276      * @exception XNIException
277      * Thrown by handler to signal an error.
278      */

279     public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
280         throws XNIException {
281         // the order of events that occurs here is:
282
// schemaDOM.startAnnotation/startAnnotationElement (if applicable)
283
// schemaDOM.emptyElement (basically the same as startElement then endElement)
284
// schemaDOM.endAnnotationElement (if applicable)
285
// the order of events that would occur if this was <element></element>:
286
// schemaDOM.startAnnotation/startAnnotationElement (if applicable)
287
// schemaDOM.startElement
288
// schemaDOM.endAnnotationElement (if applicable)
289
// schemaDOM.endElementElement
290
// Thus, we can see that the order of events isn't the same. However, it doesn't
291
// seem to matter. -- PJM
292
if (fAnnotationDepth == -1) {
293             // this is messed up, but a case to consider:
294
if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
295                     element.localpart == SchemaSymbols.ELT_ANNOTATION) {
296                 schemaDOM.startAnnotation(element, attributes, fNamespaceContext);
297             }
298         } else {
299             schemaDOM.startAnnotationElement(element, attributes);
300         }
301         
302         schemaDOM.emptyElement(element, attributes,
303                                fLocator.getLineNumber(),
304                                fLocator.getColumnNumber());
305         
306         if (fAnnotationDepth == -1) {
307             // this is messed up, but a case to consider:
308
if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
309                     element.localpart == SchemaSymbols.ELT_ANNOTATION) {
310                 schemaDOM.endAnnotationElement(element, true);
311             }
312         } else {
313             schemaDOM.endAnnotationElement(element, false);
314         }
315     }
316
317
318     /**
319      * The end of an element.
320      *
321      * @param element The name of the element.
322      * @param augs Additional information that may include infoset augmentations
323      *
324      * @exception XNIException
325      * Thrown by handler to signal an error.
326      */

327     public void endElement(QName element, Augmentations augs) throws XNIException {
328
329         // when we reach the endElement of xs:appinfo or xs:documentation,
330
// change fInnerAnnotationDepth to -1
331
if(fAnnotationDepth > -1) {
332             if (fInnerAnnotationDepth == fDepth) {
333                 fInnerAnnotationDepth = -1;
334                 schemaDOM.endAnnotationElement(element, false);
335                 schemaDOM.endElement();
336             } else if (fAnnotationDepth == fDepth) {
337                 fAnnotationDepth = -1;
338                 schemaDOM.endAnnotationElement(element, true);
339                 schemaDOM.endElement();
340             } else { // inside a child of annotation
341
schemaDOM.endAnnotationElement(element, false);
342             }
343         } else { // not in an annotation at all
344
schemaDOM.endElement();
345         }
346         fDepth--;
347
348     }
349     
350     /**
351      * Ignorable whitespace. For this method to be called, the document
352      * source must have some way of determining that the text containing
353      * only whitespace characters should be considered ignorable. For
354      * example, the validator can determine if a length of whitespace
355      * characters in the document are ignorable based on the element
356      * content model.
357      *
358      * @param text The ignorable whitespace.
359      * @param augs Additional information that may include infoset augmentations
360      *
361      * @exception XNIException
362      * Thrown by handler to signal an error.
363      */

364     public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
365         // unlikely to be called, but you never know...
366
if (fAnnotationDepth != -1 ) {
367             schemaDOM.characters(text);
368         }
369     }
370
371     /**
372      * The start of a CDATA section.
373      *
374      * @param augs Additional information that may include infoset augmentations
375      *
376      * @exception XNIException
377      * Thrown by handler to signal an error.
378      */

379     public void startCDATA(Augmentations augs) throws XNIException {
380         // only deal with CDATA boundaries within an annotation.
381
if (fAnnotationDepth != -1) {
382             schemaDOM.startAnnotationCDATA();
383         }
384     }
385
386     /**
387      * The end of a CDATA section.
388      *
389      * @param augs Additional information that may include infoset augmentations
390      *
391      * @exception XNIException
392      * Thrown by handler to signal an error.
393      */

394     public void endCDATA(Augmentations augs) throws XNIException {
395         // only deal with CDATA boundaries within an annotation.
396
if (fAnnotationDepth != -1) {
397             schemaDOM.endAnnotationCDATA();
398         }
399     }
400
401     
402     //
403
// other methods
404
//
405

406     /**
407      * Returns the DOM document object.
408      */

409     public Document JavaDoc getDocument() {
410         return schemaDOM;
411     }
412
413 }
414
Popular Tags