From 5e30bde04f4c3da1c6d6103ec2dbd0a036de1765 Mon Sep 17 00:00:00 2001
From: Khaled Noaman <knoaman@apache.org>
Date: Wed, 24 Dec 2003 17:42:03 +0000
Subject: [PATCH] Misc. PSVI updates

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@175620 13f79535-47bb-0310-9956-ffa450edef68
---
 .../framework/psvi/XSAttributeDeclaration.hpp |  14 +
 .../psvi/XSComplexTypeDefinition.cpp          |   7 +-
 .../psvi/XSComplexTypeDefinition.hpp          |  14 +
 .../framework/psvi/XSElementDeclaration.hpp   |  11 +
 src/xercesc/framework/psvi/XSModel.cpp        | 537 +++++++++++-------
 src/xercesc/framework/psvi/XSModel.hpp        |  31 +-
 .../framework/psvi/XSNamespaceItem.cpp        |  67 ++-
 .../framework/psvi/XSNamespaceItem.hpp        |  25 +-
 .../framework/psvi/XSSimpleTypeDefinition.hpp |  10 +
 src/xercesc/internal/XSObjectFactory.cpp      | 147 +++--
 src/xercesc/internal/XSObjectFactory.hpp      |   5 +
 .../validators/schema/ComplexTypeInfo.cpp     | 112 +++-
 .../validators/schema/ComplexTypeInfo.hpp     |  14 +
 src/xercesc/validators/schema/PSVIDefs.hpp    |   7 +-
 .../validators/schema/SchemaAttDef.cpp        |  15 +-
 .../validators/schema/SchemaAttDef.hpp        |  28 +-
 .../validators/schema/SchemaElementDecl.cpp   |   6 +
 .../validators/schema/SchemaElementDecl.hpp   |  18 +-
 .../validators/schema/TraverseSchema.cpp      |  38 +-
 19 files changed, 800 insertions(+), 306 deletions(-)

diff --git a/src/xercesc/framework/psvi/XSAttributeDeclaration.hpp b/src/xercesc/framework/psvi/XSAttributeDeclaration.hpp
index 59ec563a9..670dc9ae5 100644
--- a/src/xercesc/framework/psvi/XSAttributeDeclaration.hpp
+++ b/src/xercesc/framework/psvi/XSAttributeDeclaration.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.8  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.7  2003/12/01 23:23:26  neilg
  * fix for bug 25118; thanks to Jeroen Witmond
  *
@@ -229,6 +232,9 @@ public:
 
 private:
 
+    void setEnclosingCTDefinition(XSComplexTypeDefinition* const toSet);
+    friend class XSObjectFactory;
+
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
     // -----------------------------------------------------------------------
@@ -276,6 +282,14 @@ inline XSComplexTypeDefinition *XSAttributeDeclaration::getEnclosingCTDefinition
     return fEnclosingCTDefinition;
 }
 
+inline void XSAttributeDeclaration::setEnclosingCTDefinition
+(
+    XSComplexTypeDefinition* const toSet
+)
+{
+    fEnclosingCTDefinition = toSet;
+}
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/framework/psvi/XSComplexTypeDefinition.cpp b/src/xercesc/framework/psvi/XSComplexTypeDefinition.cpp
index 570e238e9..75fe019c0 100644
--- a/src/xercesc/framework/psvi/XSComplexTypeDefinition.cpp
+++ b/src/xercesc/framework/psvi/XSComplexTypeDefinition.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.11  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.10  2003/12/19 15:09:47  knoaman
  * PSVI: process 'final' information
  *
@@ -182,12 +185,10 @@ XSComplexTypeDefinition::~XSComplexTypeDefinition()
 XSConstants::DERIVATION_TYPE XSComplexTypeDefinition::getDerivationMethod() const
 {
     switch(fComplexTypeInfo->getDerivedBy()) {
-        case SchemaSymbols::XSD_RESTRICTION:
-            return XSConstants::DERIVATION_RESTRICTION;
         case SchemaSymbols::XSD_EXTENSION:
             return XSConstants::DERIVATION_EXTENSION;
         default:
-            return XSConstants::DERIVATION_NONE;
+            return XSConstants::DERIVATION_RESTRICTION;
     }
 }
 
diff --git a/src/xercesc/framework/psvi/XSComplexTypeDefinition.hpp b/src/xercesc/framework/psvi/XSComplexTypeDefinition.hpp
index 23448d1e5..ff0ee66a9 100644
--- a/src/xercesc/framework/psvi/XSComplexTypeDefinition.hpp
+++ b/src/xercesc/framework/psvi/XSComplexTypeDefinition.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.8  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.7  2003/12/01 23:23:26  neilg
  * fix for bug 25118; thanks to Jeroen Witmond
  *
@@ -294,6 +297,12 @@ public:
 
 private:
 
+    /**
+     * Set the base type
+     */
+    void setBaseType(XSTypeDefinition* const xsBaseType);
+    friend class XSObjectFactory;
+
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
     // -----------------------------------------------------------------------
@@ -340,6 +349,11 @@ inline XSParticle *XSComplexTypeDefinition::getParticle() const
     return fParticle;
 }
 
+inline void
+XSComplexTypeDefinition::setBaseType(XSTypeDefinition* const xsBaseType)
+{
+    fBaseType = xsBaseType;
+}
 
 XERCES_CPP_NAMESPACE_END
 
diff --git a/src/xercesc/framework/psvi/XSElementDeclaration.hpp b/src/xercesc/framework/psvi/XSElementDeclaration.hpp
index b85a18015..51c621730 100644
--- a/src/xercesc/framework/psvi/XSElementDeclaration.hpp
+++ b/src/xercesc/framework/psvi/XSElementDeclaration.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.9  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.8  2003/12/01 23:23:26  neilg
  * fix for bug 25118; thanks to Jeroen Witmond
  *
@@ -304,6 +307,9 @@ public:
     //@}
 private:
 
+    void setEnclosingCTDefinition(XSComplexTypeDefinition* const toSet);
+    friend class XSObjectFactory;
+
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
     // -----------------------------------------------------------------------
@@ -377,6 +383,11 @@ inline void XSElementDeclaration::setTypeDefinition(XSTypeDefinition* typeDefini
     fTypeDefinition = typeDefinition;
 }
 
+inline void XSElementDeclaration::setEnclosingCTDefinition(XSComplexTypeDefinition* const toSet)
+{
+    fEnclosingTypeDefinition = toSet;
+}
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/framework/psvi/XSModel.cpp b/src/xercesc/framework/psvi/XSModel.cpp
index 4bcac0156..d73729878 100644
--- a/src/xercesc/framework/psvi/XSModel.cpp
+++ b/src/xercesc/framework/psvi/XSModel.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.15  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.14  2003/12/17 00:18:34  cargilld
  * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
  *
