1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.impl.xs;
  23 
  24 import com.sun.org.apache.xerces.internal.impl.Constants;
  25 import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;
  26 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  27 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  28 import com.sun.org.apache.xerces.internal.impl.dv.xs.XSSimpleTypeDecl;
  29 import com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
  30 import com.sun.org.apache.xerces.internal.impl.xs.util.ObjectListImpl;
  31 import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
  32 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
  33 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMap4Types;
  34 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
  35 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  36 import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  37 import com.sun.org.apache.xerces.internal.parsers.SAXParser;
  38 import com.sun.org.apache.xerces.internal.parsers.XML11Configuration;
  39 import com.sun.org.apache.xerces.internal.util.SymbolHash;
  40 import com.sun.org.apache.xerces.internal.util.SymbolTable;
  41 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  42 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  43 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar;
  44 import com.sun.org.apache.xerces.internal.xs.StringList;
  45 import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
  46 import com.sun.org.apache.xerces.internal.xs.XSAttributeDeclaration;
  47 import com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition;
  48 import com.sun.org.apache.xerces.internal.xs.XSConstants;
  49 import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
  50 import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
  51 import com.sun.org.apache.xerces.internal.xs.XSModel;
  52 import com.sun.org.apache.xerces.internal.xs.XSModelGroupDefinition;
  53 import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
  54 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  55 import com.sun.org.apache.xerces.internal.xs.XSNotationDeclaration;
  56 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
  57 import com.sun.org.apache.xerces.internal.xs.XSParticle;
  58 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  59 import com.sun.org.apache.xerces.internal.xs.XSWildcard;
  60 import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
  61 import java.lang.ref.SoftReference;
  62 import java.util.ArrayList;
  63 import java.util.List;
  64 import java.util.concurrent.CopyOnWriteArrayList;
  65 import org.xml.sax.SAXException;
  66 
  67 /**
  68  * This class is to hold all schema component declaration that are declared
  69  * within one namespace.
  70  *
  71  * The Grammar class this class extends contains what little
  72  * commonality there is between XML Schema and DTD grammars.  It's
  73  * useful to distinguish grammar objects from other kinds of object
  74  * when they exist in pools or caches.
  75  *
  76  * @xerces.internal
  77  *
  78  * @author Sandy Gao, IBM
  79  * @author Elena Litani, IBM
  80  *
  81  */
  82 
  83 public class SchemaGrammar implements XSGrammar, XSNamespaceItem {
  84 
  85     // the target namespace of grammar
  86     String fTargetNamespace;
  87 
  88     // global decls: map from decl name to decl object
  89     SymbolHash fGlobalAttrDecls;
  90     SymbolHash fGlobalAttrGrpDecls;
  91     SymbolHash fGlobalElemDecls;
  92     SymbolHash fGlobalGroupDecls;
  93     SymbolHash fGlobalNotationDecls;
  94     SymbolHash fGlobalIDConstraintDecls;
  95     SymbolHash fGlobalTypeDecls;
  96 
  97     // extended global decls: map from schema location + decl name to decl object
  98     // key is location,name
  99     SymbolHash fGlobalAttrDeclsExt;
 100     SymbolHash fGlobalAttrGrpDeclsExt;
 101     SymbolHash fGlobalElemDeclsExt;
 102     SymbolHash fGlobalGroupDeclsExt;
 103     SymbolHash fGlobalNotationDeclsExt;
 104     SymbolHash fGlobalIDConstraintDeclsExt;
 105     SymbolHash fGlobalTypeDeclsExt;
 106 
 107     // A global map of all global element declarations - used for substitution group computation
 108     // (handy when sharing components by reference, since we might end up with duplicate components
 109     //  that are not added to either of the global element declarations above)
 110     SymbolHash fAllGlobalElemDecls;
 111 
 112     // the XMLGrammarDescription member
 113     XSDDescription fGrammarDescription = null;
 114 
 115     // annotations associated with the "root" schema of this targetNamespace
 116     XSAnnotationImpl [] fAnnotations = null;
 117 
 118     // number of annotations declared
 119     int fNumAnnotations;
 120 
 121     // symbol table for constructing parsers (annotation support)
 122     private SymbolTable fSymbolTable = null;
 123     // parsers for annotation support
 124     private SoftReference<SAXParser> fSAXParser = null;
 125     private SoftReference<DOMParser> fDOMParser = null;
 126 
 127     // is this grammar immutable?  (fully constructed and not changeable)
 128     private boolean fIsImmutable = false;
 129 
 130     //
 131     // Constructors
 132     //
 133 
 134     // needed to make BuiltinSchemaGrammar work.
 135     protected SchemaGrammar() {}
 136 
 137     /**
 138      * Default constructor.
 139      *
 140      * @param targetNamespace
 141      * @param grammarDesc the XMLGrammarDescription corresponding to this object
 142      *          at the least a systemId should always be known.
 143      * @param symbolTable   needed for annotation support
 144      */
 145     public SchemaGrammar(String targetNamespace, XSDDescription grammarDesc,
 146                 SymbolTable symbolTable) {
 147         fTargetNamespace = targetNamespace;
 148         fGrammarDescription = grammarDesc;
 149         fSymbolTable = symbolTable;
 150 
 151         // REVISIT: the initial sizes being chosen for each SymbolHash
 152         // may not be ideal and could still be tuned. They were chosen
 153         // somewhat arbitrarily to reduce the initial footprint of
 154         // SymbolHash buckets from 1,515 to 177 (about 12% of the
 155         // default size).
 156         fGlobalAttrDecls  = new SymbolHash(12);
 157         fGlobalAttrGrpDecls = new SymbolHash(5);
 158         fGlobalElemDecls = new SymbolHash(25);
 159         fGlobalGroupDecls = new SymbolHash(5);
 160         fGlobalNotationDecls = new SymbolHash(1);
 161         fGlobalIDConstraintDecls = new SymbolHash(3);
 162 
 163         // Extended tables
 164         fGlobalAttrDeclsExt  = new SymbolHash(12);
 165         fGlobalAttrGrpDeclsExt = new SymbolHash(5);
 166         fGlobalElemDeclsExt = new SymbolHash(25);
 167         fGlobalGroupDeclsExt = new SymbolHash(5);
 168         fGlobalNotationDeclsExt = new SymbolHash(1);
 169         fGlobalIDConstraintDeclsExt = new SymbolHash(3);
 170         fGlobalTypeDeclsExt = new SymbolHash(25);
 171 
 172         // All global elements table
 173         fAllGlobalElemDecls = new SymbolHash(25);
 174 
 175         // if we are parsing S4S, put built-in types in first
 176         // they might get overwritten by the types from S4S, but that's
 177         // considered what the application wants to do.
 178         if (fTargetNamespace == SchemaSymbols.URI_SCHEMAFORSCHEMA) {
 179             fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls.makeClone();
 180         }
 181         else {
 182             fGlobalTypeDecls = new SymbolHash(25);
 183         }
 184     } // <init>(String, XSDDescription)
 185 
 186     // Clone an existing schema grammar
 187     public SchemaGrammar(SchemaGrammar grammar) {
 188         fTargetNamespace = grammar.fTargetNamespace;
 189         fGrammarDescription = grammar.fGrammarDescription.makeClone();
 190         //fGrammarDescription.fContextType |= XSDDescription.CONTEXT_COLLISION; // REVISIT
 191         fSymbolTable = grammar.fSymbolTable; // REVISIT
 192 
 193         fGlobalAttrDecls  = grammar.fGlobalAttrDecls.makeClone();
 194         fGlobalAttrGrpDecls = grammar.fGlobalAttrGrpDecls.makeClone();
 195         fGlobalElemDecls = grammar.fGlobalElemDecls.makeClone();
 196         fGlobalGroupDecls = grammar.fGlobalGroupDecls.makeClone();
 197         fGlobalNotationDecls = grammar.fGlobalNotationDecls.makeClone();
 198         fGlobalIDConstraintDecls = grammar.fGlobalIDConstraintDecls.makeClone();
 199         fGlobalTypeDecls = grammar.fGlobalTypeDecls.makeClone();
 200 
 201         // Extended tables
 202         fGlobalAttrDeclsExt  = grammar.fGlobalAttrDeclsExt.makeClone();
 203         fGlobalAttrGrpDeclsExt = grammar.fGlobalAttrGrpDeclsExt.makeClone();
 204         fGlobalElemDeclsExt = grammar.fGlobalElemDeclsExt.makeClone();
 205         fGlobalGroupDeclsExt = grammar.fGlobalGroupDeclsExt.makeClone();
 206         fGlobalNotationDeclsExt = grammar.fGlobalNotationDeclsExt.makeClone();
 207         fGlobalIDConstraintDeclsExt = grammar.fGlobalIDConstraintDeclsExt.makeClone();
 208         fGlobalTypeDeclsExt = grammar.fGlobalTypeDeclsExt.makeClone();
 209 
 210         // All global elements table
 211         fAllGlobalElemDecls = grammar.fAllGlobalElemDecls.makeClone();
 212 
 213         // Annotations associated with the "root" schema of this targetNamespace
 214         fNumAnnotations = grammar.fNumAnnotations;
 215         if (fNumAnnotations > 0) {
 216             fAnnotations = new XSAnnotationImpl[grammar.fAnnotations.length];
 217             System.arraycopy(grammar.fAnnotations, 0, fAnnotations, 0, fNumAnnotations);
 218         }
 219 
 220         // All substitution group information declared in this namespace
 221         fSubGroupCount = grammar.fSubGroupCount;
 222         if (fSubGroupCount > 0) {
 223             fSubGroups = new XSElementDecl[grammar.fSubGroups.length];
 224             System.arraycopy(grammar.fSubGroups, 0, fSubGroups, 0, fSubGroupCount);
 225         }
 226 
 227         // Array to store complex type decls for constraint checking
 228         fCTCount = grammar.fCTCount;
 229         if (fCTCount > 0) {
 230             fComplexTypeDecls = new XSComplexTypeDecl[grammar.fComplexTypeDecls.length];
 231             fCTLocators = new SimpleLocator[grammar.fCTLocators.length];
 232             System.arraycopy(grammar.fComplexTypeDecls, 0, fComplexTypeDecls, 0, fCTCount);
 233             System.arraycopy(grammar.fCTLocators, 0, fCTLocators, 0, fCTCount);
 234         }
 235 
 236         // Groups being redefined by restriction
 237         fRGCount = grammar.fRGCount;
 238         if (fRGCount > 0) {
 239             fRedefinedGroupDecls = new XSGroupDecl[grammar.fRedefinedGroupDecls.length];
 240             fRGLocators = new SimpleLocator[grammar.fRGLocators.length];
 241             System.arraycopy(grammar.fRedefinedGroupDecls, 0, fRedefinedGroupDecls, 0, fRGCount);
 242             System.arraycopy(grammar.fRGLocators, 0, fRGLocators, 0, fRGCount/2);
 243         }
 244 
 245         // List of imported grammars
 246         if (grammar.fImported != null) {
 247             fImported = new ArrayList<>();
 248             for (int i=0; i<grammar.fImported.size(); i++) {
 249                 fImported.add(grammar.fImported.get(i));
 250             }
 251         }
 252 
 253         // Locations
 254         if (grammar.fLocations != null) {
 255             for (int k=0; k<grammar.fLocations.size(); k++) {
 256                 addDocument(null, grammar.fLocations.get(k));
 257             }
 258         }
 259 
 260     } // <init>(String, XSDDescription)
 261 
 262     // number of built-in XSTypes we need to create for base and full
 263     // datatype set
 264     private static final int BASICSET_COUNT = 29;
 265     private static final int FULLSET_COUNT  = 46;
 266 
 267     private static final int GRAMMAR_XS  = 1;
 268     private static final int GRAMMAR_XSI = 2;
 269 
 270     // this class makes sure the static, built-in schema grammars
 271     // are immutable.
 272     public static class BuiltinSchemaGrammar extends SchemaGrammar {
 273 
 274         private static final String EXTENDED_SCHEMA_FACTORY_CLASS = "com.sun.org.apache.xerces.internal.impl.dv.xs.ExtendedSchemaDVFactoryImpl";
 275 
 276         /**
 277          * Special constructor to create the grammars for the schema namespaces
 278          *
 279          * @param grammar
 280          */
 281         public BuiltinSchemaGrammar(int grammar, short schemaVersion) {
 282             SchemaDVFactory schemaFactory;
 283             if (schemaVersion == Constants.SCHEMA_VERSION_1_0) {
 284                 schemaFactory = SchemaDVFactory.getInstance();
 285             }
 286             else {
 287                 schemaFactory = SchemaDVFactory.getInstance(EXTENDED_SCHEMA_FACTORY_CLASS);
 288             }
 289 
 290             if (grammar == GRAMMAR_XS) {
 291                 // target namespace
 292                 fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
 293 
 294                 // grammar description
 295                 fGrammarDescription = new XSDDescription();
 296                 fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
 297                 fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
 298 
 299                 // no global decls other than types
 300                 fGlobalAttrDecls  = new SymbolHash(1);
 301                 fGlobalAttrGrpDecls = new SymbolHash(1);
 302                 fGlobalElemDecls = new SymbolHash(1);
 303                 fGlobalGroupDecls = new SymbolHash(1);
 304                 fGlobalNotationDecls = new SymbolHash(1);
 305                 fGlobalIDConstraintDecls = new SymbolHash(1);
 306 
 307                 // no extended global decls
 308                 fGlobalAttrDeclsExt  = new SymbolHash(1);
 309                 fGlobalAttrGrpDeclsExt = new SymbolHash(1);
 310                 fGlobalElemDeclsExt = new SymbolHash(1);
 311                 fGlobalGroupDeclsExt = new SymbolHash(1);
 312                 fGlobalNotationDeclsExt = new SymbolHash(1);
 313                 fGlobalIDConstraintDeclsExt = new SymbolHash(1);
 314                 fGlobalTypeDeclsExt = new SymbolHash(1);
 315 
 316                 // all global element decls table
 317                 fAllGlobalElemDecls = new SymbolHash(1);
 318 
 319                 // get all built-in types
 320                 fGlobalTypeDecls = schemaFactory.getBuiltInTypes();
 321 
 322                 // assign the built-in schema grammar as the XSNamespaceItem
 323                 // for each of the built-in simple type definitions.
 324                 int length = fGlobalTypeDecls.getLength();
 325                 XSTypeDefinition [] typeDefinitions = new XSTypeDefinition[length];
 326                 fGlobalTypeDecls.getValues(typeDefinitions, 0);
 327                 for (int i = 0; i < length; ++i) {
 328                     XSTypeDefinition xtd = typeDefinitions[i];
 329                     if (xtd instanceof XSSimpleTypeDecl) {
 330                         ((XSSimpleTypeDecl) xtd).setNamespaceItem(this);
 331                     }
 332                 }
 333 
 334                 // add anyType
 335                 fGlobalTypeDecls.put(fAnyType.getName(), fAnyType);
 336             }
 337             else if (grammar == GRAMMAR_XSI) {
 338                 // target namespace
 339                 fTargetNamespace = SchemaSymbols.URI_XSI;
 340                 // grammar description
 341                 fGrammarDescription = new XSDDescription();
 342                 fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
 343                 fGrammarDescription.setNamespace(SchemaSymbols.URI_XSI);
 344 
 345                 // no global decls other than attributes
 346                 fGlobalAttrGrpDecls = new SymbolHash(1);
 347                 fGlobalElemDecls = new SymbolHash(1);
 348                 fGlobalGroupDecls = new SymbolHash(1);
 349                 fGlobalNotationDecls = new SymbolHash(1);
 350                 fGlobalIDConstraintDecls = new SymbolHash(1);
 351                 fGlobalTypeDecls = new SymbolHash(1);
 352 
 353                 // no extended global decls
 354                 fGlobalAttrDeclsExt  = new SymbolHash(1);
 355                 fGlobalAttrGrpDeclsExt = new SymbolHash(1);
 356                 fGlobalElemDeclsExt = new SymbolHash(1);
 357                 fGlobalGroupDeclsExt = new SymbolHash(1);
 358                 fGlobalNotationDeclsExt = new SymbolHash(1);
 359                 fGlobalIDConstraintDeclsExt = new SymbolHash(1);
 360                 fGlobalTypeDeclsExt = new SymbolHash(1);
 361 
 362                 // no all global element decls
 363                 fAllGlobalElemDecls = new SymbolHash(1);
 364 
 365                 // 4 attributes, so initialize the size as 4*2 = 8
 366                 fGlobalAttrDecls  = new SymbolHash(8);
 367                 String name = null;
 368                 String tns = null;
 369                 XSSimpleType type = null;
 370                 short scope = XSConstants.SCOPE_GLOBAL;
 371 
 372                 // xsi:type
 373                 name = SchemaSymbols.XSI_TYPE;
 374                 tns = SchemaSymbols.URI_XSI;
 375                 type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_QNAME);
 376                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
 377 
 378                 // xsi:nil
 379                 name = SchemaSymbols.XSI_NIL;
 380                 tns = SchemaSymbols.URI_XSI;
 381                 type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_BOOLEAN);
 382                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
 383 
 384                 XSSimpleType anyURI = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_ANYURI);
 385 
 386                 // xsi:schemaLocation
 387                 name = SchemaSymbols.XSI_SCHEMALOCATION;
 388                 tns = SchemaSymbols.URI_XSI;
 389                 type = schemaFactory.createTypeList("#AnonType_schemaLocation", SchemaSymbols.URI_XSI, (short)0, anyURI, null);
 390                 if (type instanceof XSSimpleTypeDecl) {
 391                     ((XSSimpleTypeDecl)type).setAnonymous(true);
 392                 }
 393                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
 394 
 395                 // xsi:noNamespaceSchemaLocation
 396                 name = SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION;
 397                 tns = SchemaSymbols.URI_XSI;
 398                 type = anyURI;
 399                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
 400             }
 401         } // <init>(int)
 402 
 403         // return the XMLGrammarDescription corresponding to this
 404         // object
 405         public XMLGrammarDescription getGrammarDescription() {
 406             return fGrammarDescription.makeClone();
 407         } // getGrammarDescription():  XMLGrammarDescription
 408 
 409         // override these methods solely so that these
 410         // objects cannot be modified once they're created.
 411         public void setImportedGrammars(List<SchemaGrammar> importedGrammars) {
 412             // ignore
 413         }
 414         public void addGlobalAttributeDecl(XSAttributeDecl decl) {
 415             // ignore
 416         }
 417         public void addGlobalAttributeDecl(XSAttributeDecl decl, String location) {
 418             // ignore
 419         }
 420         public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
 421             // ignore
 422         }
 423         public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl, String location) {
 424             // ignore
 425         }
 426         public void addGlobalElementDecl(XSElementDecl decl) {
 427             // ignore
 428         }
 429         public void addGlobalElementDecl(XSElementDecl decl, String location) {
 430             // ignore
 431         }
 432         public void addGlobalElementDeclAll(XSElementDecl decl) {
 433             // ignore
 434         }
 435         public void addGlobalGroupDecl(XSGroupDecl decl) {
 436             // ignore
 437         }
 438         public void addGlobalGroupDecl(XSGroupDecl decl, String location) {
 439             // ignore
 440         }
 441         public void addGlobalNotationDecl(XSNotationDecl decl) {
 442             // ignore
 443         }
 444         public void addGlobalNotationDecl(XSNotationDecl decl, String location) {
 445             // ignore
 446         }
 447         public void addGlobalTypeDecl(XSTypeDefinition decl) {
 448             // ignore
 449         }
 450         public void addGlobalTypeDecl(XSTypeDefinition decl, String location) {
 451             // ignore
 452         }
 453         public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl) {
 454             // ignore
 455         }
 456         public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl, String location) {
 457             // ignore
 458         }
 459         public void addGlobalSimpleTypeDecl(XSSimpleType decl) {
 460             // ignore
 461         }
 462         public void addGlobalSimpleTypeDecl(XSSimpleType decl, String location) {
 463             // ignore
 464         }
 465         public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
 466             // ignore
 467         }
 468         public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
 469             // ignore
 470         }
 471         public synchronized void addDocument(Object document, String location) {
 472             // ignore
 473         }
 474 
 475         // annotation support
 476         synchronized DOMParser getDOMParser() {
 477             return null;
 478         }
 479         synchronized SAXParser getSAXParser() {
 480             return null;
 481         }
 482     }
 483 
 484     /**
 485      * <p>A partial schema for schemas for validating annotations.</p>
 486      *
 487      * @xerces.internal
 488      *
 489      * @author Michael Glavassevich, IBM
 490      */
 491     public static final class Schema4Annotations extends SchemaGrammar {
 492 
 493         /**
 494          * Singleton instance.
 495          */
 496         public static final Schema4Annotations INSTANCE = new Schema4Annotations();
 497 
 498         /**
 499          * Special constructor to create a schema
 500          * capable of validating annotations.
 501          */
 502         private Schema4Annotations() {
 503 
 504             // target namespace
 505             fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
 506 
 507             // grammar description
 508             fGrammarDescription = new XSDDescription();
 509             fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
 510             fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
 511 
 512             // no global decls other than types and
 513             // element declarations for <annotation>, <documentation> and <appinfo>.
 514             fGlobalAttrDecls  = new SymbolHash(1);
 515             fGlobalAttrGrpDecls = new SymbolHash(1);
 516             fGlobalElemDecls = new SymbolHash(6);
 517             fGlobalGroupDecls = new SymbolHash(1);
 518             fGlobalNotationDecls = new SymbolHash(1);
 519             fGlobalIDConstraintDecls = new SymbolHash(1);
 520 
 521             // no extended global decls
 522             fGlobalAttrDeclsExt  = new SymbolHash(1);
 523             fGlobalAttrGrpDeclsExt = new SymbolHash(1);
 524             fGlobalElemDeclsExt = new SymbolHash(6);
 525             fGlobalGroupDeclsExt = new SymbolHash(1);
 526             fGlobalNotationDeclsExt = new SymbolHash(1);
 527             fGlobalIDConstraintDeclsExt = new SymbolHash(1);
 528             fGlobalTypeDeclsExt = new SymbolHash(1);
 529 
 530             // all global element declarations
 531             fAllGlobalElemDecls = new SymbolHash(6);
 532 
 533             // get all built-in types
 534             fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls;
 535 
 536             // create element declarations for <annotation>, <documentation> and <appinfo>
 537             XSElementDecl annotationDecl = createAnnotationElementDecl(SchemaSymbols.ELT_ANNOTATION);
 538             XSElementDecl documentationDecl = createAnnotationElementDecl(SchemaSymbols.ELT_DOCUMENTATION);
 539             XSElementDecl appinfoDecl = createAnnotationElementDecl(SchemaSymbols.ELT_APPINFO);
 540 
 541             // add global element declarations
 542             fGlobalElemDecls.put(annotationDecl.fName, annotationDecl);
 543             fGlobalElemDecls.put(documentationDecl.fName, documentationDecl);
 544             fGlobalElemDecls.put(appinfoDecl.fName, appinfoDecl);
 545 
 546             fGlobalElemDeclsExt.put(","+annotationDecl.fName, annotationDecl);
 547             fGlobalElemDeclsExt.put(","+documentationDecl.fName, documentationDecl);
 548             fGlobalElemDeclsExt.put(","+appinfoDecl.fName, appinfoDecl);
 549 
 550             fAllGlobalElemDecls.put(annotationDecl, annotationDecl);
 551             fAllGlobalElemDecls.put(documentationDecl, documentationDecl);
 552             fAllGlobalElemDecls.put(appinfoDecl, appinfoDecl);
 553 
 554             // create complex type declarations for <annotation>, <documentation> and <appinfo>
 555             XSComplexTypeDecl annotationType = new XSComplexTypeDecl();
 556             XSComplexTypeDecl documentationType = new XSComplexTypeDecl();
 557             XSComplexTypeDecl appinfoType = new XSComplexTypeDecl();
 558 
 559             // set the types on their element declarations
 560             annotationDecl.fType = annotationType;
 561             documentationDecl.fType = documentationType;
 562             appinfoDecl.fType = appinfoType;
 563 
 564             // create attribute groups for <annotation>, <documentation> and <appinfo>
 565             XSAttributeGroupDecl annotationAttrs = new XSAttributeGroupDecl();
 566             XSAttributeGroupDecl documentationAttrs = new XSAttributeGroupDecl();
 567             XSAttributeGroupDecl appinfoAttrs = new XSAttributeGroupDecl();
 568 
 569             // fill in attribute groups
 570             {
 571                 // create and fill attribute uses for <annotation>, <documentation> and <appinfo>
 572                 XSAttributeUseImpl annotationIDAttr = new XSAttributeUseImpl();
 573                 annotationIDAttr.fAttrDecl = new XSAttributeDecl();
 574                 annotationIDAttr.fAttrDecl.setValues(SchemaSymbols.ATT_ID, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ID),
 575                         XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, annotationType, null);
 576                 annotationIDAttr.fUse = SchemaSymbols.USE_OPTIONAL;
 577                 annotationIDAttr.fConstraintType = XSConstants.VC_NONE;
 578 
 579                 XSAttributeUseImpl documentationSourceAttr = new XSAttributeUseImpl();
 580                 documentationSourceAttr.fAttrDecl = new XSAttributeDecl();
 581                 documentationSourceAttr.fAttrDecl.setValues(SchemaSymbols.ATT_SOURCE, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ANYURI),
 582                         XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, documentationType, null);
 583                 documentationSourceAttr.fUse = SchemaSymbols.USE_OPTIONAL;
 584                 documentationSourceAttr.fConstraintType = XSConstants.VC_NONE;
 585 
 586                 XSAttributeUseImpl documentationLangAttr = new XSAttributeUseImpl();
 587                 documentationLangAttr.fAttrDecl = new XSAttributeDecl();
 588                 documentationLangAttr.fAttrDecl.setValues("lang".intern(), NamespaceContext.XML_URI, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_LANGUAGE),
 589                         XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, documentationType, null);
 590                 documentationLangAttr.fUse = SchemaSymbols.USE_OPTIONAL;
 591                 documentationLangAttr.fConstraintType = XSConstants.VC_NONE;
 592 
 593                 XSAttributeUseImpl appinfoSourceAttr = new XSAttributeUseImpl();
 594                 appinfoSourceAttr.fAttrDecl = new XSAttributeDecl();
 595                 appinfoSourceAttr.fAttrDecl.setValues(SchemaSymbols.ATT_SOURCE, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ANYURI),
 596                         XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, appinfoType, null);
 597                 appinfoSourceAttr.fUse = SchemaSymbols.USE_OPTIONAL;
 598                 appinfoSourceAttr.fConstraintType = XSConstants.VC_NONE;
 599 
 600                 // create lax attribute wildcard for <annotation>, <documentation> and <appinfo>
 601                 XSWildcardDecl otherAttrs = new XSWildcardDecl();
 602                 otherAttrs.fNamespaceList = new String [] {fTargetNamespace, null};
 603                 otherAttrs.fType = XSWildcard.NSCONSTRAINT_NOT;
 604                 otherAttrs.fProcessContents = XSWildcard.PC_LAX;
 605 
 606                 // add attribute uses and wildcards to attribute groups for <annotation>, <documentation> and <appinfo>
 607                 annotationAttrs.addAttributeUse(annotationIDAttr);
 608                 annotationAttrs.fAttributeWC = otherAttrs;
 609 
 610                 documentationAttrs.addAttributeUse(documentationSourceAttr);
 611                 documentationAttrs.addAttributeUse(documentationLangAttr);
 612                 documentationAttrs.fAttributeWC = otherAttrs;
 613 
 614                 appinfoAttrs.addAttributeUse(appinfoSourceAttr);
 615                 appinfoAttrs.fAttributeWC = otherAttrs;
 616             }
 617 
 618             // create particles for <annotation>
 619             XSParticleDecl annotationParticle = createUnboundedModelGroupParticle();
 620             {
 621                 XSModelGroupImpl annotationChoice = new XSModelGroupImpl();
 622                 annotationChoice.fCompositor = XSModelGroupImpl.MODELGROUP_CHOICE;
 623                 annotationChoice.fParticleCount = 2;
 624                 annotationChoice.fParticles = new XSParticleDecl[2];
 625                 annotationChoice.fParticles[0] = createChoiceElementParticle(appinfoDecl);
 626                 annotationChoice.fParticles[1] = createChoiceElementParticle(documentationDecl);
 627                 annotationParticle.fValue = annotationChoice;
 628             }
 629 
 630             // create wildcard particle for <documentation> and <appinfo>
 631             XSParticleDecl anyWCSequenceParticle = createUnboundedAnyWildcardSequenceParticle();
 632 
 633             // fill complex types
 634             annotationType.setValues("#AnonType_" + SchemaSymbols.ELT_ANNOTATION, fTargetNamespace, SchemaGrammar.fAnyType,
 635                     XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
 636                     XSComplexTypeDecl.CONTENTTYPE_ELEMENT, false, annotationAttrs, null, annotationParticle, XSObjectListImpl.EMPTY_LIST);
 637             annotationType.setName("#AnonType_" + SchemaSymbols.ELT_ANNOTATION);
 638             annotationType.setIsAnonymous();
 639 
 640             documentationType.setValues("#AnonType_" + SchemaSymbols.ELT_DOCUMENTATION, fTargetNamespace, SchemaGrammar.fAnyType,
 641                     XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
 642                     XSComplexTypeDecl.CONTENTTYPE_MIXED, false, documentationAttrs, null, anyWCSequenceParticle, XSObjectListImpl.EMPTY_LIST);
 643             documentationType.setName("#AnonType_" + SchemaSymbols.ELT_DOCUMENTATION);
 644             documentationType.setIsAnonymous();
 645 
 646             appinfoType.setValues("#AnonType_" + SchemaSymbols.ELT_APPINFO, fTargetNamespace, SchemaGrammar.fAnyType,
 647                     XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
 648                     XSComplexTypeDecl.CONTENTTYPE_MIXED, false, appinfoAttrs, null, anyWCSequenceParticle, XSObjectListImpl.EMPTY_LIST);
 649             appinfoType.setName("#AnonType_" + SchemaSymbols.ELT_APPINFO);
 650             appinfoType.setIsAnonymous();
 651 
 652         } // <init>(int)
 653 
 654         // return the XMLGrammarDescription corresponding to this
 655         // object
 656         public XMLGrammarDescription getGrammarDescription() {
 657             return fGrammarDescription.makeClone();
 658         } // getGrammarDescription():  XMLGrammarDescription
 659 
 660         // override these methods solely so that these
 661         // objects cannot be modified once they're created.
 662         public void setImportedGrammars(List<SchemaGrammar> importedGrammars) {
 663             // ignore
 664         }
 665         public void addGlobalAttributeDecl(XSAttributeDecl decl) {
 666             // ignore
 667         }
 668         public void addGlobalAttributeDecl(XSAttributeGroupDecl decl, String location) {
 669             // ignore
 670         }
 671         public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
 672             // ignore
 673         }
 674         public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl, String location) {
 675             // ignore
 676         }
 677         public void addGlobalElementDecl(XSElementDecl decl) {
 678             // ignore
 679         }
 680         public void addGlobalElementDecl(XSElementDecl decl, String location) {
 681             // ignore
 682         }
 683         public void addGlobalElementDeclAll(XSElementDecl decl) {
 684             // ignore
 685         }
 686         public void addGlobalGroupDecl(XSGroupDecl decl) {
 687             // ignore
 688         }
 689         public void addGlobalGroupDecl(XSGroupDecl decl, String location) {
 690             // ignore
 691         }
 692         public void addGlobalNotationDecl(XSNotationDecl decl) {
 693             // ignore
 694         }
 695         public void addGlobalNotationDecl(XSNotationDecl decl, String location) {
 696             // ignore
 697         }
 698         public void addGlobalTypeDecl(XSTypeDefinition decl) {
 699             // ignore
 700         }
 701         public void addGlobalTypeDecl(XSTypeDefinition decl, String location) {
 702             // ignore
 703         }
 704         public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl) {
 705             // ignore
 706         }
 707         public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl, String location) {
 708             // ignore
 709         }
 710         public void addGlobalSimpleTypeDecl(XSSimpleType decl) {
 711             // ignore
 712         }
 713         public void addGlobalSimpleTypeDecl(XSSimpleType decl, String location) {
 714             // ignore
 715         }
 716         public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
 717             // ignore
 718         }
 719         public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
 720             // ignore
 721         }
 722         public synchronized void addDocument(Object document, String location) {
 723             // ignore
 724         }
 725 
 726         // annotation support
 727         synchronized DOMParser getDOMParser() {
 728             return null;
 729         }
 730         synchronized SAXParser getSAXParser() {
 731             return null;
 732         }
 733 
 734         //
 735         // private helper methods
 736         //
 737 
 738         private XSElementDecl createAnnotationElementDecl(String localName) {
 739             XSElementDecl eDecl = new XSElementDecl();
 740             eDecl.fName = localName;
 741             eDecl.fTargetNamespace = fTargetNamespace;
 742             eDecl.setIsGlobal();
 743             eDecl.fBlock = (XSConstants.DERIVATION_EXTENSION |
 744                     XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION);
 745             eDecl.setConstraintType(XSConstants.VC_NONE);
 746             return eDecl;
 747         }
 748 
 749         private XSParticleDecl createUnboundedModelGroupParticle() {
 750             XSParticleDecl particle = new XSParticleDecl();
 751             particle.fMinOccurs = 0;
 752             particle.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
 753             particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
 754             return particle;
 755         }
 756 
 757         private XSParticleDecl createChoiceElementParticle(XSElementDecl ref) {
 758             XSParticleDecl particle = new XSParticleDecl();
 759             particle.fMinOccurs = 1;
 760             particle.fMaxOccurs = 1;
 761             particle.fType = XSParticleDecl.PARTICLE_ELEMENT;
 762             particle.fValue = ref;
 763             return particle;
 764         }
 765 
 766         private XSParticleDecl createUnboundedAnyWildcardSequenceParticle() {
 767             XSParticleDecl particle = createUnboundedModelGroupParticle();
 768             XSModelGroupImpl sequence = new XSModelGroupImpl();
 769             sequence.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
 770             sequence.fParticleCount = 1;
 771             sequence.fParticles = new XSParticleDecl[1];
 772             sequence.fParticles[0] = createAnyLaxWildcardParticle();
 773             particle.fValue = sequence;
 774             return particle;
 775         }
 776 
 777         private XSParticleDecl createAnyLaxWildcardParticle() {
 778             XSParticleDecl particle = new XSParticleDecl();
 779             particle.fMinOccurs = 1;
 780             particle.fMaxOccurs = 1;
 781             particle.fType = XSParticleDecl.PARTICLE_WILDCARD;
 782 
 783             XSWildcardDecl anyWC = new XSWildcardDecl();
 784             anyWC.fNamespaceList = null;
 785             anyWC.fType = XSWildcard.NSCONSTRAINT_ANY;
 786             anyWC.fProcessContents = XSWildcard.PC_LAX;
 787 
 788             particle.fValue = anyWC;
 789             return particle;
 790         }
 791     }
 792 
 793     // Grammar methods
 794 
 795     // return the XMLGrammarDescription corresponding to this
 796     // object
 797     public XMLGrammarDescription getGrammarDescription() {
 798         return fGrammarDescription;
 799     } // getGrammarDescription():  XMLGrammarDescription
 800 
 801     // DTDGrammar methods
 802     public boolean isNamespaceAware () {
 803         return true;
 804     } // isNamespaceAware():boolean
 805 
 806     List<SchemaGrammar> fImported = null;
 807 
 808     public void setImportedGrammars(List<SchemaGrammar> importedGrammars) {
 809         fImported = importedGrammars;
 810     }
 811 
 812     public List<SchemaGrammar> getImportedGrammars() {
 813         return fImported;
 814     }
 815 
 816     /**
 817      * Returns this grammar's target namespace.
 818      */
 819     public final String getTargetNamespace() {
 820         return fTargetNamespace;
 821     } // getTargetNamespace():String
 822 
 823     /**
 824      * register one global attribute
 825      */
 826     public void addGlobalAttributeDecl(XSAttributeDecl decl) {
 827         fGlobalAttrDecls.put(decl.fName, decl);
 828         decl.setNamespaceItem(this);
 829     }
 830 
 831     public void addGlobalAttributeDecl(XSAttributeDecl decl, String location) {
 832         fGlobalAttrDeclsExt.put(((location!=null) ? location : "") + "," + decl.fName, decl);
 833         if (decl.getNamespaceItem() == null) {
 834             decl.setNamespaceItem(this);
 835         }
 836     }
 837 
 838     /**
 839      * register one global attribute group
 840      */
 841     public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
 842         fGlobalAttrGrpDecls.put(decl.fName, decl);
 843         decl.setNamespaceItem(this);
 844     }
 845 
 846     public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl, String location) {
 847         fGlobalAttrGrpDeclsExt.put(((location!=null) ? location : "") + "," + decl.fName, decl);
 848         if (decl.getNamespaceItem() == null) {
 849             decl.setNamespaceItem(this);
 850         }
 851     }
 852 
 853     /**
 854      * register one global element
 855      */
 856     public void addGlobalElementDeclAll(XSElementDecl decl) {
 857         if (fAllGlobalElemDecls.get(decl) == null) {
 858             fAllGlobalElemDecls.put(decl, decl);
 859             // if there is a substitution group affiliation, store in an array,
 860             // for further constraint checking: UPA, PD, EDC
 861             if (decl.fSubGroup != null) {
 862                if (fSubGroupCount == fSubGroups.length)
 863                     fSubGroups = resize(fSubGroups, fSubGroupCount+INC_SIZE);
 864                 fSubGroups[fSubGroupCount++] = decl;
 865             }
 866         }
 867     }
 868 
 869     public void addGlobalElementDecl(XSElementDecl decl) {
 870         fGlobalElemDecls.put(decl.fName, decl);
 871         decl.setNamespaceItem(this);
 872     }
 873 
 874     public void addGlobalElementDecl(XSElementDecl decl, String location) {
 875         fGlobalElemDeclsExt.put(((location != null) ? location : "") + "," + decl.fName, decl);
 876         if (decl.getNamespaceItem() == null) {
 877             decl.setNamespaceItem(this);
 878         }
 879     }
 880 
 881     /**
 882      * register one global group
 883      */
 884     public void addGlobalGroupDecl(XSGroupDecl decl) {
 885         fGlobalGroupDecls.put(decl.fName, decl);
 886         decl.setNamespaceItem(this);
 887     }
 888 
 889     public void addGlobalGroupDecl(XSGroupDecl decl, String location) {
 890         fGlobalGroupDeclsExt.put(((location!=null) ? location : "") + "," + decl.fName, decl);
 891         if (decl.getNamespaceItem() == null) {
 892             decl.setNamespaceItem(this);
 893         }
 894     }
 895 
 896     /**
 897      * register one global notation
 898      */
 899     public void addGlobalNotationDecl(XSNotationDecl decl) {
 900         fGlobalNotationDecls.put(decl.fName, decl);
 901         decl.setNamespaceItem(this);
 902     }
 903 
 904     public void addGlobalNotationDecl(XSNotationDecl decl, String location) {
 905         fGlobalNotationDeclsExt.put(((location!=null) ? location : "") + "," +decl.fName, decl);
 906         if (decl.getNamespaceItem() == null) {
 907             decl.setNamespaceItem(this);
 908         }
 909     }
 910 
 911     /**
 912      * register one global type
 913      */
 914     public void addGlobalTypeDecl(XSTypeDefinition decl) {
 915         fGlobalTypeDecls.put(decl.getName(), decl);
 916         if (decl instanceof XSComplexTypeDecl) {
 917             ((XSComplexTypeDecl) decl).setNamespaceItem(this);
 918         }
 919         else if (decl instanceof XSSimpleTypeDecl) {
 920             ((XSSimpleTypeDecl) decl).setNamespaceItem(this);
 921         }
 922     }
 923 
 924     public void addGlobalTypeDecl(XSTypeDefinition decl, String location) {
 925         fGlobalTypeDeclsExt.put(((location!=null) ? location : "") + "," + decl.getName(), decl);
 926         if (decl.getNamespaceItem() == null) {
 927             if (decl instanceof XSComplexTypeDecl) {
 928                 ((XSComplexTypeDecl) decl).setNamespaceItem(this);
 929             }
 930             else if (decl instanceof XSSimpleTypeDecl) {
 931                 ((XSSimpleTypeDecl) decl).setNamespaceItem(this);
 932             }
 933         }
 934     }
 935 
 936     /**
 937      * register one global complex type
 938      */
 939     public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl) {
 940         fGlobalTypeDecls.put(decl.getName(), decl);
 941         decl.setNamespaceItem(this);
 942     }
 943 
 944     public void addGlobalComplexTypeDecl(XSComplexTypeDecl decl, String location) {
 945         fGlobalTypeDeclsExt.put(((location!=null) ? location : "") + "," + decl.getName(), decl);
 946         if (decl.getNamespaceItem() == null) {
 947             decl.setNamespaceItem(this);
 948         }
 949     }
 950 
 951     /**
 952      * register one global simple type
 953      */
 954     public void addGlobalSimpleTypeDecl(XSSimpleType decl) {
 955         fGlobalTypeDecls.put(decl.getName(), decl);
 956         if (decl instanceof XSSimpleTypeDecl) {
 957             ((XSSimpleTypeDecl) decl).setNamespaceItem(this);
 958         }
 959     }
 960 
 961     public void addGlobalSimpleTypeDecl(XSSimpleType decl, String location) {
 962         fGlobalTypeDeclsExt.put(((location != null) ? location : "") + "," + decl.getName(), decl);
 963         if (decl.getNamespaceItem() == null && decl instanceof XSSimpleTypeDecl) {
 964             ((XSSimpleTypeDecl) decl).setNamespaceItem(this);
 965         }
 966     }
 967 
 968     /**
 969      * register one identity constraint
 970      */
 971     public final void addIDConstraintDecl(XSElementDecl elmDecl, IdentityConstraint decl) {
 972         elmDecl.addIDConstraint(decl);
 973         fGlobalIDConstraintDecls.put(decl.getIdentityConstraintName(), decl);
 974     }
 975 
 976     public final void addIDConstraintDecl(XSElementDecl elmDecl, IdentityConstraint decl, String location) {
 977         fGlobalIDConstraintDeclsExt.put(((location != null) ? location : "") + "," + decl.getIdentityConstraintName(), decl);
 978     }
 979 
 980     /**
 981      * get one global attribute
 982      */
 983     public final XSAttributeDecl getGlobalAttributeDecl(String declName) {
 984         return(XSAttributeDecl)fGlobalAttrDecls.get(declName);
 985     }
 986 
 987     public final XSAttributeDecl getGlobalAttributeDecl(String declName, String location) {
 988         return(XSAttributeDecl)fGlobalAttrDeclsExt.get(((location != null) ? location : "") + "," + declName);
 989     }
 990 
 991     /**
 992      * get one global attribute group
 993      */
 994     public final XSAttributeGroupDecl getGlobalAttributeGroupDecl(String declName) {
 995         return(XSAttributeGroupDecl)fGlobalAttrGrpDecls.get(declName);
 996     }
 997 
 998     public final XSAttributeGroupDecl getGlobalAttributeGroupDecl(String declName, String location) {
 999         return(XSAttributeGroupDecl)fGlobalAttrGrpDeclsExt.get(((location != null) ? location : "") + "," + declName);
1000     }
1001 
1002     /**
1003      * get one global element
1004      */
1005     public final XSElementDecl getGlobalElementDecl(String declName) {
1006         return(XSElementDecl)fGlobalElemDecls.get(declName);
1007     }
1008 
1009     public final XSElementDecl getGlobalElementDecl(String declName, String location) {
1010         return(XSElementDecl)fGlobalElemDeclsExt.get(((location != null) ? location : "") + "," + declName);
1011     }
1012 
1013     /**
1014      * get one global group
1015      */
1016     public final XSGroupDecl getGlobalGroupDecl(String declName) {
1017         return(XSGroupDecl)fGlobalGroupDecls.get(declName);
1018     }
1019 
1020     public final XSGroupDecl getGlobalGroupDecl(String declName, String location) {
1021         return(XSGroupDecl)fGlobalGroupDeclsExt.get(((location != null) ? location : "") + "," + declName);
1022     }
1023 
1024     /**
1025      * get one global notation
1026      */
1027     public final XSNotationDecl getGlobalNotationDecl(String declName) {
1028         return(XSNotationDecl)fGlobalNotationDecls.get(declName);
1029     }
1030 
1031     public final XSNotationDecl getGlobalNotationDecl(String declName, String location) {
1032         return(XSNotationDecl)fGlobalNotationDeclsExt.get(((location != null) ? location : "") + "," + declName);
1033     }
1034 
1035     /**
1036      * get one global type
1037      */
1038     public final XSTypeDefinition getGlobalTypeDecl(String declName) {
1039         return(XSTypeDefinition)fGlobalTypeDecls.get(declName);
1040     }
1041 
1042     public final XSTypeDefinition getGlobalTypeDecl(String declName, String location) {
1043         return(XSTypeDefinition)fGlobalTypeDeclsExt.get(((location != null) ? location : "") + "," + declName);
1044     }
1045 
1046     /**
1047      * get one identity constraint
1048      */
1049     public final IdentityConstraint getIDConstraintDecl(String declName) {
1050         return(IdentityConstraint)fGlobalIDConstraintDecls.get(declName);
1051     }
1052 
1053     public final IdentityConstraint getIDConstraintDecl(String declName, String location) {
1054         return(IdentityConstraint)fGlobalIDConstraintDeclsExt.get(((location != null) ? location : "") + "," + declName);
1055     }
1056 
1057     /**
1058      * get one identity constraint
1059      */
1060     public final boolean hasIDConstraints() {
1061         return fGlobalIDConstraintDecls.getLength() > 0;
1062     }
1063 
1064     // array to store complex type decls
1065     private static final int INITIAL_SIZE = 16;
1066     private static final int INC_SIZE     = 16;
1067 
1068     private int fCTCount = 0;
1069     private XSComplexTypeDecl[] fComplexTypeDecls = new XSComplexTypeDecl[INITIAL_SIZE];
1070     private SimpleLocator[] fCTLocators = new SimpleLocator[INITIAL_SIZE];
1071 
1072     // an array to store groups being redefined by restriction
1073     // even-numbered elements are the derived groups, odd-numbered ones their bases
1074     private static final int REDEFINED_GROUP_INIT_SIZE = 2;
1075     private int fRGCount = 0;
1076     private XSGroupDecl[] fRedefinedGroupDecls = new XSGroupDecl[REDEFINED_GROUP_INIT_SIZE];
1077     private SimpleLocator[] fRGLocators = new SimpleLocator[REDEFINED_GROUP_INIT_SIZE/2];
1078 
1079     // a flag to indicate whether we have checked the 3 constraints on this
1080     // grammar.
1081     boolean fFullChecked = false;
1082 
1083     /**
1084      * add one complex type decl: for later constraint checking
1085      */
1086     public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
1087         if (fCTCount == fComplexTypeDecls.length) {
1088             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount+INC_SIZE);
1089             fCTLocators = resize(fCTLocators, fCTCount+INC_SIZE);
1090         }
1091         fCTLocators[fCTCount] = locator;
1092         fComplexTypeDecls[fCTCount++] = decl;
1093     }
1094 
1095     /**
1096      * add a group redefined by restriction: for later constraint checking
1097      */
1098     public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
1099         if (fRGCount == fRedefinedGroupDecls.length) {
1100             // double array size each time.
1101             fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount << 1);
1102             fRGLocators = resize(fRGLocators, fRGCount);
1103         }
1104         fRGLocators[fRGCount/2] = locator;
1105         fRedefinedGroupDecls[fRGCount++] = derived;
1106         fRedefinedGroupDecls[fRGCount++] = base;
1107     }
1108 
1109     /**
1110      * get all complex type decls: for later constraint checking
1111      */
1112     final XSComplexTypeDecl[] getUncheckedComplexTypeDecls() {
1113         if (fCTCount < fComplexTypeDecls.length) {
1114             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
1115             fCTLocators = resize(fCTLocators, fCTCount);
1116         }
1117         return fComplexTypeDecls;
1118     }
1119 
1120     /**
1121      * get the error locator of all complex type decls
1122      */
1123     final SimpleLocator[] getUncheckedCTLocators() {
1124         if (fCTCount < fCTLocators.length) {
1125             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
1126             fCTLocators = resize(fCTLocators, fCTCount);
1127         }
1128         return fCTLocators;
1129     }
1130 
1131     /**
1132      * get all redefined groups: for later constraint checking
1133      */
1134     final XSGroupDecl[] getRedefinedGroupDecls() {
1135         if (fRGCount < fRedefinedGroupDecls.length) {
1136             fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
1137             fRGLocators = resize(fRGLocators, fRGCount/2);
1138         }
1139         return fRedefinedGroupDecls;
1140     }
1141 
1142     /**
1143      * get the error locator of all redefined groups
1144      */
1145     final SimpleLocator[] getRGLocators() {
1146         if (fRGCount < fRedefinedGroupDecls.length) {
1147             fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
1148             fRGLocators = resize(fRGLocators, fRGCount/2);
1149         }
1150         return fRGLocators;
1151     }
1152 
1153     /**
1154      * after the first-round checking, some types don't need to be checked
1155      * against UPA again. here we trim the array to the proper size.
1156      */
1157     final void setUncheckedTypeNum(int newSize) {
1158         fCTCount = newSize;
1159         fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
1160         fCTLocators = resize(fCTLocators, fCTCount);
1161     }
1162 
1163     // used to store all substitution group information declared in
1164     // this namespace
1165     private int fSubGroupCount = 0;
1166     private XSElementDecl[] fSubGroups = new XSElementDecl[INITIAL_SIZE];
1167 
1168     /**
1169      * get all substitution group information: for the 3 constraint checking
1170      */
1171     final XSElementDecl[] getSubstitutionGroups() {
1172         if (fSubGroupCount < fSubGroups.length)
1173             fSubGroups = resize(fSubGroups, fSubGroupCount);
1174         return fSubGroups;
1175     }
1176 
1177     // anyType and anySimpleType: because there are so many places where
1178     // we need direct access to these two types
1179     public final static XSComplexTypeDecl fAnyType = new XSAnyType();
1180     private static class XSAnyType extends XSComplexTypeDecl {
1181         public XSAnyType () {
1182             fName = SchemaSymbols.ATTVAL_ANYTYPE;
1183             super.fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
1184             fBaseType = this;
1185             fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
1186             fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
1187 
1188             fParticle = createParticle();
1189             fAttrGrp = createAttrGrp();
1190         }
1191 
1192         // overridden methods
1193         public void setValues(String name, String targetNamespace,
1194                 XSTypeDefinition baseType, short derivedBy, short schemaFinal,
1195                 short block, short contentType,
1196                 boolean isAbstract, XSAttributeGroupDecl attrGrp,
1197                 XSSimpleType simpleType, XSParticleDecl particle) {
1198             // don't allow this.
1199         }
1200 
1201         public void setName(String name){
1202             // don't allow this.
1203         }
1204 
1205         public void setIsAbstractType() {
1206             // null implementation
1207         }
1208 
1209         public void setContainsTypeID() {
1210             // null implementation
1211         }
1212 
1213         public void setIsAnonymous() {
1214             // null implementation
1215         }
1216 
1217         public void reset() {
1218             // null implementation
1219         }
1220 
1221         public XSObjectList getAnnotations() {
1222             return XSObjectListImpl.EMPTY_LIST;
1223         }
1224 
1225         public XSNamespaceItem getNamespaceItem() {
1226             return SG_SchemaNS;
1227         }
1228 
1229         private XSAttributeGroupDecl createAttrGrp() {
1230             XSWildcardDecl wildcard = new XSWildcardDecl();
1231             wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
1232             XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
1233             attrGrp.fAttributeWC = wildcard;
1234             return attrGrp;
1235         }
1236 
1237         private XSParticleDecl createParticle() {
1238             // the wildcard used in anyType (content and attribute)
1239             // the spec will change strict to skip for anyType
1240             XSWildcardDecl wildcard = new XSWildcardDecl();
1241             wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
1242             // the particle for the content wildcard
1243             XSParticleDecl particleW = new XSParticleDecl();
1244             particleW.fMinOccurs = 0;
1245             particleW.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
1246             particleW.fType = XSParticleDecl.PARTICLE_WILDCARD;
1247             particleW.fValue = wildcard;
1248             // the model group of a sequence of the above particle
1249             XSModelGroupImpl group = new XSModelGroupImpl();
1250             group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
1251             group.fParticleCount = 1;
1252             group.fParticles = new XSParticleDecl[1];
1253             group.fParticles[0] = particleW;
1254             // the content of anyType: particle of the above model group
1255             XSParticleDecl particleG = new XSParticleDecl();
1256             particleG.fType = XSParticleDecl.PARTICLE_MODELGROUP;
1257             particleG.fValue = group;
1258 
1259             return particleG;
1260         }
1261     }
1262     private static class BuiltinAttrDecl extends XSAttributeDecl {
1263         public BuiltinAttrDecl(String name, String tns,
1264                 XSSimpleType type, short scope) {
1265             fName = name;
1266             super.fTargetNamespace = tns;
1267             fType = type;
1268             fScope = scope;
1269         }
1270 
1271         public void setValues(String name, String targetNamespace,
1272                 XSSimpleType simpleType, short constraintType, short scope,
1273                 ValidatedInfo valInfo, XSComplexTypeDecl enclosingCT) {
1274             // ignore this call.
1275         }
1276 
1277         public void reset () {
1278             // also ignore this call.
1279         }
1280 
1281         public XSAnnotation getAnnotation() {
1282             return null;
1283         }
1284 
1285         public XSNamespaceItem getNamespaceItem() {
1286             return SG_XSI;
1287         }
1288 
1289     } // class BuiltinAttrDecl
1290 
1291     // the grammars to hold components of the schema namespace
1292     public final static BuiltinSchemaGrammar SG_SchemaNS = new BuiltinSchemaGrammar(GRAMMAR_XS, Constants.SCHEMA_VERSION_1_0);
1293     private final static BuiltinSchemaGrammar SG_SchemaNSExtended = new BuiltinSchemaGrammar(GRAMMAR_XS, Constants.SCHEMA_VERSION_1_0_EXTENDED);
1294 
1295     public final static XSSimpleType fAnySimpleType = (XSSimpleType)SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_ANYSIMPLETYPE);
1296 
1297     // the grammars to hold components of the schema-instance namespace
1298     public final static BuiltinSchemaGrammar SG_XSI = new BuiltinSchemaGrammar(GRAMMAR_XSI, Constants.SCHEMA_VERSION_1_0);
1299 
1300     public static SchemaGrammar getS4SGrammar(short schemaVersion) {
1301         if (schemaVersion == Constants.SCHEMA_VERSION_1_0) {
1302             return SG_SchemaNS;
1303         }
1304         else {
1305             return SG_SchemaNSExtended;
1306         }
1307     }
1308 
1309     static final XSComplexTypeDecl[] resize(XSComplexTypeDecl[] oldArray, int newSize) {
1310         XSComplexTypeDecl[] newArray = new XSComplexTypeDecl[newSize];
1311         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
1312         return newArray;
1313     }
1314 
1315     static final XSGroupDecl[] resize(XSGroupDecl[] oldArray, int newSize) {
1316         XSGroupDecl[] newArray = new XSGroupDecl[newSize];
1317         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
1318         return newArray;
1319     }
1320 
1321     static final XSElementDecl[] resize(XSElementDecl[] oldArray, int newSize) {
1322         XSElementDecl[] newArray = new XSElementDecl[newSize];
1323         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
1324         return newArray;
1325     }
1326 
1327     static final SimpleLocator[] resize(SimpleLocator[] oldArray, int newSize) {
1328         SimpleLocator[] newArray = new SimpleLocator[newSize];
1329         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
1330         return newArray;
1331     }
1332 
1333     // XSNamespaceItem methods
1334 
1335     // the max index / the max value of XSObject type
1336     private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE;
1337     private static final boolean[] GLOBAL_COMP = {false,    // null
1338                                                   true,     // attribute
1339                                                   true,     // element
1340                                                   true,     // type
1341                                                   false,    // attribute use
1342                                                   true,     // attribute group
1343                                                   true,     // group
1344                                                   false,    // model group
1345                                                   false,    // particle
1346                                                   false,    // wildcard
1347                                                   true,    // idc
1348                                                   true,     // notation
1349                                                   false,    // annotation
1350                                                   false,    // facet
1351                                                   false,    // multi value facet
1352                                                   true,     // complex type
1353                                                   true      // simple type
1354                                                  };
1355 
1356     // store a certain kind of components from all namespaces
1357     private XSNamedMap[] fComponents = null;
1358     private ObjectList[] fComponentsExt = null;
1359 
1360     // store the documents and their locations contributing to this namespace
1361     // REVISIT: use StringList and XSObjectList for there fields.
1362     // fDocuments is never used
1363     private List<Object> fDocuments = null;
1364     private List<String> fLocations = null;
1365 
1366     public synchronized void addDocument(Object document, String location) {
1367         if (fDocuments == null) {
1368             // Parsing schema is not thread safe, synchronized may be removed
1369             fDocuments = new CopyOnWriteArrayList<>();
1370             fLocations = new CopyOnWriteArrayList<>();
1371         }
1372         fDocuments.add(document);
1373         fLocations.add(location);
1374     }
1375 
1376     public synchronized void removeDocument(int index) {
1377         if (fDocuments != null &&
1378             index >= 0 &&
1379             index < fDocuments.size()) {
1380             fDocuments.remove(index);
1381             fLocations.remove(index);
1382         }
1383     }
1384 
1385     /**
1386      * [schema namespace]
1387      * @see <a href="http://www.w3.org/TR/xmlschema-1/#nsi-schema_namespace">[schema namespace]</a>
1388      * @return The target namespace of this item.
1389      */
1390     public String getSchemaNamespace() {
1391         return fTargetNamespace;
1392     }
1393 
1394     // annotation support
1395     synchronized DOMParser getDOMParser() {
1396         if (fDOMParser != null) {
1397             DOMParser parser = fDOMParser.get();
1398             if (parser != null) {
1399                 return parser;
1400             }
1401         }
1402         // REVISIT:  when schema handles XML 1.1, will need to
1403         // revisit this (and the practice of not prepending an XML decl to the annotation string
1404         XML11Configuration config = new XML11Configuration(fSymbolTable);
1405         // note that this should never produce errors or require
1406         // entity resolution, so just a barebones configuration with
1407         // a couple of feature  set will do fine
1408         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
1409         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
1410 
1411         DOMParser parser = new DOMParser(config);
1412         try {
1413             parser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE, false);
1414         }
1415         catch (SAXException exc) {}
1416         fDOMParser = new SoftReference<DOMParser>(parser);
1417         return parser;
1418     }
1419 
1420     synchronized SAXParser getSAXParser() {
1421         if (fSAXParser != null) {
1422             SAXParser parser = fSAXParser.get();
1423             if (parser != null) {
1424                 return parser;
1425             }
1426         }
1427         // REVISIT:  when schema handles XML 1.1, will need to
1428         // revisit this (and the practice of not prepending an XML decl to the annotation string
1429         XML11Configuration config = new XML11Configuration(fSymbolTable);
1430         // note that this should never produce errors or require
1431         // entity resolution, so just a barebones configuration with
1432         // a couple of feature  set will do fine
1433         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
1434         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
1435         SAXParser parser = new SAXParser(config);
1436         fSAXParser = new SoftReference<SAXParser>(parser);
1437         return parser;
1438     }
1439 
1440     /**
1441      * [schema components]: a list of top-level components, i.e. element
1442      * declarations, attribute declarations, etc.
1443      * @param objectType The type of the declaration, i.e.
1444      *   <code>ELEMENT_DECLARATION</code>. Note that
1445      *   <code>XSTypeDefinition.SIMPLE_TYPE</code> and
1446      *   <code>XSTypeDefinition.COMPLEX_TYPE</code> can also be used as the
1447      *   <code>objectType</code> to retrieve only complex types or simple
1448      *   types, instead of all types.
1449      * @return  A list of top-level definition of the specified type in
1450      *   <code>objectType</code> or an empty <code>XSNamedMap</code> if no
1451      *   such definitions exist.
1452      */
1453     public synchronized XSNamedMap getComponents(short objectType) {
1454         if (objectType <= 0 || objectType > MAX_COMP_IDX ||
1455             !GLOBAL_COMP[objectType]) {
1456             return XSNamedMapImpl.EMPTY_MAP;
1457         }
1458 
1459         if (fComponents == null)
1460             fComponents = new XSNamedMap[MAX_COMP_IDX+1];
1461 
1462         // get the hashtable for this type of components
1463         if (fComponents[objectType] == null) {
1464             SymbolHash table = null;
1465             switch (objectType) {
1466             case XSConstants.TYPE_DEFINITION:
1467             case XSTypeDefinition.COMPLEX_TYPE:
1468             case XSTypeDefinition.SIMPLE_TYPE:
1469                 table = fGlobalTypeDecls;
1470                 break;
1471             case XSConstants.ATTRIBUTE_DECLARATION:
1472                 table = fGlobalAttrDecls;
1473                 break;
1474             case XSConstants.ELEMENT_DECLARATION:
1475                 table = fGlobalElemDecls;
1476                 break;
1477             case XSConstants.ATTRIBUTE_GROUP:
1478                 table = fGlobalAttrGrpDecls;
1479                 break;
1480             case XSConstants.MODEL_GROUP_DEFINITION:
1481                 table = fGlobalGroupDecls;
1482                 break;
1483             case XSConstants.NOTATION_DECLARATION:
1484                 table = fGlobalNotationDecls;
1485                 break;
1486             case XSConstants.IDENTITY_CONSTRAINT:
1487                 table = this.fGlobalIDConstraintDecls;
1488                 break;
1489             }
1490 
1491             // for complex/simple types, create a special implementation,
1492             // which take specific types out of the hash table
1493             if (objectType == XSTypeDefinition.COMPLEX_TYPE ||
1494                 objectType == XSTypeDefinition.SIMPLE_TYPE) {
1495                 fComponents[objectType] = new XSNamedMap4Types(fTargetNamespace, table, objectType);
1496             }
1497             else {
1498                 fComponents[objectType] = new XSNamedMapImpl(fTargetNamespace, table);
1499             }
1500         }
1501 
1502         return fComponents[objectType];
1503     }
1504 
1505     public synchronized ObjectList getComponentsExt(short objectType) {
1506         if (objectType <= 0 || objectType > MAX_COMP_IDX ||
1507             !GLOBAL_COMP[objectType]) {
1508             return ObjectListImpl.EMPTY_LIST;
1509         }
1510 
1511         if (fComponentsExt == null)
1512             fComponentsExt = new ObjectList[MAX_COMP_IDX+1];
1513 
1514         // get the hashtable for this type of components
1515         if (fComponentsExt[objectType] == null) {
1516             SymbolHash table = null;
1517             switch (objectType) {
1518             case XSConstants.TYPE_DEFINITION:
1519             case XSTypeDefinition.COMPLEX_TYPE:
1520             case XSTypeDefinition.SIMPLE_TYPE:
1521                 table = fGlobalTypeDeclsExt;
1522                 break;
1523             case XSConstants.ATTRIBUTE_DECLARATION:
1524                 table = fGlobalAttrDeclsExt;
1525                 break;
1526             case XSConstants.ELEMENT_DECLARATION:
1527                 table = fGlobalElemDeclsExt;
1528                 break;
1529             case XSConstants.ATTRIBUTE_GROUP:
1530                 table = fGlobalAttrGrpDeclsExt;
1531                 break;
1532             case XSConstants.MODEL_GROUP_DEFINITION:
1533                 table = fGlobalGroupDeclsExt;
1534                 break;
1535             case XSConstants.NOTATION_DECLARATION:
1536                 table = fGlobalNotationDeclsExt;
1537                 break;
1538             case XSConstants.IDENTITY_CONSTRAINT:
1539                 table = this.fGlobalIDConstraintDeclsExt;
1540                 break;
1541             }
1542 
1543             Object[] entries = table.getEntries();
1544             fComponentsExt[objectType] = new ObjectListImpl(entries, entries.length);
1545         }
1546 
1547         return fComponentsExt[objectType];
1548     }
1549 
1550     public synchronized void resetComponents() {
1551         fComponents = null;
1552         fComponentsExt = null;
1553     }
1554 
1555     /**
1556      * Convenience method. Returns a top-level simple or complex type
1557      * definition.
1558      * @param name The name of the definition.
1559      * @return An <code>XSTypeDefinition</code> or null if such definition
1560      *   does not exist.
1561      */
1562     public XSTypeDefinition getTypeDefinition(String name) {
1563         return getGlobalTypeDecl(name);
1564     }
1565 
1566     /**
1567      * Convenience method. Returns a top-level attribute declaration.
1568      * @param name The name of the declaration.
1569      * @return A top-level attribute declaration or null if such declaration
1570      *   does not exist.
1571      */
1572     public XSAttributeDeclaration getAttributeDeclaration(String name) {
1573         return getGlobalAttributeDecl(name);
1574     }
1575 
1576     /**
1577      * Convenience method. Returns a top-level element declaration.
1578      * @param name The name of the declaration.
1579      * @return A top-level element declaration or null if such declaration
1580      *   does not exist.
1581      */
1582     public XSElementDeclaration getElementDeclaration(String name) {
1583         return getGlobalElementDecl(name);
1584     }
1585 
1586     /**
1587      * Convenience method. Returns a top-level attribute group definition.
1588      * @param name The name of the definition.
1589      * @return A top-level attribute group definition or null if such
1590      *   definition does not exist.
1591      */
1592     public XSAttributeGroupDefinition getAttributeGroup(String name) {
1593         return getGlobalAttributeGroupDecl(name);
1594     }
1595 
1596     /**
1597      * Convenience method. Returns a top-level model group definition.
1598      *
1599      * @param name      The name of the definition.
1600      * @return A top-level model group definition definition or null if such
1601      *         definition does not exist.
1602      */
1603     public XSModelGroupDefinition getModelGroupDefinition(String name) {
1604         return getGlobalGroupDecl(name);
1605     }
1606 
1607     /**
1608      * Convenience method. Returns a top-level notation declaration.
1609      *
1610      * @param name      The name of the declaration.
1611      * @return A top-level notation declaration or null if such declaration
1612      *         does not exist.
1613      */
1614     public XSNotationDeclaration getNotationDeclaration(String name) {
1615         return getGlobalNotationDecl(name);
1616     }
1617 
1618     public XSIDCDefinition getIDCDefinition(String name) {
1619         return getIDConstraintDecl(name);
1620     }
1621 
1622 
1623     /**
1624      * [document location]
1625      * @see <a href="http://www.w3.org/TR/xmlschema-1/#sd-document_location">[document location]</a>
1626      * @return a list of document information item
1627      */
1628     public StringList getDocumentLocations() {
1629         return new StringListImpl(fLocations);
1630     }
1631 
1632     /**
1633      * Return an <code>XSModel</code> that represents components in this schema
1634      * grammar.
1635      *
1636      * @return  an <code>XSModel</code> representing this schema grammar
1637      */
1638     public XSModel toXSModel() {
1639         return new XSModelImpl(new SchemaGrammar[]{this});
1640     }
1641 
1642     public XSModel toXSModel(XSGrammar[] grammars) {
1643         if (grammars == null || grammars.length == 0)
1644             return toXSModel();
1645 
1646         int len = grammars.length;
1647         boolean hasSelf = false;
1648         for (int i = 0; i < len; i++) {
1649             if (grammars[i] == this) {
1650                 hasSelf = true;
1651                 break;
1652             }
1653         }
1654 
1655         SchemaGrammar[] gs = new SchemaGrammar[hasSelf ? len : len+1];
1656         for (int i = 0; i < len; i++)
1657             gs[i] = (SchemaGrammar)grammars[i];
1658         if (!hasSelf)
1659             gs[len] = this;
1660         return new XSModelImpl(gs);
1661     }
1662 
1663     /**
1664      * @see org.apache.xerces.xs.XSNamespaceItem#getAnnotations()
1665      */
1666     public XSObjectList getAnnotations() {
1667         if (fNumAnnotations == 0) {
1668             return XSObjectListImpl.EMPTY_LIST;
1669         }
1670         return new XSObjectListImpl(fAnnotations, fNumAnnotations);
1671     }
1672 
1673     public void addAnnotation(XSAnnotationImpl annotation) {
1674         if (annotation == null) {
1675             return;
1676         }
1677         if (fAnnotations == null) {
1678             fAnnotations = new XSAnnotationImpl[2];
1679         }
1680         else if (fNumAnnotations == fAnnotations.length) {
1681             XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
1682             System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
1683             fAnnotations = newArray;
1684         }
1685         fAnnotations[fNumAnnotations++] = annotation;
1686     }
1687 
1688     public void setImmutable(boolean isImmutable) {
1689         fIsImmutable = isImmutable;
1690     }
1691 
1692     public boolean isImmutable() {
1693         return fIsImmutable;
1694     }
1695 
1696 } // class SchemaGrammar