KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > schema > TraverseSchema


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000,2001 The Apache Software Foundation.
6  * All rights 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 package org.enhydra.apache.xerces.validators.schema;
58
59 import java.io.IOException JavaDoc;
60 import java.net.MalformedURLException JavaDoc;
61 import java.net.URL JavaDoc;
62 import java.util.Enumeration JavaDoc;
63 import java.util.Hashtable JavaDoc;
64 import java.util.Stack JavaDoc;
65 import java.util.StringTokenizer JavaDoc;
66 import java.util.Vector JavaDoc;
67
68 import org.enhydra.apache.xerces.framework.XMLContentSpec;
69 import org.enhydra.apache.xerces.framework.XMLErrorReporter;
70 import org.enhydra.apache.xerces.parsers.DOMParser;
71 import org.enhydra.apache.xerces.utils.NamespacesScope;
72 import org.enhydra.apache.xerces.utils.QName;
73 import org.enhydra.apache.xerces.utils.StringPool;
74 import org.enhydra.apache.xerces.validators.common.Grammar;
75 import org.enhydra.apache.xerces.validators.common.GrammarResolver;
76 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl;
77 import org.enhydra.apache.xerces.validators.common.XMLElementDecl;
78 import org.enhydra.apache.xerces.validators.datatype.AnySimpleType;
79 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidator;
80 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidatorFactoryImpl;
81 import org.enhydra.apache.xerces.validators.datatype.IDDatatypeValidator;
82 import org.enhydra.apache.xerces.validators.datatype.InvalidDatatypeValueException;
83 import org.enhydra.apache.xerces.validators.datatype.ListDatatypeValidator;
84 import org.enhydra.apache.xerces.validators.datatype.NOTATIONDatatypeValidator;
85 import org.enhydra.apache.xerces.validators.datatype.StringDatatypeValidator;
86 import org.enhydra.apache.xerces.validators.datatype.UnionDatatypeValidator;
87 import org.enhydra.apache.xerces.validators.schema.identity.Field;
88 import org.enhydra.apache.xerces.validators.schema.identity.IdentityConstraint;
89 import org.enhydra.apache.xerces.validators.schema.identity.Key;
90 import org.enhydra.apache.xerces.validators.schema.identity.KeyRef;
91 import org.enhydra.apache.xerces.validators.schema.identity.Selector;
92 import org.enhydra.apache.xerces.validators.schema.identity.Unique;
93 import org.enhydra.apache.xerces.validators.schema.identity.XPathException;
94 import org.w3c.dom.Attr JavaDoc;
95 import org.w3c.dom.Document JavaDoc;
96 import org.w3c.dom.Element JavaDoc;
97 import org.w3c.dom.NamedNodeMap JavaDoc;
98 import org.w3c.dom.Node JavaDoc;
99 import org.xml.sax.EntityResolver JavaDoc;
100 import org.xml.sax.InputSource JavaDoc;
101 import org.xml.sax.SAXException JavaDoc;
102 import org.xml.sax.SAXParseException JavaDoc;
103
104 /**
105  * Instances of this class get delegated to Traverse the Schema and
106  * to populate the Grammar internal representation by
107  * instances of Grammar objects.
108  * Traverse a Schema Grammar:
109  *
110  * @author Eric Ye, IBM
111  * @author Jeffrey Rodriguez, IBM
112  * @author Andy Clark, IBM
113  *
114  * @see org.enhydra.apache.xerces.validators.common.Grammar
115  *
116  * @version $Id: TraverseSchema.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
117  */