@@ -123,147 +126,9 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
-// local constant
-static const XMLCh emptyString[]  = {0};
-
-// the local routine to add a grammar to the XSModel
-void XSModel::addGrammarToXSModel(XSNamespaceItem* namespaceItem)
-{
-    // First loop through top-level BUILTIN simple type definitions in the grammar...
-    // all grammar's would be the same...    
-    RefHashTableOfEnumerator<DatatypeValidator> simpleEnum = RefHashTableOfEnumerator<DatatypeValidator> (namespaceItem->getSchemaGrammar()->getDatatypeRegistry()->getBuiltInRegistry(), false, fMemoryManager);
-    while (simpleEnum.hasMoreElements())
-    {
-        DatatypeValidator& curSimple = simpleEnum.nextElement();
-        XSSimpleTypeDefinition* xsSimple;
-        xsSimple = fObjFactory->addOrFind(&curSimple, this);
-        if (!fAddedBuiltinDatatypeValidators)
-        {       
-            fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsSimple, xsSimple->getName(), namespaceItem->getSchemaNamespace());          
-        }
-        namespaceItem->fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsSimple, xsSimple->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fHashMap[XSConstants::TYPE_DEFINITION -1]->put((void *) xsSimple->getName(), xsSimple);
-    } 
-    fAddedBuiltinDatatypeValidators = true;
-    // end of simple BuiltIn loop
-
-    // Loop through top-level attribute declarations in the grammar...
-    RefHashTableOfEnumerator<XMLAttDef> attrEnum = RefHashTableOfEnumerator<XMLAttDef> (namespaceItem->getSchemaGrammar()->getAttributeDeclRegistry(), false, fMemoryManager);
-        
-    while (attrEnum.hasMoreElements())
-    {
-        SchemaAttDef* attDef = (SchemaAttDef*) &(attrEnum.nextElement());
-        XSAttributeDeclaration* xsAttrDecl = fObjFactory->addOrFind(attDef, this);
-        fComponentMap[XSConstants::ATTRIBUTE_DECLARATION-1]->addElement(xsAttrDecl, xsAttrDecl->getName(), namespaceItem->getSchemaNamespace());
-        xsAttrDecl->setId(fAttributeDeclarationVector->size());
-        fAttributeDeclarationVector->addElement(xsAttrDecl);
-        namespaceItem->fComponentMap[XSConstants::ATTRIBUTE_DECLARATION-1]->addElement(xsAttrDecl, xsAttrDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fHashMap[XSConstants::ATTRIBUTE_DECLARATION -1]->put((void *) xsAttrDecl->getName(), xsAttrDecl);
-    } // end of attribute loop
-
-    // Loop through top-level elements in the grammar...
-    RefHash3KeysIdPoolEnumerator<SchemaElementDecl> elemEnum = namespaceItem->getSchemaGrammar()->getElemEnumerator();
-    
-    while (elemEnum.hasMoreElements())
-    {
-        SchemaElementDecl& curElem = elemEnum.nextElement();
-        if (curElem.getEnclosingScope() == Grammar::TOP_LEVEL_SCOPE) 
-        {
-            XSElementDeclaration* xsElemDecl = fObjFactory->addOrFind(&curElem, this);
-
-            fComponentMap[XSConstants::ELEMENT_DECLARATION-1]->addElement(xsElemDecl, xsElemDecl->getName(), namespaceItem->getSchemaNamespace());
-            xsElemDecl->setId(fElementDeclarationVector->size());
-            fElementDeclarationVector->addElement(xsElemDecl);
-            namespaceItem->fComponentMap[XSConstants::ELEMENT_DECLARATION -1]->addElement(xsElemDecl, xsElemDecl->getName(), namespaceItem->getSchemaNamespace());
-            namespaceItem->fHashMap[XSConstants::ELEMENT_DECLARATION -1]->put((void *) xsElemDecl->getName(), xsElemDecl);
-        }
-    } // end of element loop
-
-    // Now loop through top-level User Defined simple type definitions in the grammar...
-    DVHashTable* dvHT = namespaceItem->getSchemaGrammar()->getDatatypeRegistry()->getUserDefinedRegistry();
-    if (dvHT)
-    {
-        RefHashTableOfEnumerator<DatatypeValidator> simpleUserEnum = RefHashTableOfEnumerator<DatatypeValidator> (dvHT, false, fMemoryManager);
-        while (simpleUserEnum.hasMoreElements())
-        {
-            DatatypeValidator& curSimple = simpleUserEnum.nextElement();
-            if (!curSimple.getAnonymous())
-            {
-                XSSimpleTypeDefinition* xsSimple = fObjFactory->addOrFind(&curSimple, this);
-                fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsSimple, xsSimple->getName(), namespaceItem->getSchemaNamespace());
-                namespaceItem->fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsSimple, xsSimple->getName(), namespaceItem->getSchemaNamespace());
-                namespaceItem->fHashMap[XSConstants::TYPE_DEFINITION -1]->put((void *) xsSimple->getName(), xsSimple);                
-            }            
-        }
-        // end of simple User loop
-    }
-
-    // Loop through top-level COMPLEX type definitions in the grammar...        
-    RefHashTableOfEnumerator<ComplexTypeInfo> complexEnum = RefHashTableOfEnumerator<ComplexTypeInfo> (namespaceItem->getSchemaGrammar()->getComplexTypeRegistry(), false, fMemoryManager);      
-    while (complexEnum.hasMoreElements())
-    {
-        ComplexTypeInfo&  curComplex = complexEnum.nextElement();           
-        if (!curComplex.getAnonymous())
-        {
-            XSComplexTypeDefinition* xsComplex = fObjFactory->addOrFind(&curComplex, this);
-            fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsComplex, xsComplex->getName(), namespaceItem->getSchemaNamespace());
-            namespaceItem->fComponentMap[XSConstants::TYPE_DEFINITION -1]->addElement(xsComplex, xsComplex->getName(), namespaceItem->getSchemaNamespace());
-            namespaceItem->fHashMap[XSConstants::TYPE_DEFINITION -1]->put((void *) xsComplex->getName(), xsComplex);                
-        }            
-    }  // end of type definition loop
-
-    // Loop through top-level attribute group definitions in the grammar...
-    RefHashTableOfEnumerator<XercesAttGroupInfo> attrGroupEnum = RefHashTableOfEnumerator<XercesAttGroupInfo> (namespaceItem->getSchemaGrammar()->getAttGroupInfoRegistry(), false, fMemoryManager);
-    while (attrGroupEnum.hasMoreElements())
-    {
-        XercesAttGroupInfo& curAttrGroup = attrGroupEnum.nextElement();
-        XSAttributeGroupDefinition* xsAttrGroupDecl = fObjFactory->createXSAttGroupDefinition(&curAttrGroup, this);
-        fComponentMap[XSConstants::ATTRIBUTE_GROUP_DEFINITION -1]->addElement(xsAttrGroupDecl, xsAttrGroupDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fComponentMap[XSConstants::ATTRIBUTE_GROUP_DEFINITION -1]->addElement(xsAttrGroupDecl, xsAttrGroupDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fHashMap[XSConstants::ATTRIBUTE_GROUP_DEFINITION -1]->put((void *) xsAttrGroupDecl->getName(), xsAttrGroupDecl);                
-    } // end of attribute group loop
-        
-    // Loop through top-level model group definitions in the grammar...        
-    RefHashTableOfEnumerator<XercesGroupInfo> modelGroupEnum = RefHashTableOfEnumerator<XercesGroupInfo> (namespaceItem->getSchemaGrammar()->getGroupInfoRegistry(), false, fMemoryManager);
-    while (modelGroupEnum.hasMoreElements())
-    {
-        XercesGroupInfo& curModelGroup = modelGroupEnum.nextElement();
-        XSModelGroupDefinition* xsModelGroupDecl = fObjFactory->createXSModelGroupDefinition(&curModelGroup, this);
-        fComponentMap[XSConstants::MODEL_GROUP_DEFINITION -1]->addElement(xsModelGroupDecl, xsModelGroupDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fComponentMap[XSConstants::MODEL_GROUP_DEFINITION -1]->addElement(xsModelGroupDecl, xsModelGroupDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fHashMap[XSConstants::MODEL_GROUP_DEFINITION -1]->put((void *) xsModelGroupDecl->getName(), xsModelGroupDecl);                
-    } // end of model group loop
-
-    // Loop through notations in the grammar...    
-    NameIdPoolEnumerator<XMLNotationDecl> notationEnum = namespaceItem->getSchemaGrammar()->getNotationEnumerator();
-    while (notationEnum.hasMoreElements())
-    {
-        XMLNotationDecl& notationDecl = notationEnum.nextElement();            
-        XSNotationDeclaration* xsNotationDecl = fObjFactory->addOrFind(&notationDecl, this);
-
-        fComponentMap[XSConstants::NOTATION_DECLARATION -1]->addElement(xsNotationDecl, xsNotationDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fComponentMap[XSConstants::NOTATION_DECLARATION -1]->addElement(xsNotationDecl, xsNotationDecl->getName(), namespaceItem->getSchemaNamespace());
-        namespaceItem->fHashMap[XSConstants::NOTATION_DECLARATION -1]->put((void *) xsNotationDecl->getName(), xsNotationDecl);                
-    } // end of notation loop
-
-    // Loop through annotations in the grammar...
-    // As annotations are already created as XSAnnotations no need to create them
-    // or store them in the XercesToXSMap.
-    XSAnnotation* annot = namespaceItem->getSchemaGrammar()->getAnnotation();
-    while (annot)
-    {     
-        fXSAnnotationList->addElement(annot);
-        namespaceItem->fXSAnnotationList->addElement(annot);
-        annot = annot->getNext();        
-    } // end of annotation loop
-}
-
-/**
-  * The constructor to be used when a grammar pool contains all needed info
-  *
-  * @param grammarPool  the grammar pool containing the underlying data structures
-  * @param  manager     The configurable memory manager
-  */
+// ---------------------------------------------------------------------------
+//  XSModel: Constructors and Destructor
+// ---------------------------------------------------------------------------
 XSModel::XSModel( XMLGrammarPool *grammarPool
                 , MemoryManager* const manager)
     : fMemoryManager(manager)
@@ -278,28 +143,28 @@ XSModel::XSModel( XMLGrammarPool *grammarPool
     , fDeleteNamespace(0)
     , fParent(0)
     , fDeleteParent(false)
-    , fAddedBuiltinDatatypeValidators(false)
+    , fAddedS4SGrammar(false)
 {
     fURIStringPool = grammarPool->getURIStringPool();
     fObjFactory = new (fMemoryManager) XSObjectFactory(manager);
 
     // Populate XSNamedMaps by going through the components
     for (unsigned int i=0; i<XSConstants::MULTIVALUE_FACET; i++)
-    {        
-        switch (i+1) 
+    {
+        switch (i+1)
         {
             case XSConstants::ATTRIBUTE_DECLARATION:
             case XSConstants::ELEMENT_DECLARATION:
             case XSConstants::TYPE_DEFINITION:
             case XSConstants::ATTRIBUTE_GROUP_DEFINITION:
             case XSConstants::MODEL_GROUP_DEFINITION:
-            case XSConstants::NOTATION_DECLARATION:          
-                fComponentMap[i] = new (fMemoryManager) XSNamedMap<XSObject> 
+            case XSConstants::NOTATION_DECLARATION:
+                fComponentMap[i] = new (fMemoryManager) XSNamedMap<XSObject>
                 (
                     20,     // size
                     29,     // modulus
                     fURIStringPool,
-                    false,  // adoptElems 
+                    false,  // adoptElems
                     fMemoryManager
                 );
                 break;
@@ -316,7 +181,7 @@ XSModel::XSModel( XMLGrammarPool *grammarPool
                 break;
         }
     }
-       
+
     fNamespaceStringList        = new (manager) RefArrayVectorOf <XMLCh>(10, true, manager);
     fXSNamespaceItemList        = new (manager) RefVectorOf <XSNamespaceItem>(10, true, manager);
     fXSAnnotationList           = new (manager) RefVectorOf <XSAnnotation> (10, false, manager);
@@ -327,42 +192,50 @@ XSModel::XSModel( XMLGrammarPool *grammarPool
     // Loop through all grammars in the grammar pool to create the XSNamespaceItem's
     //  which will have access to Annotation Information which can be used later when
     //  we create all the XS components.
+    XSNamespaceItem* namespaceItem = 0;
     RefHashTableOfEnumerator<Grammar> grammarEnum = grammarPool->getGrammarEnumerator();
-
     while (grammarEnum.hasMoreElements())
     {
         SchemaGrammar& sGrammar = (SchemaGrammar&) grammarEnum.nextElement();
-        if (sGrammar.getGrammarType() != Grammar::SchemaGrammarType)
+        if (sGrammar.getGrammarType() != Grammar::SchemaGrammarType ||
+            XMLString::equals(sGrammar.getTargetNamespace(), SchemaSymbols::fgURI_SCHEMAFORSCHEMA))
             continue;
-        
+
         // NOTE: In the grammarpool, preprocessed grammars without targetnamespace
         //       will use an empty string...
         XMLCh* NameSpace = XMLString::replicate(sGrammar.getTargetNamespace(), manager);
         fNamespaceStringList->addElement(NameSpace);
-        
-        XSNamespaceItem* namespaceItem = new (manager) XSNamespaceItem(this, &sGrammar, manager);
+        namespaceItem = new (manager) XSNamespaceItem(this, &sGrammar, manager);
         fXSNamespaceItemList->addElement(namespaceItem);
-
         fHashNamespace->put(NameSpace, namespaceItem);
     }
-    
+
     // Now loop through all of the NamespaceItem's
+    // First, we add S4S namespace
     unsigned int numberOfNamespaces = fXSNamespaceItemList->size();
-    for (unsigned int j=0; j<numberOfNamespaces; j++)
-    {       
+    if (numberOfNamespaces)
+    {
+        namespaceItem = new (manager) XSNamespaceItem
+        (
+            this, SchemaSymbols::fgURI_SCHEMAFORSCHEMA, manager
+        );
+
+        fNamespaceStringList->addElement
+        (
+            XMLString::replicate(SchemaSymbols::fgURI_SCHEMAFORSCHEMA,manager)
+        );
+        fXSNamespaceItemList->addElement(namespaceItem);
+        fHashNamespace->put
+        (
+            (void*) SchemaSymbols::fgURI_SCHEMAFORSCHEMA
+            , namespaceItem
+        );
+    }
+
+    for (unsigned int j = 0; j < numberOfNamespaces; j++)
         addGrammarToXSModel(fXSNamespaceItemList->elementAt(j));
-    } // end of namespaceItem loop
 }
 
-/**
-  * The constructor to be used when the XSModel must represent all
-  * components in the union of an existing XSModel and a newly-created
-  * Grammar(s) from the GrammarResolver
-  *
-  * @param baseModel  the XSModel upon which this one is based
-  * @param grammarResolver  the grammar(s) whose components are to be merged
-  * @param manager     The configurable memory manager
-  */
 XSModel::XSModel( XSModel *baseModel
                 , GrammarResolver *grammarResolver
                 , MemoryManager* const manager)
@@ -378,29 +251,29 @@ XSModel::XSModel( XSModel *baseModel
     , fObjFactory(0)
     , fDeleteNamespace(0)
     , fDeleteParent(true)
-    , fAddedBuiltinDatatypeValidators(false)
+    , fAddedS4SGrammar(false)
 {
-    fURIStringPool = grammarResolver->getStringPool();        
+    fURIStringPool = grammarResolver->getStringPool();
     fObjFactory = new (manager) XSObjectFactory(manager);
 
     unsigned int i;
     // Populate XSNamedMaps by going through the components
     for (i=0; i<XSConstants::MULTIVALUE_FACET; i++)
-    {        
-        switch (i+1) 
+    {
+        switch (i+1)
         {
             case XSConstants::ATTRIBUTE_DECLARATION:
             case XSConstants::ELEMENT_DECLARATION:
             case XSConstants::TYPE_DEFINITION:
             case XSConstants::ATTRIBUTE_GROUP_DEFINITION:
             case XSConstants::MODEL_GROUP_DEFINITION:
-            case XSConstants::NOTATION_DECLARATION:          
-                fComponentMap[i] = new (fMemoryManager) XSNamedMap<XSObject> 
+            case XSConstants::NOTATION_DECLARATION:
+                fComponentMap[i] = new (fMemoryManager) XSNamedMap<XSObject>
                 (
                     20,     // size
                     29,     // modulus
                     fURIStringPool,
-                    false,  // adoptElems 
+                    false,  // adoptElems
                     fMemoryManager
                 );
                 break;
@@ -417,7 +290,7 @@ XSModel::XSModel( XSModel *baseModel
                 break;
         }
     }
-       
+
     fNamespaceStringList        = new (manager) RefArrayVectorOf <XMLCh>(10, true, manager);
     fXSNamespaceItemList        = new (manager) RefVectorOf <XSNamespaceItem>(10, false, manager);
     fDeleteNamespace            = new (manager) RefVectorOf <XSNamespaceItem>(10, true, manager);
@@ -428,17 +301,21 @@ XSModel::XSModel( XSModel *baseModel
 
     if (fParent)
     {
-        if (fParent->fAddedBuiltinDatatypeValidators)
-            fAddedBuiltinDatatypeValidators = true;
+        if (fParent->fAddedS4SGrammar)
+            fAddedS4SGrammar = true;
 
         // Need to copy information from parent so it can be returned in this object...
         for (i=0; i<fParent->fXSNamespaceItemList->size(); i++)
         {
             XSNamespaceItem* namespaceItem = fParent->fXSNamespaceItemList->elementAt(i);
             fXSNamespaceItemList->addElement(namespaceItem);
-
-            XMLCh* NameSpace = XMLString::replicate(namespaceItem->getSchemaNamespace(), manager);
-            fNamespaceStringList->addElement(NameSpace);
+            fNamespaceStringList->addElement
+            (
+                XMLString::replicate
+                (
+                    namespaceItem->getSchemaNamespace(), manager
+                )
+            );
         }
 
         for (i=0; i<XSConstants::MULTIVALUE_FACET; i++)
@@ -481,32 +358,56 @@ XSModel::XSModel( XSModel *baseModel
     // XSNamespaceItem's so we can have access to the XSAnnotations...
     ValueVectorOf<SchemaGrammar*>* grammarsToAdd = grammarResolver->getGrammarsToAddToXSModel();
     unsigned int numberOfNamespaces = fXSNamespaceItemList->size();
-    unsigned int numberOfNamespacesToAdd = grammarsToAdd->size();
-    for (i=0; i < numberOfNamespacesToAdd; i++)
+    unsigned int numberOfNamespacesToAdd = 0;
+    for (i=0; i < grammarsToAdd->size(); i++)
     {
-        XMLCh* NameSpace = XMLString::replicate(grammarsToAdd->elementAt(i)->getTargetNamespace(), manager);
+        SchemaGrammar* lGrammar = grammarsToAdd->elementAt(i);
+        if (lGrammar->getGrammarType() != Grammar::SchemaGrammarType ||
+            XMLString::equals(lGrammar->getTargetNamespace(), SchemaSymbols::fgURI_SCHEMAFORSCHEMA))
+            continue;
+
+        XMLCh* NameSpace = XMLString::replicate(lGrammar->getTargetNamespace(), manager);
         fNamespaceStringList->addElement(NameSpace);
 
-        XSNamespaceItem* namespaceItem = new (manager) XSNamespaceItem(this, grammarsToAdd->elementAt(i), manager);
+        XSNamespaceItem* namespaceItem = new (manager) XSNamespaceItem(this, lGrammar, manager);
         fXSNamespaceItemList->addElement(namespaceItem);
-        
         fHashNamespace->put(NameSpace, namespaceItem);
+        fDeleteNamespace->addElement(namespaceItem);
+        ++numberOfNamespacesToAdd;
+    }
 
-        fDeleteNamespace->addElement(namespaceItem);       
+    // Add S4S namespace if needed
+    if (numberOfNamespacesToAdd && !fAddedS4SGrammar)
+    {
+        XSNamespaceItem* namespaceItem = new (manager) XSNamespaceItem
+        (
+            this, SchemaSymbols::fgURI_SCHEMAFORSCHEMA, manager
+        );
+
+        fNamespaceStringList->addElement
+        (
+            XMLString::replicate(SchemaSymbols::fgURI_SCHEMAFORSCHEMA,manager)
+        );
+        fXSNamespaceItemList->addElement(namespaceItem);
+        fHashNamespace->put
+        (
+            (void*) SchemaSymbols::fgURI_SCHEMAFORSCHEMA , namespaceItem
+        );
+        fDeleteNamespace->addElement(namespaceItem);
     }
-    
-    // Now loop through all of the newly created NamespaceItem's    
+
+    // Now loop through all of the newly created NamespaceItem's
     for (i=numberOfNamespaces; i<(numberOfNamespaces+numberOfNamespacesToAdd); i++)
-    {        
+    {
         addGrammarToXSModel(fXSNamespaceItemList->elementAt(i));
     } // end of namespaceItem loop
 }
 
-XSModel::~XSModel() 
+XSModel::~XSModel()
 {
     for (unsigned int i=0; i<XSConstants::MULTIVALUE_FACET; i++)
-    {        
-        switch (i+1) 
+    {
+        switch (i+1)
         {
             case XSConstants::ATTRIBUTE_DECLARATION:
             case XSConstants::ELEMENT_DECLARATION:
@@ -515,11 +416,11 @@ XSModel::~XSModel()
             case XSConstants::MODEL_GROUP_DEFINITION:
             case XSConstants::NOTATION_DECLARATION:
                 delete fComponentMap[i];
-                break;           
+                break;
         }
     }
-   
-    delete fNamespaceStringList; 
+
+    delete fNamespaceStringList;
     delete fXSNamespaceItemList;
     delete fXSAnnotationList;
     delete fHashNamespace;
@@ -534,10 +435,228 @@ XSModel::~XSModel()
         delete fParent;
 }
 
+// ---------------------------------------------------------------------------
+//  XSModel: Helper methods
+// ---------------------------------------------------------------------------
+void XSModel::addComponentToNamespace(XSNamespaceItem* const namespaceItem,
+                                      XSObject* const component,
+                                      int componentIndex,
+                                      bool addToXSModel)
+{
+    namespaceItem->fComponentMap[componentIndex]->addElement
+    (
+        component, component->getName(), namespaceItem->getSchemaNamespace()
+    );
+    namespaceItem->fHashMap[componentIndex]->put
+    (
+        (void *) component->getName(), component
+    );
+
+    if (addToXSModel)
+    {
+        fComponentMap[componentIndex]->addElement
+        (
+            component, component->getName(), namespaceItem->getSchemaNamespace()
+        );
+    }
+}
+
+void
+XSModel::addS4SToXSModel(XSNamespaceItem* const namespaceItem,
+                         RefHashTableOf<DatatypeValidator>* const builtInDV)
+{
+    addComponentToNamespace
+    (
+        namespaceItem
+        , fObjFactory->addOrFind
+          (
+              ComplexTypeInfo::getAnyType
+              (
+                  fURIStringPool->getId(XMLUni::fgZeroLenString)
+              )
+              , this
+          )
+        , XSConstants::TYPE_DEFINITION - 1
+    );
+
+    // Loop through built-in simple types
+    // First add 'anySimpleType' which is the base for the other built-ins
+    DatatypeValidator* dv = builtInDV->get(SchemaSymbols::fgDT_ANYSIMPLETYPE);
+    addComponentToNamespace
+    (
+        namespaceItem
+        , fObjFactory->addOrFind(dv, this, true)
+        , XSConstants::TYPE_DEFINITION - 1
+    );
+
+    // add remaining built-in
+    RefHashTableOfEnumerator<DatatypeValidator> simpleEnum = 
+        RefHashTableOfEnumerator<DatatypeValidator> (builtInDV, false, fMemoryManager);
+    while (simpleEnum.hasMoreElements())
+    {
+        DatatypeValidator& curSimple = simpleEnum.nextElement();
+        if (&curSimple == dv)
+            continue;
+
+        addComponentToNamespace
+        (
+            namespaceItem
+            , fObjFactory->addOrFind(&curSimple, this)
+            , XSConstants::TYPE_DEFINITION - 1
+        );
+    }
+
+    // Set flag to indicate that we have added S4S grammar info
+    fAddedS4SGrammar = true;
+}
+
+
+void XSModel::addGrammarToXSModel(XSNamespaceItem* namespaceItem)
+{
+    // populate S4S namespace if needed
+    if (!fAddedS4SGrammar)
+    {
+        addS4SToXSModel
+        (
+            getNamespaceItem(SchemaSymbols::fgURI_SCHEMAFORSCHEMA)
+            , namespaceItem->fGrammar->getDatatypeRegistry()->getBuiltInRegistry()
+        );
+    }
+
+    // Loop through top-level attribute declarations in the grammar...
+    RefHashTableOfEnumerator<XMLAttDef> attrEnum = RefHashTableOfEnumerator<XMLAttDef> (namespaceItem->fGrammar->getAttributeDeclRegistry(), false, fMemoryManager);
+    while (attrEnum.hasMoreElements())
+    {
+        XSAttributeDeclaration* xsAttrDecl = fObjFactory->addOrFind
+        (
+            (SchemaAttDef*) &(attrEnum.nextElement()), this
+        );
+
+        xsAttrDecl->setId(fAttributeDeclarationVector->size());
+        fAttributeDeclarationVector->addElement(xsAttrDecl);
+        addComponentToNamespace
+        (
+            namespaceItem, xsAttrDecl, XSConstants::ATTRIBUTE_DECLARATION - 1
+        );
+    } // end of attribute loop
+
+    // Loop through top-level elements in the grammar...
+    RefHash3KeysIdPoolEnumerator<SchemaElementDecl> elemEnum = namespaceItem->fGrammar->getElemEnumerator();    
+    while (elemEnum.hasMoreElements())
+    {
+        SchemaElementDecl& curElem = elemEnum.nextElement();
+        if (curElem.getEnclosingScope() == Grammar::TOP_LEVEL_SCOPE) 
+        {
+            XSElementDeclaration* xsElemDecl = fObjFactory->addOrFind
+            (
+                &curElem, this
+            );
+
+            xsElemDecl->setId(fElementDeclarationVector->size());
+            fElementDeclarationVector->addElement(xsElemDecl);
+            addComponentToNamespace
+            (
+                namespaceItem, xsElemDecl, XSConstants::ELEMENT_DECLARATION -1
+            );
+        }
+    } // end of element loop
+
+    // Now loop through top-level User Defined simple type definitions in the grammar...
+    DVHashTable* dvHT = namespaceItem->fGrammar->getDatatypeRegistry()->getUserDefinedRegistry();
+    if (dvHT)
+    {
+        RefHashTableOfEnumerator<DatatypeValidator> simpleUserEnum = RefHashTableOfEnumerator<DatatypeValidator> (dvHT, false, fMemoryManager);
+        while (simpleUserEnum.hasMoreElements())
+        {
+            DatatypeValidator& curSimple = simpleUserEnum.nextElement();
+            if (!curSimple.getAnonymous())
+            {
+                addComponentToNamespace
+                (
+                    namespaceItem
+                    , fObjFactory->addOrFind(&curSimple, this)
+                    , XSConstants::TYPE_DEFINITION - 1
+                );
+            }
+        } // end of simple User loop
+    }
+
+    // Loop through top-level COMPLEX type definitions in the grammar...
+    RefHashTableOfEnumerator<ComplexTypeInfo> complexEnum = RefHashTableOfEnumerator<ComplexTypeInfo> (namespaceItem->fGrammar->getComplexTypeRegistry(), false, fMemoryManager);
+    while (complexEnum.hasMoreElements())
+    {
+        ComplexTypeInfo&  curComplex = complexEnum.nextElement();
+        if (!curComplex.getAnonymous())
+        {
+            addComponentToNamespace
+            (
+                namespaceItem
+                , fObjFactory->addOrFind(&curComplex, this)
+                , XSConstants::TYPE_DEFINITION - 1
+            );
+        }
+    }  // end of type definition loop
+
+    // Loop through top-level attribute group definitions in the grammar...
+    RefHashTableOfEnumerator<XercesAttGroupInfo> attrGroupEnum = RefHashTableOfEnumerator<XercesAttGroupInfo> (namespaceItem->fGrammar->getAttGroupInfoRegistry(), false, fMemoryManager);
+    while (attrGroupEnum.hasMoreElements())
+    {
+        addComponentToNamespace
+        (
+            namespaceItem
+            , fObjFactory->createXSAttGroupDefinition
+              (
+                  &(attrGroupEnum.nextElement()), this
+              )
+            , XSConstants::ATTRIBUTE_GROUP_DEFINITION - 1
+        );
+    } // end of attribute group loop
+
+    // Loop through top-level model group definitions in the grammar...
+    RefHashTableOfEnumerator<XercesGroupInfo> modelGroupEnum = RefHashTableOfEnumerator<XercesGroupInfo> (namespaceItem->fGrammar->getGroupInfoRegistry(), false, fMemoryManager);
+    while (modelGroupEnum.hasMoreElements())
+    {
+        addComponentToNamespace
+        (
+            namespaceItem
+            , fObjFactory->createXSModelGroupDefinition
+              (
+                  &(modelGroupEnum.nextElement()), this
+              )
+            , XSConstants::MODEL_GROUP_DEFINITION - 1
+        );
+    } // end of model group loop
+
+    // Loop through notations in the grammar...    
+    NameIdPoolEnumerator<XMLNotationDecl> notationEnum = namespaceItem->fGrammar->getNotationEnumerator();
+    while (notationEnum.hasMoreElements())
+    {
+        addComponentToNamespace
+        (
+            namespaceItem
+            , fObjFactory->addOrFind(&(notationEnum.nextElement()), this)
+            , XSConstants::NOTATION_DECLARATION - 1
+        );
+    } // end of notation loop
+
+    // Loop through annotations in the grammar...
+    // As annotations are already created as XSAnnotations no need to create them
+    // or store them in the XercesToXSMap.
+    XSAnnotation* annot = namespaceItem->fGrammar->getAnnotation();
+    while (annot)
+    {
+        fXSAnnotationList->addElement(annot);
+        namespaceItem->fXSAnnotationList->addElement(annot);
+        annot = annot->getNext();
+    } // end of annotation loop
+}
+
 
-// XSModel methods
 
 
+// ---------------------------------------------------------------------------
+//  XSModel: Access methods
+// ---------------------------------------------------------------------------
 /**
  * [schema components]: a list of top-level components, i.e. element 
  * declarations, attribute declarations, etc. 
@@ -565,14 +684,14 @@ XSNamedMap <XSObject> *XSModel::getComponents(XSConstants::COMPONENT_TYPE object
  *   <code>objectType</code> and defined in the specified 
  *   <code>namespace</code> or <code>null</code>. 
  */
-XSNamedMap <XSObject> *XSModel::getComponentsByNamespace(XSConstants::COMPONENT_TYPE objectType, 
+XSNamedMap <XSObject> *XSModel::getComponentsByNamespace(XSConstants::COMPONENT_TYPE objectType,
                                                const XMLCh *compNamespace)
 {
     XSNamespaceItem* namespaceItem;
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
     
     if (namespaceItem)
         return namespaceItem->getComponents(objectType);
@@ -602,8 +721,8 @@ XSElementDeclaration *XSModel::getElementDeclaration(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
-    
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
+
     if (namespaceItem)
         return namespaceItem->getElementDeclaration(name);
 
@@ -624,8 +743,8 @@ XSAttributeDeclaration *XSModel::getAttributeDeclaration(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);    
-    
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
+
     if (namespaceItem)
         return namespaceItem->getAttributeDeclaration(name);
 
@@ -647,7 +766,7 @@ XSTypeDefinition *XSModel::getTypeDefinition(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
     
     if (namespaceItem)
         return namespaceItem->getTypeDefinition(name);
@@ -669,8 +788,8 @@ XSAttributeGroupDefinition *XSModel::getAttributeGroup(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
-   
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
+
     if (namespaceItem)
         return namespaceItem->getAttributeGroup(name);
 
@@ -691,8 +810,8 @@ XSModelGroupDefinition *XSModel::getModelGroupDefinition(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
-   
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
+
     if (namespaceItem)
         return namespaceItem->getModelGroupDefinition(name);
 
@@ -713,8 +832,8 @@ XSNotationDeclaration *XSModel::getNotationDeclaration(const XMLCh *name
     if (compNamespace)
         namespaceItem = getNamespaceItem(compNamespace);
     else
-        namespaceItem = getNamespaceItem(emptyString);
-   
+        namespaceItem = getNamespaceItem(XMLUni::fgZeroLenString);
+
     if (namespaceItem)
         return namespaceItem->getNotationDeclaration(name);
 
diff --git a/src/xercesc/framework/psvi/XSModel.hpp b/src/xercesc/framework/psvi/XSModel.hpp
index 5c2892be5..810c3d629 100644
--- a/src/xercesc/framework/psvi/XSModel.hpp
+++ b/src/xercesc/framework/psvi/XSModel.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.10  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.9  2003/11/26 16:12:23  knoaman
  * Add a method to return the XSObject mapped to a schema grammar component
  *
@@ -313,8 +316,26 @@ public:
 
     //@}
 private:
-    // Local Functions:
-    void addGrammarToXSModel(XSNamespaceItem* namespaceItem);
+
+    // -----------------------------------------------------------------------
+    //  Helper methods
+    // -----------------------------------------------------------------------
+    void addGrammarToXSModel
+    (
+        XSNamespaceItem* namespaceItem
+    );
+    void addS4SToXSModel
+    (
+        XSNamespaceItem* const namespaceItem
+        , RefHashTableOf<DatatypeValidator>* const builtInDV
+    );
+    void addComponentToNamespace
+    (
+         XSNamespaceItem* const namespaceItem
+         , XSObject* const component
+         , int componentIndex
+         , bool addToXSModel = true
+    );
 
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
@@ -355,16 +376,14 @@ protected:
 	      MULTIVALUE_FACET          = 14       no
     */
     XSNamedMap<XSObject>*                   fComponentMap[XSConstants::MULTIVALUE_FACET];
-
     XMLStringPool*                          fURIStringPool;
     XSAnnotationList*                       fXSAnnotationList;
     RefHashTableOf<XSNamespaceItem>*        fHashNamespace;
     XSObjectFactory*                        fObjFactory;
-    RefVectorOf<XSNamespaceItem>*           fDeleteNamespace; 
-
+    RefVectorOf<XSNamespaceItem>*           fDeleteNamespace;
     XSModel*                                fParent;
     bool                                    fDeleteParent;
-    bool                                    fAddedBuiltinDatatypeValidators;
+    bool                                    fAddedS4SGrammar;
 };
 
 inline XMLStringPool*  XSModel::getURIStringPool()
diff --git a/src/xercesc/framework/psvi/XSNamespaceItem.cpp b/src/xercesc/framework/psvi/XSNamespaceItem.cpp
index 507c3441e..ac67c6e72 100644
--- a/src/xercesc/framework/psvi/XSNamespaceItem.cpp
+++ b/src/xercesc/framework/psvi/XSNamespaceItem.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.8  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.7  2003/12/15 17:23:48  cargilld
  * psvi updates; cleanup revisits and bug fixes
  *
@@ -102,6 +105,60 @@ XSNamespaceItem::XSNamespaceItem(XSModel* const       xsModel,
     , fGrammar(grammar)
     , fXSModel(xsModel)
     , fXSAnnotationList(0)
+    , fSchemaNamespace(grammar->getTargetNamespace())
+{
+    // Populate XSNamedMaps by going through the components
+    for (unsigned int i=0; i<XSConstants::MULTIVALUE_FACET; i++)
+    {        
+        switch (i+1) 
+        {
+            case XSConstants::ATTRIBUTE_DECLARATION:
+            case XSConstants::ELEMENT_DECLARATION:
+            case XSConstants::TYPE_DEFINITION:
+            case XSConstants::ATTRIBUTE_GROUP_DEFINITION:
+            case XSConstants::MODEL_GROUP_DEFINITION:
+            case XSConstants::NOTATION_DECLARATION:
+                fComponentMap[i] = new (fMemoryManager) XSNamedMap<XSObject> 
+                (
+                    20,     // size
+                    29,     // modulus
+                    fXSModel->getURIStringPool(),
+                    false,  // adoptElems 
+                    fMemoryManager
+                );               
+                fHashMap[i] = new (fMemoryManager) RefHashTableOf<XSObject>
+                (
+                    29,
+                    false,
+                    fMemoryManager
+                );
+                break;
+            default:
+                // ATTRIBUTE_USE
+                // MODEL_GROUP
+                // PARTICLE
+                // IDENTITY_CONSTRAINT
+                // WILDCARD
+                // ANNOTATION
+                // FACET
+                // MULTIVALUE
+                fComponentMap[i] = 0;
+                fHashMap[i] = 0;
+                break;
+        }
+    }
+    
+    fXSAnnotationList = new (manager) RefVectorOf <XSAnnotation> (5, false, manager);
+}
+
+XSNamespaceItem::XSNamespaceItem(XSModel* const       xsModel,
+                                 const XMLCh* const   schemaNamespace,
+                                 MemoryManager* const manager)
+    : fMemoryManager(manager)
+    , fGrammar(0)
+    , fXSModel(xsModel)
+    , fXSAnnotationList(0)
+    , fSchemaNamespace(schemaNamespace)
 {
     // Populate XSNamedMaps by going through the components
     for (unsigned int i=0; i<XSConstants::MULTIVALUE_FACET; i++)
@@ -171,11 +228,6 @@ XSNamespaceItem::~XSNamespaceItem()
 // ---------------------------------------------------------------------------
 //  XSNamespaceItem: access methods
 // ---------------------------------------------------------------------------
-const XMLCh *XSNamespaceItem::getSchemaNamespace()
-{
-    return fGrammar->getTargetNamespace();
-}
-
 XSNamedMap<XSObject> *XSNamespaceItem::getComponents(XSConstants::COMPONENT_TYPE objectType)
 {
     return fComponentMap[objectType -1];
@@ -225,7 +277,10 @@ XSNotationDeclaration *XSNamespaceItem::getNotationDeclaration(const XMLCh *name
 
 StringList *XSNamespaceItem::getDocumentLocations()
 {
-    return ((XMLSchemaDescriptionImpl*) fGrammar->getGrammarDescription())->getLocationHints();
+    if (fGrammar)
+        return ((XMLSchemaDescriptionImpl*) fGrammar->getGrammarDescription())->getLocationHints();
+
+    return 0;
 }
 
 XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/framework/psvi/XSNamespaceItem.hpp b/src/xercesc/framework/psvi/XSNamespaceItem.hpp
index 080cdd3a2..032bd48ed 100644
--- a/src/xercesc/framework/psvi/XSNamespaceItem.hpp
+++ b/src/xercesc/framework/psvi/XSNamespaceItem.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.8  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.7  2003/12/01 23:23:26  neilg
  * fix for bug 25118; thanks to Jeroen Witmond
  *
@@ -138,6 +141,13 @@ public:
         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
     );
 
+    XSNamespaceItem
+    (
+        XSModel* const         xsModel
+        , const XMLCh* const   schemaNamespace
+        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
+    );
+
     //@};
 
     /** @name Destructor */
@@ -234,7 +244,7 @@ public:
     /** methods needed by implementation */
 
     //@{
-    SchemaGrammar*  getSchemaGrammar()  const;
+
 
     //@}
 private:
@@ -247,6 +257,7 @@ private:
 
 protected:
     friend class XSModel;
+    friend class XSObjectFactory;
     // -----------------------------------------------------------------------
     //  data members
     // -----------------------------------------------------------------------
@@ -275,22 +286,22 @@ protected:
     */
     XSNamedMap<XSObject>*                   fComponentMap[XSConstants::MULTIVALUE_FACET];
     XSAnnotationList*                       fXSAnnotationList;
-
     RefHashTableOf<XSObject>*               fHashMap[XSConstants::MULTIVALUE_FACET];
-
+    const XMLCh*                            fSchemaNamespace;
 };
 
-inline SchemaGrammar* XSNamespaceItem::getSchemaGrammar() const
+inline XSAnnotationList* XSNamespaceItem::getAnnotations()
 {
-    return fGrammar;
+    return fXSAnnotationList;
 }
 
-inline XSAnnotationList* XSNamespaceItem::getAnnotations()
+inline const XMLCh *XSNamespaceItem::getSchemaNamespace()
 {
-    return fXSAnnotationList;
+    return fSchemaNamespace;
 }
 
 
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/framework/psvi/XSSimpleTypeDefinition.hpp b/src/xercesc/framework/psvi/XSSimpleTypeDefinition.hpp
index 9f1f1845c..587753596 100644
--- a/src/xercesc/framework/psvi/XSSimpleTypeDefinition.hpp
+++ b/src/xercesc/framework/psvi/XSSimpleTypeDefinition.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.10  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.9  2003/12/01 23:23:26  neilg
  * fix for bug 25118; thanks to Jeroen Witmond
  *
@@ -428,6 +431,7 @@ private:
         , XSMultiValueFacetList* const xsMultiValueFacetList
         , StringList* const            patternList
     );
+    void setPrimitiveType(XSSimpleTypeDefinition*  const toSet);
 
     friend class XSObjectFactory;
 
@@ -504,6 +508,12 @@ inline XSAnnotationList *XSSimpleTypeDefinition::getAnnotations()
     return fXSAnnotationList;
 }
 
+inline void
+XSSimpleTypeDefinition::setPrimitiveType(XSSimpleTypeDefinition* const toSet)
+{
+    fPrimitiveOrItemType = toSet;
+}
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/internal/XSObjectFactory.cpp b/src/xercesc/internal/XSObjectFactory.cpp
index cd682b1ed..735d88bb2 100644
--- a/src/xercesc/internal/XSObjectFactory.cpp
+++ b/src/xercesc/internal/XSObjectFactory.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.13  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.12  2003/12/17 20:50:35  knoaman
  * PSVI: fix for annotation of attributes in attributeGroup/derived types
  *
@@ -318,32 +321,36 @@ XSObjectFactory::createWildcardParticle(const ContentSpecNode* const rootNode,
     return 0;
 }
 
-XSAttributeDeclaration* XSObjectFactory::addOrFind(SchemaAttDef* const attDef,
-                                                   XSModel* const xsModel)
+XSAttributeDeclaration*
+XSObjectFactory::addOrFind(SchemaAttDef* const attDef,
+                           XSModel* const xsModel,
+                           XSComplexTypeDefinition* const enclosingTypeDef)
 {
     XSAttributeDeclaration* xsObj = (XSAttributeDeclaration*) getObjectFromMap(attDef, xsModel);
-    if (!xsObj)
+
+    if (xsObj)
+    {
+        if (xsObj->getScope() == XSConstants::SCOPE_LOCAL
+            && xsObj->getEnclosingCTDefinition() == 0
+            && enclosingTypeDef)
+            xsObj->setEnclosingCTDefinition(enclosingTypeDef);
+    }
+    else
     {
         XSSimpleTypeDefinition* xsType = 0;
         if (attDef->getDatatypeValidator())
             xsType = addOrFind(attDef->getDatatypeValidator(), xsModel);
 
-        // need grammar to determine if scope is global:                   
-                                               
         XSConstants::SCOPE scope = XSConstants::SCOPE_ABSENT;
         XSComplexTypeDefinition* enclosingCTDefinition = 0;
-        XSNamespaceItem* namespaceItem = xsModel->getNamespaceItem(xsModel->getURIStringPool()->getValueForId(attDef->getAttName()->getURI()));
-        if (namespaceItem)
+
+        if (attDef->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
+            scope = XSConstants::SCOPE_GLOBAL;
+        else if (attDef->getPSVIScope() == PSVIDefs::SCP_LOCAL)
         {
-            // REVISIT: what if local name and global name are the same???
-            if (namespaceItem->getSchemaGrammar()->getAttributeDeclRegistry()->get(attDef))
-                scope = XSConstants::SCOPE_GLOBAL;
-            else if (attDef->getEnclosingCT())
-            {
-                scope = XSConstants::SCOPE_LOCAL;
-                enclosingCTDefinition = addOrFind(attDef->getEnclosingCT(), xsModel);
-            }
-        }        
+            scope = XSConstants::SCOPE_LOCAL;
+            enclosingCTDefinition = enclosingTypeDef;
+        }
 
         xsObj = new (fMemoryManager) XSAttributeDeclaration
         (
@@ -363,25 +370,23 @@ XSAttributeDeclaration* XSObjectFactory::addOrFind(SchemaAttDef* const attDef,
 
 XSSimpleTypeDefinition*
 XSObjectFactory::addOrFind(DatatypeValidator* const validator,
-                           XSModel* const xsModel)
+                           XSModel* const xsModel,
+                           bool isAnySimpleType)
 {
     XSSimpleTypeDefinition* xsObj = (XSSimpleTypeDefinition*) getObjectFromMap(validator, xsModel);
-
     if (!xsObj)
     {
         XSSimpleTypeDefinition* baseType = 0;
         XSSimpleTypeDefinitionList* memberTypes = 0;
         XSSimpleTypeDefinition* primitiveOrItemType = 0;
         XSSimpleTypeDefinition::VARIETY typeVariety = XSSimpleTypeDefinition::VARIETY_ATOMIC;
+        bool primitiveTypeSelf = false;
 
-        // compute fBaseType
-        DatatypeValidator* baseDV = validator->getBaseValidator();
-        if (baseDV)
-            baseType = addOrFind(baseDV, xsModel);
-    
         //REVISIT: the getFixed method is protected so added friend XSObjectFactory
         //         to DatatypeValidator class... 
         DatatypeValidator::ValidatorType dvType = validator->getType();
+        DatatypeValidator* baseDV = validator->getBaseValidator();
+
         if (dvType == DatatypeValidator::Union)
         {
             typeVariety = XSSimpleTypeDefinition::VARIETY_UNION;
@@ -393,26 +398,53 @@ XSObjectFactory::addOrFind(DatatypeValidator* const validator,
                 for (unsigned int i=0; i<size; i++)
                     memberTypes->addElement(addOrFind(membersDV->elementAt(i), xsModel));
             }
+            
+            if (baseDV)
+            {
+                baseType = addOrFind(baseDV, xsModel);
+            }
+            else
+            {
+                baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
+                (
+                    SchemaSymbols::fgDT_ANYSIMPLETYPE
+                    , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
+                );
+            }
         } 
         else if (dvType == DatatypeValidator::List)
         {
             typeVariety = XSSimpleTypeDefinition::VARIETY_LIST;
-
-            while (baseDV && (baseDV->getType() == DatatypeValidator::List))
+            if (baseDV->getType() == DatatypeValidator::List)
             {
-                addOrFind(baseDV, xsModel);
-                baseDV = baseDV->getBaseValidator();
+                baseType = addOrFind(baseDV, xsModel);
+                primitiveOrItemType = baseType->getItemType();
             }
-            if (baseDV)
+            else
+            {
+                baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
+                (
+                    SchemaSymbols::fgDT_ANYSIMPLETYPE
+                    , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
+                );
                 primitiveOrItemType = addOrFind(baseDV, xsModel);
+            }
         }
-        else
+        else if (!isAnySimpleType)
         {
-            // REVISIT: assume ATOMIC but what about VARIETY_ABSENT?
-            while (baseDV)
+            if (baseDV)
             {
-                primitiveOrItemType = addOrFind(baseDV, xsModel);
-                baseDV = baseDV->getBaseValidator();
+                baseType = addOrFind(baseDV, xsModel);
+                primitiveOrItemType = baseType->getPrimitiveType();
+            }
+            else // built-in
+            {
+                baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
+                (
+                    SchemaSymbols::fgDT_ANYSIMPLETYPE
+                    , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
+                );
+                primitiveTypeSelf = true;
             }
         }
 
@@ -429,6 +461,9 @@ XSObjectFactory::addOrFind(DatatypeValidator* const validator,
         );
         putObjectInMap(validator, xsObj, xsModel);
 
+        if (primitiveTypeSelf)
+            xsObj->setPrimitiveType(xsObj);
+
         // process facets
         if (validator->getFacetsDefined())
             processFacets(validator, xsModel, xsObj);
@@ -443,7 +478,12 @@ XSObjectFactory::addOrFind(SchemaElementDecl* const elemDecl,
                            XSComplexTypeDefinition* const enclosingTypeDef)
 {
     XSElementDeclaration* xsObj = (XSElementDeclaration*) getObjectFromMap(elemDecl, xsModel);
-    if (!xsObj)
+    if (xsObj)
+    {
+        if (!xsObj->getEnclosingCTDefinition() && enclosingTypeDef)
+            xsObj->setEnclosingCTDefinition(enclosingTypeDef);
+    }
+    else
     {
         XSElementDeclaration*        xsSubElem = 0;
         XSTypeDefinition*            xsType = 0;
@@ -491,10 +531,9 @@ XSObjectFactory::addOrFind(SchemaElementDecl* const elemDecl,
         }
 
         XSConstants::SCOPE elemScope = XSConstants::SCOPE_ABSENT;
-
-        if (enclosingTypeDef)
+        if (elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
             elemScope = XSConstants::SCOPE_LOCAL;
-        else if (elemDecl->getEnclosingScope() == Grammar::TOP_LEVEL_SCOPE)
+        else if (elemDecl->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
             elemScope = XSConstants::SCOPE_GLOBAL;
 
         xsObj = new (fMemoryManager) XSElementDeclaration
@@ -516,6 +555,15 @@ XSObjectFactory::addOrFind(SchemaElementDecl* const elemDecl,
             xsType = addOrFind(elemDecl->getComplexTypeInfo(), xsModel);
             xsObj->setTypeDefinition(xsType);
         }
+        else if (!xsType)
+        {
+            xsType = xsModel->getTypeDefinition
+            (
+                SchemaSymbols::fgATTVAL_ANYTYPE
+                , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
+            );
+            xsObj->setTypeDefinition(xsType);
+        }
     }
 
     return xsObj;
@@ -553,10 +601,15 @@ XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
         }
 
         // compute fBase
-        if (typeInfo->getBaseComplexTypeInfo())
+        bool isAnyType = false;
+        if (typeInfo->getBaseComplexTypeInfo() == typeInfo) // case of anyType
+            isAnyType = true;
+        else if (typeInfo->getBaseComplexTypeInfo())
             xsBaseType = addOrFind(typeInfo->getBaseComplexTypeInfo(), xsModel);
         else if (typeInfo->getBaseDatatypeValidator())
             xsBaseType = addOrFind(typeInfo->getBaseDatatypeValidator(), xsModel);
+        else // base is anyType
+            xsBaseType = xsModel->getTypeDefinition(SchemaSymbols::fgATTVAL_ANYTYPE, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
 
         // compute particle
         ContentSpecNode* contentSpec = typeInfo->getContentSpec();
@@ -576,6 +629,9 @@ XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
             , fMemoryManager
         );
         putObjectInMap(typeInfo, xsObj, xsModel);
+
+        if (isAnyType)
+            xsObj->setBaseType(xsObj);
        
         if (typeInfo->hasAttDefs())
         {
@@ -589,7 +645,7 @@ XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
                 if (attDef.getBaseAttDecl())
                     xsAttDecl = addOrFind(attDef.getBaseAttDecl(), xsModel);
                 else
-                    xsAttDecl = addOrFind(&attDef, xsModel);
+                    xsAttDecl = addOrFind(&attDef, xsModel, xsObj);
 
                 XSAttributeUse* attUse = createXSAttributeUse(xsAttDecl, xsModel);
                 xsAttList->addElement(attUse);
@@ -603,7 +659,8 @@ XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
         {
             SchemaElementDecl* elemDecl = typeInfo->elementAt(j);
 
-            if (elemDecl->getEnclosingScope() == typeInfo->getScopeDefined())
+            if (elemDecl->getEnclosingScope() == typeInfo->getScopeDefined()
+                && elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
                 addOrFind(elemDecl, xsModel, xsObj);
         }
     }
@@ -806,9 +863,13 @@ XSAnnotation* XSObjectFactory::getAnnotationFromModel(XSModel* const xsModel,
     XSAnnotation* annot = 0;
     for (unsigned int i=0; i<namespaceItemList->size(); i++)
     {
-        annot = namespaceItemList->elementAt(i)->getSchemaGrammar()->getAnnotation(key);
-        if (annot)
-            return annot;
+        XSNamespaceItem* nsItem = namespaceItemList->elementAt(i);
+        if (nsItem->fGrammar)
+        {
+            annot = nsItem->fGrammar->getAnnotation(key);
+            if (annot)
+                return annot;
+        }
     }
 
     if (xsModel->fParent)
diff --git a/src/xercesc/internal/XSObjectFactory.hpp b/src/xercesc/internal/XSObjectFactory.hpp
index 26da5a003..b3ca75060 100644
--- a/src/xercesc/internal/XSObjectFactory.hpp
+++ b/src/xercesc/internal/XSObjectFactory.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.6  2003/12/24 17:42:02  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.5  2003/12/17 20:50:35  knoaman
  * PSVI: fix for annotation of attributes in attributeGroup/derived types
  *
@@ -139,12 +142,14 @@ private:
     (
         SchemaAttDef* const attDef
         , XSModel* const xsModel
+        , XSComplexTypeDefinition* const enclosingTypeDef = 0
     );
 
     XSSimpleTypeDefinition* addOrFind
     (
         DatatypeValidator* const validator
         , XSModel* const xsModel
+        , bool isAnySimpleType = false
     );
 
     XSElementDeclaration* addOrFind
diff --git a/src/xercesc/validators/schema/ComplexTypeInfo.cpp b/src/xercesc/validators/schema/ComplexTypeInfo.cpp
index c96cb94af..033ed57e3 100644
--- a/src/xercesc/validators/schema/ComplexTypeInfo.cpp
+++ b/src/xercesc/validators/schema/ComplexTypeInfo.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.23  2003/12/24 17:42:03  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.22  2003/12/17 00:18:40  cargilld
  * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
  *
@@ -218,11 +221,117 @@
 #include <xercesc/validators/common/MixedContentModel.hpp>
 #include <xercesc/validators/common/SimpleContentModel.hpp>
 #include <xercesc/validators/schema/XSDLocator.hpp>
-
 #include <xercesc/internal/XTemplateSerializer.hpp>
+#include <xercesc/util/XMLRegisterCleanup.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+// ---------------------------------------------------------------------------
+//  Local static data
+// ---------------------------------------------------------------------------
+static bool               sAnyTypeMutexRegistered = false;
+static XMLMutex*          sAnyTypeMutex = 0;
+static XMLRegisterCleanup anyTypeCleanup;
+
+
+// ---------------------------------------------------------------------------
+//  ComplexTypeInfo: Static member data
+// ---------------------------------------------------------------------------
+ComplexTypeInfo* ComplexTypeInfo::fAnyType = 0;
+
+
+// ---------------------------------------------------------------------------
+//  ComplexTypeInfo: Static meber methods
+// ---------------------------------------------------------------------------
+void ComplexTypeInfo::reinitAnyType() {
+
+    delete fAnyType;
+    fAnyType = 0;
+
+    // delete local static data
+    delete sAnyTypeMutex;
+    sAnyTypeMutex = 0;
+    sAnyTypeMutexRegistered = false;
+}
+
+ComplexTypeInfo* ComplexTypeInfo::getAnyType(unsigned int emptyNSId)
+{
+    if (!sAnyTypeMutexRegistered)
+    {
+        XMLMutex* tmpMutex = new XMLMutex;
+        if (XMLPlatformUtils::compareAndSwap((void**)&sAnyTypeMutex, tmpMutex, 0))
+        {
+            // Someone beat us to it, so let's clean up ours
+            delete tmpMutex;
+        }
+
+        // Now lock it and try to register it
+        XMLMutexLock lock(sAnyTypeMutex);
+
+        // If we got here first, then register it and set the registered flag
+        if (!sAnyTypeMutexRegistered)
+        {
+            // create type name
+            XMLCh typeName[128];
+            unsigned int nsLen = XMLString::stringLen(
+                SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
+
+			XMLString::copyString(
+                typeName, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
+            typeName[nsLen] = chComma;
+            XMLString::copyString(
+                typeName + nsLen + 1, SchemaSymbols::fgATTVAL_ANYTYPE);
+
+            // Create and initialize 'anyType'
+            fAnyType = new ComplexTypeInfo();
+
+            ContentSpecNode* term = new ContentSpecNode
+            (
+                new QName
+                (
+                    XMLUni::fgZeroLenString
+                    , XMLUni::fgZeroLenString
+                    , emptyNSId
+                )
+                , false
+            );
+            term->setType(ContentSpecNode::Any_Lax);
+            term->setMinOccurs(0);
+            term->setMaxOccurs(SchemaSymbols::XSD_UNBOUNDED);
+
+            ContentSpecNode* particle = new ContentSpecNode
+            (
+                ContentSpecNode::ModelGroupSequence
+                , term
+                , 0
+            );
+
+            SchemaAttDef* attWildCard = new SchemaAttDef
+            (
+                XMLUni::fgZeroLenString
+                , XMLUni::fgZeroLenString
+                , emptyNSId
+                , XMLAttDef::Any_Any
+                , XMLAttDef::ProcessContents_Lax
+            );
+
+            fAnyType->setTypeName(typeName);
+            fAnyType->setBaseComplexTypeInfo(fAnyType);
+            fAnyType->setDerivedBy(SchemaSymbols::XSD_RESTRICTION);
+            fAnyType->setContentType(SchemaElementDecl::Mixed_Complex);
+            fAnyType->setContentSpec(particle);
+            fAnyType->setAttWildCard(attWildCard);
+
+            // register cleanup method
+            anyTypeCleanup.registerCleanup(reinitAnyType);
+            sAnyTypeMutexRegistered = true;
+        }
+    }
+
+    return fAnyType;
+}
+
+
 // ---------------------------------------------------------------------------
 //  ComplexTypeInfo: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -1044,6 +1153,7 @@ void ComplexTypeInfo::serialize(XSerializeEngine& serEng)
     }
 }
 
+
 XERCES_CPP_NAMESPACE_END
 
 /**
diff --git a/src/xercesc/validators/schema/ComplexTypeInfo.hpp b/src/xercesc/validators/schema/ComplexTypeInfo.hpp
index 3132f70ea..299b0ef5f 100644
--- a/src/xercesc/validators/schema/ComplexTypeInfo.hpp
+++ b/src/xercesc/validators/schema/ComplexTypeInfo.hpp
@@ -192,6 +192,18 @@ public:
       , XMLValidator*     const pValidator
     ) ;
 
+    /**
+      * Return a singleton that represents 'anyType'
+      *
+      * @param emptyNSId the uri id of the empty namespace 
+      */
+    static ComplexTypeInfo* getAnyType(unsigned int emptyNSId);
+
+    /**
+      *  Notification that lazy data has been deleted
+      */
+    static void reinitAnyType();
+
     /***
      * Support for Serialization/De-serialization
      ***/
@@ -249,6 +261,8 @@ private:
     unsigned int                       fContentSpecOrgURISize;
     XSDLocator*                        fLocator;
     MemoryManager*                     fMemoryManager;
+
+    static ComplexTypeInfo*            fAnyType;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/src/xercesc/validators/schema/PSVIDefs.hpp b/src/xercesc/validators/schema/PSVIDefs.hpp
index 3eb966bd1..efccd2a3f 100644
--- a/src/xercesc/validators/schema/PSVIDefs.hpp
+++ b/src/xercesc/validators/schema/PSVIDefs.hpp
@@ -79,7 +79,12 @@ public:
         COMPLEX = 2
     };
 
-
+    enum PSVIScope
+    {
+        SCP_ABSENT    // declared in group/attribute group
+	    , SCP_GLOBAL  // global decalarion or ref
+	    , SCP_LOCAL   // local declaration
+    };
 };
 
 XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/validators/schema/SchemaAttDef.cpp b/src/xercesc/validators/schema/SchemaAttDef.cpp
index e6b4eaa81..00934a9da 100644
--- a/src/xercesc/validators/schema/SchemaAttDef.cpp
+++ b/src/xercesc/validators/schema/SchemaAttDef.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.12  2003/12/24 17:42:03  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.11  2003/12/17 20:50:35  knoaman
  * PSVI: fix for annotation of attributes in attributeGroup/derived types
  *
@@ -137,7 +140,7 @@ SchemaAttDef::SchemaAttDef(MemoryManager* const manager) :
     , fNamespaceList(0)
     , fValidity(PSVIDefs::UNKNOWN)
     , fValidation(PSVIDefs::NONE)
-    , fEnclosingCT(0)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)    
     , fBaseAttDecl(0)
 {
 }
@@ -156,7 +159,7 @@ SchemaAttDef::SchemaAttDef( const XMLCh* const           prefix
     , fNamespaceList(0)
     , fValidity(PSVIDefs::UNKNOWN)
     , fValidation(PSVIDefs::NONE)
-    , fEnclosingCT(0)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)
     , fBaseAttDecl(0)
 {
     fAttName = new (manager) QName(prefix, localPart, uriId, manager);
@@ -179,7 +182,7 @@ SchemaAttDef::SchemaAttDef( const XMLCh* const           prefix
     , fNamespaceList(0)
     , fValidity(PSVIDefs::UNKNOWN)
     , fValidation(PSVIDefs::NONE)
-    , fEnclosingCT(0)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)
     , fBaseAttDecl(0)
 {
     fAttName = new (manager) QName(prefix, localPart, uriId, manager);
@@ -198,7 +201,7 @@ SchemaAttDef::SchemaAttDef(const SchemaAttDef* other) :
     , fNamespaceList(0)
     , fValidity(other->fValidity)
     , fValidation(other->fValidation)
-    , fEnclosingCT(other->fEnclosingCT)
+    , fPSVIScope(other->fPSVIScope)
     , fBaseAttDecl(other->fBaseAttDecl)
 {
     QName* otherName = other->getAttName();
@@ -254,7 +257,6 @@ void SchemaAttDef::serialize(XSerializeEngine& serEng)
 
         serEng<<(int)fValidity;
         serEng<<(int)fValidation;
-        serEng<<fEnclosingCT;
         serEng<<fBaseAttDecl;
     }
     else
@@ -277,8 +279,7 @@ void SchemaAttDef::serialize(XSerializeEngine& serEng)
 
         serEng>>i;
         fValidation = (PSVIDefs::Validation)i;
-        
-        serEng>>fEnclosingCT;
+
         serEng>>fBaseAttDecl;
     }
 }
diff --git a/src/xercesc/validators/schema/SchemaAttDef.hpp b/src/xercesc/validators/schema/SchemaAttDef.hpp
index b4d82990d..a4327308a 100644
--- a/src/xercesc/validators/schema/SchemaAttDef.hpp
+++ b/src/xercesc/validators/schema/SchemaAttDef.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.11  2003/12/24 17:42:03  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.10  2003/12/17 20:50:35  knoaman
  * PSVI: fix for annotation of attributes in attributeGroup/derived types
  *
@@ -264,9 +267,14 @@ public :
     QName* getAttName() const;
     DatatypeValidator* getDatatypeValidator() const;
     ValueVectorOf<unsigned int>* getNamespaceList() const;
-    ComplexTypeInfo* getEnclosingCT() const;
     const SchemaAttDef* getBaseAttDecl() const;
     SchemaAttDef* getBaseAttDecl();
+    PSVIDefs::PSVIScope getPSVIScope() const;
+
+    /*
+     * @deprecated
+     */
+    ComplexTypeInfo* getEnclosingCT() const;
 
     // -----------------------------------------------------------------------
     //  Setter methods
@@ -281,6 +289,7 @@ public :
     void setDatatypeValidator(DatatypeValidator* newDatatypeValidator);
     void setAnyDatatypeValidator(DatatypeValidator* newDatatypeValidator);
     void setBaseAttDecl(SchemaAttDef* const attDef);
+    void setPSVIScope(const PSVIDefs::PSVIScope toSet);
 
     /*
      * @deprecated
@@ -350,7 +359,7 @@ private :
     ValueVectorOf<unsigned int>* fNamespaceList;
     PSVIDefs::Validity           fValidity;
     PSVIDefs::Validation         fValidation;
-    ComplexTypeInfo*             fEnclosingCT;
+    PSVIDefs::PSVIScope          fPSVIScope;
     SchemaAttDef*                fBaseAttDecl;
 };
 
@@ -486,7 +495,7 @@ SchemaAttDef::getNamespaceList() const {
 
 inline ComplexTypeInfo* SchemaAttDef::getEnclosingCT() const
 {
-    return fEnclosingCT;
+    return 0;
 }
 
 inline SchemaAttDef* SchemaAttDef::getBaseAttDecl()
@@ -499,6 +508,11 @@ inline const SchemaAttDef* SchemaAttDef::getBaseAttDecl() const
     return fBaseAttDecl;
 }
 
+inline PSVIDefs::PSVIScope SchemaAttDef::getPSVIScope() const
+{
+    return fPSVIScope;
+}
+
 // ---------------------------------------------------------------------------
 //  SchemaAttDef: Setter methods
 // ---------------------------------------------------------------------------
@@ -556,9 +570,8 @@ inline void SchemaAttDef::reset() {
     fValidation = PSVIDefs::NONE;    
 }
 
-inline void SchemaAttDef::setEnclosingCT(ComplexTypeInfo* complexTypeInfo)
+inline void SchemaAttDef::setEnclosingCT(ComplexTypeInfo*)
 {
-    fEnclosingCT = complexTypeInfo;
 }
 
 inline void SchemaAttDef::setBaseAttDecl(SchemaAttDef* const attDef)
@@ -566,6 +579,11 @@ inline void SchemaAttDef::setBaseAttDecl(SchemaAttDef* const attDef)
     fBaseAttDecl = attDef;
 }
 
+inline void SchemaAttDef::setPSVIScope(const PSVIDefs::PSVIScope toSet)
+{
+    fPSVIScope = toSet;
+}
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/validators/schema/SchemaElementDecl.cpp b/src/xercesc/validators/schema/SchemaElementDecl.cpp
index 134086190..540c6e28a 100644
--- a/src/xercesc/validators/schema/SchemaElementDecl.cpp
+++ b/src/xercesc/validators/schema/SchemaElementDecl.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.18  2003/12/24 17:42:03  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.17  2003/12/17 00:18:40  cargilld
  * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
  *
@@ -183,6 +186,7 @@ XERCES_CPP_NAMESPACE_BEGIN
 SchemaElementDecl::SchemaElementDecl(MemoryManager* const manager) :
     XMLElementDecl(manager)
     , fModelType(Any)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)
     , fDatatypeValidator(0)
     , fEnclosingScope(Grammar::TOP_LEVEL_SCOPE)
     , fBlockSet(0)
@@ -212,6 +216,7 @@ SchemaElementDecl::SchemaElementDecl(const XMLCh* const                  prefix
                                    , MemoryManager* const                manager) :
     XMLElementDecl(manager)
     , fModelType(type)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)
     , fDatatypeValidator(0)
     , fEnclosingScope(enclosingScope)
     , fBlockSet(0)
@@ -240,6 +245,7 @@ SchemaElementDecl::SchemaElementDecl(const QName* const                  element
                                    , MemoryManager* const                manager) :
     XMLElementDecl(manager)
     , fModelType(type)
+    , fPSVIScope(PSVIDefs::SCP_ABSENT)
     , fDatatypeValidator(0)
     , fEnclosingScope(enclosingScope)
     , fBlockSet(0)
diff --git a/src/xercesc/validators/schema/SchemaElementDecl.hpp b/src/xercesc/validators/schema/SchemaElementDecl.hpp
index 78e1b8371..ccbe4844a 100644
--- a/src/xercesc/validators/schema/SchemaElementDecl.hpp
+++ b/src/xercesc/validators/schema/SchemaElementDecl.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.19  2003/12/24 17:42:03  knoaman
+ * Misc. PSVI updates
+ *
  * Revision 1.18  2003/12/12 18:36:37  peiyongz
  * getObjectType()
  *
@@ -207,7 +210,6 @@ public :
         , ModelTypes_Count
     };
 
-
     // -----------------------------------------------------------------------
     //  Constructors and Destructor
     // -----------------------------------------------------------------------
@@ -266,6 +268,7 @@ public :
     const SchemaAttDef* getAttWildCard() const;
     SchemaAttDef* getAttWildCard();
     ModelTypes getModelType() const;
+    PSVIDefs::PSVIScope getPSVIScope() const;
     DatatypeValidator* getDatatypeValidator() const;
     int getEnclosingScope() const;
     int getFinalSet() const;
@@ -381,6 +384,7 @@ public :
     // -----------------------------------------------------------------------
     void setElemId(unsigned int elemId);
     void setModelType(const SchemaElementDecl::ModelTypes toSet);
+    void setPSVIScope(const PSVIDefs::PSVIScope toSet);
     void setDatatypeValidator(DatatypeValidator* newDatatypeValidator);
     void setEnclosingScope(const int enclosingScope);
     void setFinalSet(const int finalSet);
@@ -509,6 +513,7 @@ private :
 
     // -----------------------------------------------------------------------
     ModelTypes                         fModelType;
+    PSVIDefs::PSVIScope                fPSVIScope;
     DatatypeValidator*                 fDatatypeValidator;
     int                                fEnclosingScope;
     int                                fFinalSet;
@@ -584,6 +589,11 @@ inline SchemaElementDecl::ModelTypes SchemaElementDecl::getModelType() const
     return fModelType;
 }
 
+inline PSVIDefs::PSVIScope SchemaElementDecl::getPSVIScope() const
+{
+    return fPSVIScope;
+}
+
 inline DatatypeValidator* SchemaElementDecl::getDatatypeValidator() const
 {
 
@@ -815,6 +825,12 @@ SchemaElementDecl::setModelType(const SchemaElementDecl::ModelTypes toSet)
     fModelType = toSet;
 }
 
+inline void
+SchemaElementDecl::setPSVIScope(const PSVIDefs::PSVIScope toSet)
+{
+    fPSVIScope = toSet;
+}
+
 inline void SchemaElementDecl::setDatatypeValidator(DatatypeValidator* newDatatypeValidator)
 {
     fDatatypeValidator = newDatatypeValidator;
diff --git a/src/xercesc/validators/schema/TraverseSchema.cpp b/src/xercesc/validators/schema/TraverseSchema.cpp
index e7f3d1354..2316d5011 100644
--- a/src/xercesc/validators/schema/TraverseSchema.cpp
+++ b/src/xercesc/validators/schema/TraverseSchema.cpp
@@ -390,7 +390,7 @@ void TraverseSchema::preprocessSchema(DOMElement* const schemaRoot,
     fTargetNSURIString = fSchemaGrammar->getTargetNamespace();
     fTargetNSURI = fURIStringPool->addOrFind(fTargetNSURIString);
 
-    XMLSchemaDescription* gramDesc = (XMLSchemaDescription*) fSchemaGrammar->getGrammarDescription();       
+    XMLSchemaDescription* gramDesc = (XMLSchemaDescription*) fSchemaGrammar->getGrammarDescription();
     gramDesc->setTargetNamespace(fTargetNSURIString);
 
     fGrammarResolver->putGrammar(fSchemaGrammar);
@@ -2396,24 +2396,22 @@ void TraverseSchema::traverseAttributeDecl(const DOMElement* const elem,
     if (!janAnnot.isDataNull())
         fSchemaGrammar->putAnnotation(attDef, janAnnot.release());
 
-    if (topLevel) {
+    if (topLevel)
+    {
         fAttributeDeclRegistry->put((void*) fStringPool->getValueForId(fStringPool->addOrFind(name)), attDef);
+        attDef->setPSVIScope(PSVIDefs::SCP_GLOBAL);
     }
-    else {
-	
-        bool toClone = false;
-
-        if (typeInfo) {
-            toClone = true;
+    else
+    {
+        if (typeInfo)
+        {
             typeInfo->addAttDef(attDef);
             if (!fCurrentAttGroupInfo)
-            {
-                attDef->setEnclosingCT(typeInfo);
-            }
+                attDef->setPSVIScope(PSVIDefs::SCP_LOCAL);
         }
 
         if (fCurrentAttGroupInfo) {
-            fCurrentAttGroupInfo->addAttDef(attDef, toClone);
+            fCurrentAttGroupInfo->addAttDef(attDef, (typeInfo != 0));
         }
     }
 }
@@ -2505,14 +2503,16 @@ TraverseSchema::traverseElementDecl(const DOMElement* const elem,
         if (!janAnnot.isDataNull())
             fSchemaGrammar->putAnnotation(elemDecl, janAnnot.release());
 
-        if (fCurrentGroupInfo &&
-            elemDecl->getEnclosingScope() == fCurrentGroupInfo->getScope()) {
-            fCurrentGroupInfo->addElement(elemDecl);
-        }
-
         if (fCurrentComplexType &&
             elemDecl->getEnclosingScope() == fCurrentComplexType->getScopeDefined()) {
             fCurrentComplexType->addElement(elemDecl);
+            elemDecl->setPSVIScope(PSVIDefs::SCP_LOCAL);
+        }
+
+        if (fCurrentGroupInfo &&
+            elemDecl->getEnclosingScope() == fCurrentGroupInfo->getScope()) {
+            fCurrentGroupInfo->addElement(elemDecl);
+            elemDecl->setPSVIScope(PSVIDefs::SCP_ABSENT);
         }
     }
 
@@ -5387,6 +5387,9 @@ TraverseSchema::createSchemaElementDecl(const DOMElement* const elem,
 
     elemDecl->setCreateReason(XMLElementDecl::Declared);
 
+    if (topLevel)
+        elemDecl->setPSVIScope(PSVIDefs::SCP_GLOBAL);
+
     // process attributes
     processElemDeclAttrs(elem, elemDecl, valConstraint, topLevel);
 
@@ -5544,6 +5547,7 @@ void TraverseSchema::processAttributeDeclRef(const DOMElement* const elem,
                                             0, fGrammarPoolMemoryManager);
 
     attDef->setBaseAttDecl(refAttDef);
+    attDef->setPSVIScope(PSVIDefs::SCP_GLOBAL);
 
     if (refAttDefType == XMLAttDef::Fixed) {
         if (required && !invalidAttUse) {
-- 
GitLab