118 public class TraverseSchema implements
119                             NamespacesScope.NamespacesHandler{
120
121     //CONSTANTS
122
private static final int TOP_LEVEL_SCOPE = -1;
123
124     /** Identity constraint keywords. */
125     private static final String JavaDoc[][] IDENTITY_CONSTRAINTS = {
126         { SchemaSymbols.URI_SCHEMAFORSCHEMA, SchemaSymbols.ELT_UNIQUE },
127         { SchemaSymbols.URI_SCHEMAFORSCHEMA, SchemaSymbols.ELT_KEY },
128         { SchemaSymbols.URI_SCHEMAFORSCHEMA, SchemaSymbols.ELT_KEYREF },
129     };
130     // hopefully a string people will not use in their names very often...
131
private static final String JavaDoc redefIdentifier = "_fn3dktizrknc9pi";
132     // fields to hold application-specified schemaLocations
133
private String JavaDoc fUnparsedExternalSchemas = null;
134     private Hashtable JavaDoc fExternalSchemas = null;
135     private String JavaDoc fExternalNoNamespaceSchema = null;
136     // Flags for handleOccurrences to indicate any special
137
// restrictions on minOccurs and maxOccurs relating to "all".
138
// NOT_ALL_CONTEXT - not processing an <all>
139
// PROCESSING_ALL_EL - processing an <element> in an <all>
140
// GROUP_REF_WITH_ALL - processing <group> reference that contained <all>
141
// CHILD_OF_GROUP - processing a child of a model group definition
142
// PROCESSING_ALL_GP - processing an <all> group itself
143
private static final int NOT_ALL_CONTEXT = 0;
144     private static final int PROCESSING_ALL_EL = 1;
145     private static final int GROUP_REF_WITH_ALL = 2;
146     private static final int CHILD_OF_GROUP = 4;
147     private static final int PROCESSING_ALL_GP = 8;
148
149     //debugging
150
private static final boolean DEBUGGING = false;
151
152     /** Compile to true to debug identity constraints. */
153     private static final boolean DEBUG_IDENTITY_CONSTRAINTS = false;
154     private static final boolean DEBUG_NEW_GROUP = true;
155
156     /**
157      * Compile to true to debug datatype validator lookup for
158      * identity constraint support.
159      */

160     private static final boolean DEBUG_IC_DATATYPES = false;
161
162     //private data members
163

164     private boolean fFullConstraintChecking = false;
165
166     private XMLErrorReporter fErrorReporter = null;
167     private StringPool fStringPool = null;
168
169     private GrammarResolver fGrammarResolver = null;
170     private SchemaGrammar fSchemaGrammar = null;
171
172     private Element JavaDoc fSchemaRootElement;
173     // this is always set to refer to the root of the linked list containing the root info of schemas under redefinition.
174
private SchemaInfo fSchemaInfoListRoot = null;
175     private SchemaInfo fCurrentSchemaInfo = null;
176     private boolean fRedefineSucceeded;
177
178     private DatatypeValidatorFactoryImpl fDatatypeRegistry = null;
179
180     private Hashtable JavaDoc fComplexTypeRegistry = new Hashtable JavaDoc();
181     private Hashtable JavaDoc fAttributeDeclRegistry = new Hashtable JavaDoc();
182
183     // stores the names of groups that we've traversed so we can avoid multiple traversals
184
// qualified group names are keys and their contentSpecIndexes are values.
185
private Hashtable JavaDoc fGroupNameRegistry = new Hashtable JavaDoc();
186     // this Hashtable keeps track of whether a given redefined group does so by restriction.
187
private Hashtable JavaDoc fRestrictedRedefinedGroupRegistry = new Hashtable JavaDoc();
188
189     // stores "final" values of simpleTypes--no clean way to integrate this into the existing datatype validation structure...
190
private Hashtable JavaDoc fSimpleTypeFinalRegistry = new Hashtable JavaDoc();
191
192     // stores <notation> decl
193
private Hashtable JavaDoc fNotationRegistry = new Hashtable JavaDoc();
194
195     private Vector JavaDoc fIncludeLocations = new Vector JavaDoc();
196     private Vector JavaDoc fImportLocations = new Vector JavaDoc();
197     private Hashtable JavaDoc fRedefineLocations = new Hashtable JavaDoc();
198     private Vector JavaDoc fTraversedRedefineElements = new Vector JavaDoc();
199     // Hashtable associating attributeGroups within a <redefine> which
200
// restrict attributeGroups in the original schema with the
201
// new name for those groups in the modified redefined schema.
202
private Hashtable JavaDoc fRedefineAttributeGroupMap = null;
203
204     // simpleType data
205
private Hashtable JavaDoc fFacetData = new Hashtable JavaDoc(10);
206     private Stack JavaDoc fSimpleTypeNameStack = new Stack JavaDoc();
207     private String JavaDoc fListName = "";
208
209
210     private int fAnonTypeCount =0;
211     private int fScopeCount=0;
212     private int fCurrentScope=TOP_LEVEL_SCOPE;
213     private int fSimpleTypeAnonCount = 0;
214     private Stack JavaDoc fCurrentTypeNameStack = new Stack JavaDoc();
215     private Stack JavaDoc fBaseTypeNameStack = new Stack JavaDoc();
216     private Stack JavaDoc fCurrentGroupNameStack = new Stack JavaDoc();
217     private Vector JavaDoc fElementRecurseComplex = new Vector JavaDoc();
218     private Vector JavaDoc fTopLevelElementsRefdFromGroup = new Vector JavaDoc();
219     private Stack JavaDoc fCurrentAttrGroupNameStack = new Stack JavaDoc();
220
221     private Vector JavaDoc fSubstitutionGroupRecursionRegistry = new Vector JavaDoc();
222     private boolean fElementDefaultQualified = false;
223     private boolean fAttributeDefaultQualified = false;
224     private int fBlockDefault = 0;
225     private int fFinalDefault = 0;
226
227     private int fTargetNSURI;
228     private String JavaDoc fTargetNSURIString = "";
229     private NamespacesScope fNamespacesScope = null;
230     private String JavaDoc fCurrentSchemaURL = "";
231     private Stack JavaDoc fSchemaURLStack = new Stack JavaDoc();
232
233     private XMLAttributeDecl fTempAttributeDecl = new XMLAttributeDecl();
234     private XMLAttributeDecl fTemp2AttributeDecl = new XMLAttributeDecl();
235     private XMLElementDecl fTempElementDecl = new XMLElementDecl();
236     private XMLElementDecl fTempElementDecl2 = new XMLElementDecl();
237     private XMLContentSpec tempContentSpec1 = new XMLContentSpec();
238     private XMLContentSpec tempContentSpec2 = new XMLContentSpec();
239
240     private EntityResolver JavaDoc fEntityResolver = null;
241     private SubstitutionGroupComparator fSComp = null;
242
243     private Hashtable JavaDoc fIdentityConstraints = new Hashtable JavaDoc();
244     // Yet one more data structure; this one associates
245
// <unique> and <key> QNames with their corresponding objects,
246
// so that <keyRef>s can find them.
247
private Hashtable JavaDoc fIdentityConstraintNames = new Hashtable JavaDoc();
248
249     // General Attribute Checking
250
private GeneralAttrCheck fGeneralAttrCheck = null;
251     private int fXsiURI;
252
253    // REVISIT: maybe need to be moved into SchemaGrammar class
254
public class ComplexTypeInfo {
255         public String JavaDoc typeName;
256
257         public DatatypeValidator baseDataTypeValidator;
258         public ComplexTypeInfo baseComplexTypeInfo;
259
260         public int derivedBy = 0;
261         public int blockSet = 0;
262         public int finalSet = 0;
263
264         public int miscFlags=0;
265
266         public int scopeDefined = -1;
267
268         public int contentType;
269         public int contentSpecHandle = -1;
270         public int templateElementIndex = -1;
271         public int attlistHead = -1;
272         public DatatypeValidator datatypeValidator;
273
274         public boolean isAbstractType() {
275            return ((miscFlags & CT_IS_ABSTRACT)!=0);
276         }
277         public boolean containsAttrTypeID () {
278            return ((miscFlags & CT_CONTAINS_ATTR_TYPE_ID)!=0);
279         }
280         public boolean declSeen () {
281            return ((miscFlags & CT_DECL_SEEN)!=0);
282         }
283
284         public void setIsAbstractType() {
285            miscFlags |= CT_IS_ABSTRACT;
286         }
287         public void setContainsAttrTypeID() {
288            miscFlags |= CT_CONTAINS_ATTR_TYPE_ID;
289         }
290         public void setDeclSeen() {
291            miscFlags |= CT_DECL_SEEN;
292         }
293
294     }
295     private static final int CT_IS_ABSTRACT=1;
296     private static final int CT_CONTAINS_ATTR_TYPE_ID=2;
297     private static final int CT_DECL_SEEN=4; // indicates that the declaration was
298
// traversed as opposed to processed due
299
// to a forward reference
300

301     private class ComplexTypeRecoverableError extends Exception JavaDoc {
302         ComplexTypeRecoverableError() {super();}
303         ComplexTypeRecoverableError(String JavaDoc s) {super(s);}
304     }
305
306     private class ParticleRecoverableError extends Exception JavaDoc {
307         ParticleRecoverableError(String JavaDoc s) {super(s);}
308     }
309
310     private class GroupInfo {
311         int contentSpecIndex = -1;
312         int scope = -1;
313     }
314
315     private class ElementInfo {
316         int elementIndex;
317         String JavaDoc typeName;
318
319         private ElementInfo(int i, String JavaDoc name) {
320            elementIndex = i;
321            typeName = name;
322         }
323     }
324
325     //REVISIT: verify the URI.
326
public final static String JavaDoc SchemaForSchemaURI = "http://www.w3.org/TR-1/Schema";
327
328     private TraverseSchema( ) {
329         // new TraverseSchema() is forbidden;
330
}
331
332     public void setFullConstraintCheckingEnabled() {
333         fFullConstraintChecking = true;
334     }
335
336     public void setGrammarResolver(GrammarResolver grammarResolver){
337         fGrammarResolver = grammarResolver;
338     }
339     public void startNamespaceDeclScope(int prefix, int uri){
340         //TO DO
341
}
342     public void endNamespaceDeclScope(int prefix){
343         //TO DO, do we need to do anything here?
344
}
345
346     public boolean particleEmptiable(int contentSpecIndex) {
347
348        if (!fFullConstraintChecking) {
349            return true;
350        }
351        if (minEffectiveTotalRange(contentSpecIndex)==0)
352          return true;
353        else
354          return false;
355     }
356
357     public int minEffectiveTotalRange(int contentSpecIndex) {
358
359        fSchemaGrammar.getContentSpec(contentSpecIndex, tempContentSpec1);
360        int type = tempContentSpec1.type;
361        if (type == XMLContentSpec.CONTENTSPECNODE_SEQ ||
362            type == XMLContentSpec.CONTENTSPECNODE_ALL) {
363           return minEffectiveTotalRangeSeq(contentSpecIndex);
364        }
365        else if (type == XMLContentSpec.CONTENTSPECNODE_CHOICE) {
366           return minEffectiveTotalRangeChoice(contentSpecIndex);
367        }
368        else {
369           return(fSchemaGrammar.getContentSpecMinOccurs(contentSpecIndex));
370        }
371
372     }
373
374     private int minEffectiveTotalRangeSeq(int csIndex) {
375
376        fSchemaGrammar.getContentSpec(csIndex, tempContentSpec1);
377        int type = tempContentSpec1.type;
378        int left = tempContentSpec1.value;
379        int right = tempContentSpec1.otherValue;
380        int min = fSchemaGrammar.getContentSpecMinOccurs(csIndex);
381
382        int result;
383        if (right == -2)
384           result = min * minEffectiveTotalRange(left);
385        else
386           result = min * (minEffectiveTotalRange(left) + minEffectiveTotalRange(right));
387        return result;
388
389     }
390     private int minEffectiveTotalRangeChoice(int csIndex) {
391
392        fSchemaGrammar.getContentSpec(csIndex, tempContentSpec1);
393        int type = tempContentSpec1.type;
394        int left = tempContentSpec1.value;
395        int right = tempContentSpec1.otherValue;
396        int min = fSchemaGrammar.getContentSpecMinOccurs(csIndex);
397
398        int result;
399        if (right == -2)
400           result = min * minEffectiveTotalRange(left);
401        else {
402           int minLeft = minEffectiveTotalRange(left);
403           int minRight = minEffectiveTotalRange(right);
404           result = min * ((minLeft < minRight)?minLeft:minRight);
405        }
406        return result;
407     }
408
409     public int maxEffectiveTotalRange(int contentSpecIndex) {
410
411        fSchemaGrammar.getContentSpec(contentSpecIndex, tempContentSpec1);
412        int type = tempContentSpec1.type;
413        if (type == XMLContentSpec.CONTENTSPECNODE_SEQ ||
414            type == XMLContentSpec.CONTENTSPECNODE_ALL) {
415           return maxEffectiveTotalRangeSeq(contentSpecIndex);
416        }
417        else if (type == XMLContentSpec.CONTENTSPECNODE_CHOICE) {
418           return maxEffectiveTotalRangeChoice(contentSpecIndex);
419        }
420        else {
421           return(fSchemaGrammar.getContentSpecMaxOccurs(contentSpecIndex));
422        }
423
424     }
425
426     private int maxEffectiveTotalRangeSeq(int csIndex) {
427
428        fSchemaGrammar.getContentSpec(csIndex, tempContentSpec1);
429        int type = tempContentSpec1.type;
430        int left = tempContentSpec1.value;
431        int right = tempContentSpec1.otherValue;
432        int max = fSchemaGrammar.getContentSpecMaxOccurs(csIndex);
433
434        if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
435          return SchemaSymbols.OCCURRENCE_UNBOUNDED;
436
437        int maxLeft = maxEffectiveTotalRange(left);
438        if (right == -2) {
439          if (maxLeft == SchemaSymbols.OCCURRENCE_UNBOUNDED)
440            return SchemaSymbols.OCCURRENCE_UNBOUNDED;
441          else
442            return max * maxLeft;
443        }
444        else {
445          int maxRight = maxEffectiveTotalRange(right);
446          if (maxLeft == SchemaSymbols.OCCURRENCE_UNBOUNDED ||
447             maxRight == SchemaSymbols.OCCURRENCE_UNBOUNDED)
448            return SchemaSymbols.OCCURRENCE_UNBOUNDED;
449          else
450            return max * (maxLeft + maxRight);
451        }
452     }
453
454     private int maxEffectiveTotalRangeChoice(int csIndex) {
455
456        fSchemaGrammar.getContentSpec(csIndex, tempContentSpec1);
457        int type = tempContentSpec1.type;
458        int left = tempContentSpec1.value;
459        int right = tempContentSpec1.otherValue;
460        int max = fSchemaGrammar.getContentSpecMaxOccurs(csIndex);
461
462        if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
463          return SchemaSymbols.OCCURRENCE_UNBOUNDED;
464
465        int maxLeft = maxEffectiveTotalRange(left);
466        if (right == -2) {
467          if (maxLeft == SchemaSymbols.OCCURRENCE_UNBOUNDED)
468            return SchemaSymbols.OCCURRENCE_UNBOUNDED;
469          else
470            return max * maxLeft;
471        }
472        else {
473          int maxRight = maxEffectiveTotalRange(right);
474          if (maxLeft == SchemaSymbols.OCCURRENCE_UNBOUNDED ||
475             maxRight == SchemaSymbols.OCCURRENCE_UNBOUNDED)
476            return SchemaSymbols.OCCURRENCE_UNBOUNDED;
477          else
478            return max * ((maxLeft > maxRight)?maxLeft:maxRight);
479        }
480     }
481
482
483     private String JavaDoc resolvePrefixToURI (String JavaDoc prefix) throws Exception JavaDoc {
484         String JavaDoc uriStr = fStringPool.toString(fNamespacesScope.getNamespaceForPrefix(fStringPool.addSymbol(prefix)));
485         if (uriStr.length() == 0 && prefix.length() > 0) {
486             // REVISIT: Localize
487
reportGenericSchemaError("prefix : [" + prefix +"] cannot be resolved to a URI");
488             return "";
489         }
490
491         return uriStr;
492     }
493
494     public TraverseSchema(Element JavaDoc root, StringPool stringPool,
495                            SchemaGrammar schemaGrammar,
496                            GrammarResolver grammarResolver,
497                            XMLErrorReporter errorReporter,
498                            String JavaDoc schemaURL,
499                            EntityResolver JavaDoc entityResolver,
500                            boolean fullChecking,
501                            GeneralAttrCheck generalAttrCheck,
502                            String JavaDoc externalSchemaLocations, String JavaDoc noNamespaceSchemaLocation
503                            ) throws Exception JavaDoc {
504         fErrorReporter = errorReporter;
505         fCurrentSchemaURL = schemaURL;
506         fFullConstraintChecking = fullChecking;
507         fEntityResolver = entityResolver;
508         fGeneralAttrCheck = generalAttrCheck;
509         fUnparsedExternalSchemas = externalSchemaLocations;
510         if(externalSchemaLocations != null) {
511             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(externalSchemaLocations, " \n\t\r", false);
512             int tokenTotal = tokenizer.countTokens();
513             if (tokenTotal % 2 == 0 ) {
514                 fExternalSchemas = new Hashtable JavaDoc();
515                 String JavaDoc uri = null;
516                 String JavaDoc location = null;
517                 while (tokenizer.hasMoreTokens()) {
518                     uri = tokenizer.nextToken();
519                     location = tokenizer.nextToken();
520                     fExternalSchemas.put(location, uri);
521                 }
522             }
523         }
524         fExternalNoNamespaceSchema = noNamespaceSchemaLocation;
525         doTraverseSchema(root, stringPool, schemaGrammar, grammarResolver);
526     }
527
528     public TraverseSchema(Element JavaDoc root, StringPool stringPool,
529                            SchemaGrammar schemaGrammar,
530                            GrammarResolver grammarResolver,
531                            XMLErrorReporter errorReporter,
532                            String JavaDoc schemaURL,
533                            boolean fullChecking,
534                            GeneralAttrCheck generalAttrCheck
535                            ) throws Exception JavaDoc {
536         fErrorReporter = errorReporter;
537         fCurrentSchemaURL = schemaURL;
538         fFullConstraintChecking = fullChecking;
539         fGeneralAttrCheck = generalAttrCheck;
540         doTraverseSchema(root, stringPool, schemaGrammar, grammarResolver);
541     }
542
543     public TraverseSchema(Element JavaDoc root, StringPool stringPool,
544                            SchemaGrammar schemaGrammar,
545                            GrammarResolver grammarResolver,
546                            boolean fullChecking,
547                            GeneralAttrCheck generalAttrCheck
548                            ) throws Exception JavaDoc {
549         fFullConstraintChecking = fullChecking;
550         fGeneralAttrCheck = generalAttrCheck;
551         doTraverseSchema(root, stringPool, schemaGrammar, grammarResolver);
552     }
553
554     public void doTraverseSchema(Element JavaDoc root, StringPool stringPool,
555                            SchemaGrammar schemaGrammar,
556                            GrammarResolver grammarResolver) throws Exception JavaDoc {
557
558
559         fSchemaRootElement = root;
560         fStringPool = stringPool;
561         fSchemaGrammar = schemaGrammar;
562         fNamespacesScope = new NamespacesScope(this);
563         fNamespacesScope.setNamespaceForPrefix(fStringPool.addSymbol("xml"), fStringPool.addSymbol("http://www.w3.org/XML/1998/namespace"));
564
565         if (fFullConstraintChecking) {
566           fSchemaGrammar.setDeferContentSpecExpansion();
567           fSchemaGrammar.setCheckUniqueParticleAttribution();
568         }
569
570         fGrammarResolver = grammarResolver;
571         fDatatypeRegistry = (DatatypeValidatorFactoryImpl) fGrammarResolver.getDatatypeRegistry();
572
573         //Expand to registry type to contain all primitive datatype
574
fDatatypeRegistry.expandRegistryToFullSchemaSet();
575
576         fXsiURI = fStringPool.addSymbol(SchemaSymbols.URI_XSI);
577
578         if (root == null) {
579             // REVISIT: Anything to do?
580
return;
581         }
582
583         // General Attribute Checking
584
int scope = GeneralAttrCheck.ELE_CONTEXT_GLOBAL;
585         Hashtable JavaDoc attrValues = generalCheck(root, scope);
586
587         //Retrieve the targetNamespace URI information
588
fTargetNSURIString = getTargetNamespaceString(root);
589         fTargetNSURI = fStringPool.addSymbol(fTargetNSURIString);
590
591         if (fGrammarResolver == null) {
592             // REVISIT: Localize
593
reportGenericSchemaError("Internal error: don't have a GrammarResolver for TraverseSchema");
594         }
595         else{
596             // for complex type registry, attribute decl registry and
597
// namespace mapping, needs to check whether the passed in
598
// Grammar was a newly instantiated one.
599
if (fSchemaGrammar.getComplexTypeRegistry() == null ) {
600                 fSchemaGrammar.setComplexTypeRegistry(fComplexTypeRegistry);
601             }
602             else {
603                 fComplexTypeRegistry = fSchemaGrammar.getComplexTypeRegistry();
604             }
605
606             if (fSchemaGrammar.getAttributeDeclRegistry() == null ) {
607                 fSchemaGrammar.setAttributeDeclRegistry(fAttributeDeclRegistry);
608             }
609             else {
610                 fAttributeDeclRegistry = fSchemaGrammar.getAttributeDeclRegistry();
611             }
612
613             if (fSchemaGrammar.getNamespacesScope() == null ) {
614                 fSchemaGrammar.setNamespacesScope(fNamespacesScope);
615             }
616             else {
617                 fNamespacesScope = fSchemaGrammar.getNamespacesScope();
618             }
619
620             fSchemaGrammar.setDatatypeRegistry(fDatatypeRegistry);
621             fSchemaGrammar.setTargetNamespaceURI(fTargetNSURIString);
622             fGrammarResolver.putGrammar(fTargetNSURIString, fSchemaGrammar);
623         }
624
625
626
627         // Retrived the Namespace mapping from the schema element.
628
NamedNodeMap JavaDoc schemaEltAttrs = root.getAttributes();
629         int i = 0;
630         Attr JavaDoc sattr = null;
631
632         boolean seenXMLNS = false;
633         while ((sattr = (Attr JavaDoc)schemaEltAttrs.item(i++)) != null) {
634             String JavaDoc attName = sattr.getName();
635             if (attName.startsWith("xmlns:")) {
636                 String JavaDoc attValue = sattr.getValue();
637                 String JavaDoc prefix = attName.substring(attName.indexOf(":")+1);
638                 fNamespacesScope.setNamespaceForPrefix( fStringPool.addSymbol(prefix),
639                                                         fStringPool.addSymbol(attValue) );
640             }
641             if (attName.equals("xmlns")) {
642
643                 String JavaDoc attValue = sattr.getValue();
644                 fNamespacesScope.setNamespaceForPrefix( StringPool.EMPTY_STRING,
645                                                         fStringPool.addSymbol(attValue) );
646                 seenXMLNS = true;
647             }
648
649         }
650         if (!seenXMLNS && fTargetNSURIString.length() == 0 ) {
651             fNamespacesScope.setNamespaceForPrefix( StringPool.EMPTY_STRING,
652                                                     StringPool.EMPTY_STRING);
653         }
654
655         fElementDefaultQualified =
656             root.getAttribute(SchemaSymbols.ATT_ELEMENTFORMDEFAULT).equals(SchemaSymbols.ATTVAL_QUALIFIED);
657         fAttributeDefaultQualified =
658             root.getAttribute(SchemaSymbols.ATT_ATTRIBUTEFORMDEFAULT).equals(SchemaSymbols.ATTVAL_QUALIFIED);
659         Attr JavaDoc blockAttr = root.getAttributeNode(SchemaSymbols.ATT_BLOCKDEFAULT);
660         if (blockAttr == null)
661             fBlockDefault = 0;
662         else
663             fBlockDefault =
664                 parseBlockSet(blockAttr.getValue());
665         Attr JavaDoc finalAttr = root.getAttributeNode(SchemaSymbols.ATT_FINALDEFAULT);
666         if (finalAttr == null)
667             fFinalDefault = 0;
668         else
669             fFinalDefault =
670                 parseFinalSet(finalAttr.getValue());
671
672         //REVISIT, really sticky when noTargetNamesapce, for now, we assume everyting is in the same name space);
673
if (fTargetNSURI == StringPool.EMPTY_STRING) {
674             //fElementDefaultQualified = true;
675
//fAttributeDefaultQualified = true;
676
}
677
678
679         //fScopeCount++;
680
// fCurrentScope = -1;
681

682
683         //extract all top-level attribute, attributeGroup, and group Decls and put them in the 3 hasn table in the SchemaGrammar.
684
extractTopLevel3Components(root);
685
686         // process <redefine>, <include> and <import> info items.
687
Element JavaDoc child = XUtil.getFirstChildElement(root);
688         for (; child != null;
689             child = XUtil.getNextSiblingElement(child)) {
690
691             String JavaDoc name = child.getLocalName();
692             if (name.equals(SchemaSymbols.ELT_ANNOTATION) ) {
693                 traverseAnnotationDecl(child);
694             } else if (name.equals(SchemaSymbols.ELT_INCLUDE)) {
695                 fNamespacesScope.increaseDepth();
696                 traverseInclude(child);
697                 fNamespacesScope.decreaseDepth();
698             } else if (name.equals(SchemaSymbols.ELT_IMPORT)) {
699                 traverseImport(child);
700             } else if (name.equals(SchemaSymbols.ELT_REDEFINE)) {
701                 fRedefineSucceeded = true; // presume worked until proven failed.
702
traverseRedefine(child);
703             } else
704                 break;
705         }
706
707         // child refers to the first info item which is not <annotation> or
708
// one of the schema inclusion/importation declarations.
709
for (; child != null;
710             child = XUtil.getNextSiblingElement(child)) {
711
712             String JavaDoc name = child.getLocalName();
713             if (name.equals(SchemaSymbols.ELT_ANNOTATION) ) {
714                 traverseAnnotationDecl(child);
715             } else if (name.equals(SchemaSymbols.ELT_SIMPLETYPE )) {
716                 traverseSimpleTypeDecl(child);
717             } else if (name.equals(SchemaSymbols.ELT_COMPLEXTYPE )) {
718                 traverseComplexTypeDecl(child);
719             } else if (name.equals(SchemaSymbols.ELT_ELEMENT )) {
720                 traverseElementDecl(child);
721             } else if (name.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) {
722                 traverseAttributeGroupDecl(child, null, null);
723             } else if (name.equals( SchemaSymbols.ELT_ATTRIBUTE ) ) {
724                 traverseAttributeDecl( child, null, false );
725             } else if (name.equals(SchemaSymbols.ELT_GROUP)) {
726                 traverseGroupDecl(child);
727             } else if (name.equals(