GME  13
XSObjectFactory.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/internal/XSObjectFactory.hpp>
00027 #include <xercesc/framework/psvi/XSModel.hpp>
00028 #include <xercesc/framework/psvi/XSParticle.hpp>
00029 #include <xercesc/framework/psvi/XSModelGroup.hpp>
00030 #include <xercesc/framework/psvi/XSElementDeclaration.hpp>
00031 #include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
00032 #include <xercesc/framework/psvi/XSSimpleTypeDefinition.hpp>
00033 #include <xercesc/framework/psvi/XSModelGroupDefinition.hpp>
00034 #include <xercesc/framework/psvi/XSAttributeGroupDefinition.hpp>
00035 #include <xercesc/framework/psvi/XSWildcard.hpp>
00036 #include <xercesc/framework/psvi/XSNamespaceItem.hpp>
00037 #include <xercesc/framework/psvi/XSIDCDefinition.hpp>
00038 #include <xercesc/framework/psvi/XSAttributeUse.hpp>
00039 #include <xercesc/framework/psvi/XSAttributeDeclaration.hpp>
00040 #include <xercesc/framework/psvi/XSNotationDeclaration.hpp>
00041 #include <xercesc/framework/psvi/XSFacet.hpp>
00042 #include <xercesc/framework/psvi/XSMultiValueFacet.hpp>
00043 #include <xercesc/framework/psvi/XSAnnotation.hpp>
00044 #include <xercesc/validators/common/ContentSpecNode.hpp>
00045 #include <xercesc/validators/datatype/DatatypeValidator.hpp>
00046 #include <xercesc/validators/schema/SchemaAttDefList.hpp>
00047 #include <xercesc/validators/schema/SchemaGrammar.hpp>
00048 #include <xercesc/validators/schema/XercesGroupInfo.hpp>
00049 #include <xercesc/validators/schema/XercesAttGroupInfo.hpp>
00050 #include <xercesc/validators/schema/identity/IdentityConstraint.hpp>
00051 #include <xercesc/validators/schema/identity/IC_KeyRef.hpp>
00052 #include <xercesc/validators/schema/identity/XercesXPath.hpp>
00053 #include <xercesc/util/XMLStringTokenizer.hpp>
00054 
00055 XERCES_CPP_NAMESPACE_BEGIN
00056 
00057 static XMLCh regexSeparator[] = {chPipe, chNull};
00058 
00059 
00060 // ---------------------------------------------------------------------------
00061 //  XSObjectFactory: Constructors and Destructor
00062 // ---------------------------------------------------------------------------
00063 XSObjectFactory::XSObjectFactory(MemoryManager* const manager)
00064     : fMemoryManager(manager)
00065     , fXercesToXSMap(0)
00066     , fDeleteVector(0)
00067 {
00068     fDeleteVector = new (manager) RefVectorOf<XSObject>(20, true, manager);
00069     fXercesToXSMap = new (manager) RefHashTableOf<XSObject, PtrHasher> (
00070         109, false, manager);
00071 }
00072 
00073 XSObjectFactory::~XSObjectFactory()
00074 {
00075     delete fXercesToXSMap;
00076     delete fDeleteVector;
00077 }
00078 
00079 // ---------------------------------------------------------------------------
00080 //  XSObjectFactory: factory methods
00081 // ---------------------------------------------------------------------------
00082 XSParticle*
00083 XSObjectFactory::createModelGroupParticle(const ContentSpecNode* const rootNode,
00084                                           XSModel* const xsModel)
00085 {
00086     if (rootNode == 0)
00087         return 0;
00088 
00089     ContentSpecNode::NodeTypes nodeType = rootNode->getType();
00090     if (nodeType == ContentSpecNode::All
00091         || nodeType == ContentSpecNode::ModelGroupChoice
00092         || nodeType == ContentSpecNode::ModelGroupSequence)
00093     {
00094         XSParticleList* particleList = new (fMemoryManager) RefVectorOf<XSParticle> (4, true, fMemoryManager);
00095         XSAnnotation* annot = getAnnotationFromModel(xsModel, rootNode);
00096         XSModelGroup* modelGroup = 0;
00097 
00098         if (nodeType == ContentSpecNode::All)
00099         {
00100             modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_ALL, particleList, annot, xsModel, fMemoryManager);
00101             buildAllParticles(rootNode, particleList, xsModel);
00102         }
00103         else
00104         {
00105             if (nodeType == ContentSpecNode::ModelGroupChoice)
00106                 modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_CHOICE, particleList, annot, xsModel, fMemoryManager);
00107             else
00108                 modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_SEQUENCE, particleList, annot, xsModel, fMemoryManager);
00109 
00110             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
00111             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
00112         }
00113 
00114         int m = rootNode->getMaxOccurs();
00115         XSParticle* groupParticle = new (fMemoryManager) XSParticle
00116         (
00117             XSParticle::TERM_MODELGROUP
00118             , xsModel
00119             , modelGroup
00120             , (XMLSize_t)rootNode->getMinOccurs()
00121             , (XMLSize_t)m
00122             , m == -1
00123             , fMemoryManager
00124         );
00125 
00126         return groupParticle;
00127     }
00128     else
00129         return 0;
00130 }
00131 
00132 void XSObjectFactory::buildAllParticles(const ContentSpecNode* const rootNode,
00133                                  XSParticleList* const particleList,
00134                                  XSModel* const xsModel)
00135 {
00136     // Get the type of spec node our current node is
00137     const ContentSpecNode::NodeTypes nodeType = rootNode->getType();
00138 
00139     if (nodeType == ContentSpecNode::All)
00140     {
00141         const ContentSpecNode* rightNode = rootNode->getSecond();
00142 
00143         buildAllParticles(rootNode->getFirst(), particleList, xsModel);
00144         if (rightNode)
00145             buildAllParticles(rightNode, particleList, xsModel);
00146     }
00147     else if (nodeType == ContentSpecNode::Leaf)
00148     {
00149         XSParticle* elemParticle = createElementParticle(rootNode, xsModel);
00150         if (elemParticle)
00151             particleList->addElement(elemParticle);
00152     }
00153 }
00154 
00155 void XSObjectFactory::buildChoiceSequenceParticles(const ContentSpecNode* const rootNode,
00156                                             XSParticleList* const particleList,
00157                                             XSModel* const xsModel)
00158 {
00159     if (rootNode)
00160     {
00161         const ContentSpecNode::NodeTypes nodeType = rootNode->getType();
00162 
00163         if (nodeType == ContentSpecNode::Sequence)
00164         {
00165             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
00166             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
00167         }
00168         else if (nodeType == ContentSpecNode::Choice)
00169         {
00170             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
00171             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
00172         }
00173         else if ((nodeType & 0x0f) == ContentSpecNode::Any
00174                  || (nodeType & 0x0f) == ContentSpecNode::Any_Other
00175                  || (nodeType & 0x0f) == ContentSpecNode::Any_NS
00176                  || nodeType == ContentSpecNode::Any_NS_Choice)
00177         {
00178             XSParticle* wildcardParticle = createWildcardParticle(rootNode, xsModel);
00179             if (wildcardParticle)
00180                 particleList->addElement(wildcardParticle);
00181         }
00182         else if (nodeType == ContentSpecNode::Leaf)
00183         {
00184             XSParticle* elemParticle = createElementParticle(rootNode, xsModel);
00185             if (elemParticle)
00186                 particleList->addElement(elemParticle);
00187         }
00188         // must be a model group
00189         else
00190         {
00191             XSParticle* xsParticle = createModelGroupParticle(rootNode, xsModel);
00192             if (xsParticle)
00193                 particleList->addElement(xsParticle);
00194         }
00195     }
00196 }
00197 
00198 XSParticle*
00199 XSObjectFactory::createElementParticle(const ContentSpecNode* const rootNode,
00200                                        XSModel* const xsModel)
00201 {
00202     if (rootNode->getElementDecl())
00203     {
00204         XSElementDeclaration* xsElemDecl = addOrFind(
00205             (SchemaElementDecl*) rootNode->getElementDecl(), xsModel);
00206 
00207         if (xsElemDecl)
00208         {
00209             int m = rootNode->getMaxOccurs();
00210             XSParticle* particle = new (fMemoryManager) XSParticle
00211             (
00212                 XSParticle::TERM_ELEMENT
00213                 , xsModel
00214                 , xsElemDecl
00215                 , (XMLSize_t)rootNode->getMinOccurs()
00216                 , (XMLSize_t)m
00217                 , m == -1
00218                 , fMemoryManager
00219             );
00220 
00221             return particle;
00222         }
00223     }
00224 
00225     return 0;
00226 }
00227 
00228 XSParticle*
00229 XSObjectFactory::createWildcardParticle(const ContentSpecNode* const rootNode,
00230                                         XSModel* const xsModel)
00231 {
00232     XSWildcard* xsWildcard = createXSWildcard(rootNode, xsModel);
00233     if (xsWildcard)
00234     {
00235         int m = rootNode->getMaxOccurs();
00236         XSParticle* particle = new (fMemoryManager) XSParticle
00237         (
00238             XSParticle::TERM_WILDCARD
00239             , xsModel
00240             , xsWildcard
00241             , (XMLSize_t)rootNode->getMinOccurs()
00242             , (XMLSize_t)m
00243             , m == -1
00244             , fMemoryManager
00245         );
00246 
00247         return particle;
00248     }
00249 
00250     return 0;
00251 }
00252 
00253 XSAttributeDeclaration*
00254 XSObjectFactory::addOrFind(SchemaAttDef* const attDef,
00255                            XSModel* const xsModel,
00256                            XSComplexTypeDefinition* const enclosingTypeDef)
00257 {
00258     XSAttributeDeclaration* xsObj = (XSAttributeDeclaration*) xsModel->getXSObject(attDef);
00259     if (xsObj)
00260     {
00261         if (xsObj->getScope() == XSConstants::SCOPE_LOCAL
00262             && xsObj->getEnclosingCTDefinition() == 0
00263             && enclosingTypeDef)
00264             xsObj->setEnclosingCTDefinition(enclosingTypeDef);
00265     }
00266     else
00267     {
00268         XSSimpleTypeDefinition* xsType = 0;
00269         if (attDef->getDatatypeValidator())
00270             xsType = addOrFind(attDef->getDatatypeValidator(), xsModel);
00271 
00272         XSConstants::SCOPE scope = XSConstants::SCOPE_ABSENT;
00273         XSComplexTypeDefinition* enclosingCTDefinition = 0;
00274 
00275         if (attDef->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
00276             scope = XSConstants::SCOPE_GLOBAL;
00277         else if (attDef->getPSVIScope() == PSVIDefs::SCP_LOCAL)
00278         {
00279             scope = XSConstants::SCOPE_LOCAL;
00280             enclosingCTDefinition = enclosingTypeDef;
00281         }
00282 
00283         xsObj = new (fMemoryManager) XSAttributeDeclaration
00284         (
00285             attDef
00286             , xsType
00287             , getAnnotationFromModel(xsModel, attDef)
00288             , xsModel
00289             , scope
00290             , enclosingCTDefinition
00291             , fMemoryManager
00292         );
00293         putObjectInMap(attDef, xsObj);
00294     }
00295 
00296     return xsObj;
00297 }
00298 
00299 XSSimpleTypeDefinition*
00300 XSObjectFactory::addOrFind(DatatypeValidator* const validator,
00301                            XSModel* const xsModel,
00302                            bool isAnySimpleType)
00303 {
00304     XSSimpleTypeDefinition* xsObj = (XSSimpleTypeDefinition*) xsModel->getXSObject(validator);
00305     if (!xsObj)
00306     {
00307         XSTypeDefinition* baseType = 0;
00308         XSSimpleTypeDefinitionList* memberTypes = 0;
00309         XSSimpleTypeDefinition* primitiveOrItemType = 0;
00310         XSSimpleTypeDefinition::VARIETY typeVariety = XSSimpleTypeDefinition::VARIETY_ATOMIC;
00311         bool primitiveTypeSelf = false;
00312 
00313         //REVISIT: the getFixed method is protected so added friend XSObjectFactory
00314         //         to DatatypeValidator class...
00315         DatatypeValidator::ValidatorType dvType = validator->getType();
00316         DatatypeValidator* baseDV = validator->getBaseValidator();
00317 
00318         if (dvType == DatatypeValidator::Union)
00319         {
00320             typeVariety = XSSimpleTypeDefinition::VARIETY_UNION;
00321             RefVectorOf<DatatypeValidator>* membersDV = ((UnionDatatypeValidator*)validator)->getMemberTypeValidators();
00322             XMLSize_t size = membersDV->size();
00323             if (size)
00324             {
00325                 memberTypes = new (fMemoryManager) RefVectorOf<XSSimpleTypeDefinition>(size, false, fMemoryManager);
00326                 for (XMLSize_t i=0; i<size; i++)
00327                     memberTypes->addElement(addOrFind(membersDV->elementAt(i), xsModel));
00328             }
00329 
00330             if (baseDV)
00331             {
00332                 baseType = addOrFind(baseDV, xsModel);
00333             }
00334             else
00335             {
00336                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
00337                 (
00338                     SchemaSymbols::fgDT_ANYSIMPLETYPE
00339                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
00340                 );
00341             }
00342         }
00343         else if (dvType == DatatypeValidator::List)
00344         {
00345             typeVariety = XSSimpleTypeDefinition::VARIETY_LIST;
00346             if (baseDV->getType() == DatatypeValidator::List)
00347             {
00348                 baseType = addOrFind(baseDV, xsModel);
00349                 primitiveOrItemType = ((XSSimpleTypeDefinition*) baseType)->getItemType();
00350             }
00351             else
00352             {
00353                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
00354                 (
00355                     SchemaSymbols::fgDT_ANYSIMPLETYPE
00356                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
00357                 );
00358                 primitiveOrItemType = addOrFind(baseDV, xsModel);
00359             }
00360         }
00361         else if (!isAnySimpleType)
00362         {
00363             if (baseDV)
00364             {
00365                 baseType = addOrFind(baseDV, xsModel);
00366                 primitiveOrItemType = ((XSSimpleTypeDefinition*) baseType)->getPrimitiveType();
00367             }
00368             else // built-in
00369             {
00370                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
00371                 (
00372                     SchemaSymbols::fgDT_ANYSIMPLETYPE
00373                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
00374                 );
00375                 primitiveTypeSelf = true;
00376             }
00377         }
00378         else
00379         {
00380             baseType = xsModel->getTypeDefinition(SchemaSymbols::fgATTVAL_ANYTYPE, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
00381         }
00382 
00383         xsObj = new (fMemoryManager) XSSimpleTypeDefinition
00384         (
00385             validator
00386             , typeVariety
00387             , baseType
00388             , primitiveOrItemType
00389             , memberTypes
00390             , getAnnotationFromModel(xsModel, validator)
00391             , xsModel
00392             , fMemoryManager
00393         );
00394         putObjectInMap(validator, xsObj);
00395 
00396         if (primitiveTypeSelf)
00397             xsObj->setPrimitiveType(xsObj);
00398 
00399         // process facets
00400         processFacets(validator, xsModel, xsObj);
00401     }
00402 
00403     return xsObj;
00404 }
00405 
00406 XSElementDeclaration*
00407 XSObjectFactory::addOrFind(SchemaElementDecl* const elemDecl,
00408                            XSModel* const xsModel,
00409                            XSComplexTypeDefinition* const enclosingTypeDef)
00410 {
00411     XSElementDeclaration* xsObj = (XSElementDeclaration*) xsModel->getXSObject(elemDecl);
00412     if (xsObj)
00413     {
00414         if (!xsObj->getEnclosingCTDefinition() && enclosingTypeDef)
00415             xsObj->setEnclosingCTDefinition(enclosingTypeDef);
00416     }
00417     else
00418     {
00419         XSElementDeclaration*        xsSubElem = 0;
00420         XSTypeDefinition*            xsType = 0;
00421         XSNamedMap<XSIDCDefinition>* icMap = 0;
00422 
00423         if (elemDecl->getSubstitutionGroupElem())
00424             xsSubElem = addOrFind(elemDecl->getSubstitutionGroupElem(), xsModel);
00425 
00426         // defer checking for complexTypeInfo until later as it could
00427         // eventually need this elemement
00428         // but don't check simple type unless no complexTypeInfo present
00429         if (!elemDecl->getComplexTypeInfo() && elemDecl->getDatatypeValidator())
00430             xsType = addOrFind(elemDecl->getDatatypeValidator(), xsModel);
00431 
00432         XMLSize_t count = elemDecl->getIdentityConstraintCount();
00433         if (count)
00434         {
00435             //REVISIT: size of hash table....
00436             icMap = new (fMemoryManager) XSNamedMap<XSIDCDefinition>
00437             (
00438                 count
00439                 , 29
00440                 , xsModel->getURIStringPool()
00441                 , false
00442                 , fMemoryManager
00443             );
00444 
00445             for (XMLSize_t i = 0; i < count; i++)
00446             {
00447                 XSIDCDefinition* icDef = addOrFind
00448                 (
00449                     elemDecl->getIdentityConstraintAt(i)
00450                     , xsModel
00451                 );
00452                 if (icDef)
00453                 {
00454                     icMap->addElement
00455                     (
00456                         icDef
00457                         , icDef->getName()
00458                         , icDef->getNamespace()
00459                     );
00460                 }
00461             }
00462         }
00463 
00464         XSConstants::SCOPE elemScope = XSConstants::SCOPE_ABSENT;
00465         if (elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
00466             elemScope = XSConstants::SCOPE_LOCAL;
00467         else if (elemDecl->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
00468             elemScope = XSConstants::SCOPE_GLOBAL;
00469 
00470         xsObj = new (fMemoryManager) XSElementDeclaration
00471         (
00472             elemDecl
00473             , xsType
00474             , xsSubElem
00475             , getAnnotationFromModel(xsModel, elemDecl)
00476             , icMap
00477             , xsModel
00478             , elemScope
00479             , enclosingTypeDef
00480             , fMemoryManager
00481         );
00482         putObjectInMap(elemDecl, xsObj);
00483 
00484         if (elemDecl->getComplexTypeInfo())
00485         {
00486             xsType = addOrFind(elemDecl->getComplexTypeInfo(), xsModel);
00487             xsObj->setTypeDefinition(xsType);
00488         }
00489         else if (!xsType)
00490         {
00491             xsType = xsModel->getTypeDefinition
00492             (
00493                 SchemaSymbols::fgATTVAL_ANYTYPE
00494                 , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
00495             );
00496             xsObj->setTypeDefinition(xsType);
00497         }
00498     }
00499 
00500     return xsObj;
00501 }
00502 
00503 XSComplexTypeDefinition*
00504 XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
00505                            XSModel* const xsModel)
00506 {
00507     XSComplexTypeDefinition* xsObj = (XSComplexTypeDefinition*) xsModel->getXSObject(typeInfo);
00508     if (!xsObj)
00509     {
00510         XSWildcard*             xsWildcard = 0;
00511         XSSimpleTypeDefinition* xsSimpleType = 0;
00512         XSAttributeUseList*     xsAttList = 0;
00513         XSTypeDefinition*       xsBaseType = 0;
00514         XSParticle*             xsParticle = 0;
00515 
00516         if (typeInfo->getAttWildCard())
00517             xsWildcard = createXSWildcard(typeInfo->getAttWildCard(), xsModel);
00518 
00519         if ((typeInfo->getContentType() == SchemaElementDecl::Simple) &&
00520             (typeInfo->getDatatypeValidator()))
00521             xsSimpleType = addOrFind(typeInfo->getDatatypeValidator(), xsModel);
00522 
00523         XMLSize_t attCount=0;
00524         if (typeInfo->hasAttDefs())
00525         {
00526             SchemaAttDefList& attDefList = (SchemaAttDefList&) typeInfo->getAttDefList();
00527             attCount = attDefList.getAttDefCount();
00528             xsAttList = new (fMemoryManager) RefVectorOf<XSAttributeUse>(attCount, false, fMemoryManager);
00529             // create list now put fill it in after we put complextype into map
00530             // otherwise we may encounter an infinite loop: complextype needs to
00531             // addorfind attdef, which does an addorfind on the enclosingCTdefintion.
00532         }
00533 
00534         // compute fBase
00535         bool isAnyType = false;
00536         if (typeInfo->getBaseComplexTypeInfo() == typeInfo) // case of anyType
00537             isAnyType = true;
00538         else if (typeInfo->getBaseComplexTypeInfo())
00539             xsBaseType = addOrFind(typeInfo->getBaseComplexTypeInfo(), xsModel);
00540         else if (typeInfo->getBaseDatatypeValidator())
00541             xsBaseType = addOrFind(typeInfo->getBaseDatatypeValidator(), xsModel);
00542         else // base is anyType
00543             xsBaseType = xsModel->getTypeDefinition(SchemaSymbols::fgATTVAL_ANYTYPE, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
00544 
00545         // compute particle
00546         ContentSpecNode* contentSpec = typeInfo->getContentSpec();
00547         if (contentSpec)
00548             xsParticle = createModelGroupParticle(contentSpec, xsModel);
00549 
00550         xsObj = new (fMemoryManager) XSComplexTypeDefinition
00551         (
00552             typeInfo
00553             , xsWildcard
00554             , xsSimpleType
00555             , xsAttList
00556             , xsBaseType
00557             , xsParticle
00558             , getAnnotationFromModel(xsModel, typeInfo)
00559             , xsModel
00560             , fMemoryManager
00561         );
00562         putObjectInMap(typeInfo, xsObj);
00563 
00564         if (isAnyType)
00565             xsObj->setBaseType(xsObj);
00566 
00567         if (typeInfo->hasAttDefs())
00568         {
00569             // now create the xsattributedeclarations...
00570             SchemaAttDefList& attDefList = (SchemaAttDefList&) typeInfo->getAttDefList();
00571             for(unsigned int i=0; i<attCount; i++)
00572             {
00573                 XSAttributeDeclaration* xsAttDecl = 0;
00574                 SchemaAttDef& attDef = (SchemaAttDef&) attDefList.getAttDef(i);
00575 
00576                 if (attDef.getBaseAttDecl())
00577                 {
00578                     xsAttDecl = addOrFind(attDef.getBaseAttDecl(), xsModel);
00579                     fXercesToXSMap->put(&attDef, xsAttDecl);
00580                 }
00581                 else
00582                     xsAttDecl = addOrFind(&attDef, xsModel, xsObj);
00583 
00584                 if (attDef.getDefaultType() != XMLAttDef::Prohibited) {
00585 
00586                     XSAttributeUse* attUse = createXSAttributeUse(xsAttDecl, xsModel);
00587                     xsAttList->addElement(attUse);
00588                     processAttUse(&attDef, attUse);
00589                 }
00590             }
00591         }
00592 
00593         // process local elements
00594         XMLSize_t elemCount = typeInfo->elementCount();
00595         for (XMLSize_t j=0; j<elemCount; j++)
00596         {
00597             SchemaElementDecl* elemDecl = typeInfo->elementAt(j);
00598 
00599             if (elemDecl->getEnclosingScope() == typeInfo->getScopeDefined()
00600                 && elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
00601                 addOrFind(elemDecl, xsModel, xsObj);
00602         }
00603     }
00604 
00605     return xsObj;
00606 }
00607 
00608 XSIDCDefinition* XSObjectFactory::addOrFind(IdentityConstraint* const ic,
00609                                             XSModel* const xsModel)
00610 {
00611     XSIDCDefinition* xsObj = (XSIDCDefinition*) xsModel->getXSObject(ic);
00612     if (!xsObj)
00613     {
00614         XSIDCDefinition* keyIC = 0;
00615         StringList*      stringList = 0;
00616         XMLSize_t        fieldCount = ic->getFieldCount();
00617 
00618         if (fieldCount)
00619         {
00620             stringList = new (fMemoryManager) RefArrayVectorOf<XMLCh>(
00621                 fieldCount, true, fMemoryManager);
00622 
00623             for(XMLSize_t i=0; i<fieldCount; i++)
00624             {
00625                 XMLCh* expr = XMLString::replicate
00626                 (
00627                     ic->getFieldAt(i)->getXPath()->getExpression()
00628                     , fMemoryManager
00629                 );
00630                 stringList->addElement(expr);
00631             }
00632         }
00633 
00634         if (ic->getType() == IdentityConstraint::ICType_KEYREF)
00635             keyIC = addOrFind(((IC_KeyRef*) ic)->getKey(), xsModel);
00636 
00637         xsObj= new (fMemoryManager) XSIDCDefinition
00638         (
00639             ic
00640             , keyIC
00641             , getAnnotationFromModel(xsModel, ic)
00642             , stringList
00643             , xsModel
00644             , fMemoryManager
00645         );
00646         putObjectInMap(ic, xsObj);
00647     }
00648 
00649     return xsObj;
00650 }
00651 
00652 XSNotationDeclaration* XSObjectFactory::addOrFind(XMLNotationDecl* const notDecl,
00653                                                   XSModel* const xsModel)
00654 {
00655     XSNotationDeclaration* xsObj = (XSNotationDeclaration*) xsModel->getXSObject(notDecl);
00656     if (!xsObj)
00657     {
00658         xsObj = new (fMemoryManager) XSNotationDeclaration
00659         (
00660             notDecl
00661             , getAnnotationFromModel(xsModel, notDecl)
00662             , xsModel
00663             , fMemoryManager
00664         );
00665         putObjectInMap(notDecl, xsObj);
00666     }
00667 
00668     return xsObj;
00669 }
00670 
00671 XSAttributeUse*
00672 XSObjectFactory::createXSAttributeUse(XSAttributeDeclaration* const xsAttDecl,
00673                                XSModel* const xsModel)
00674 {
00675     XSAttributeUse* attrUse = new (fMemoryManager) XSAttributeUse(xsAttDecl, xsModel, fMemoryManager);
00676     fDeleteVector->addElement(attrUse);
00677 
00678     return attrUse;
00679 }
00680 
00681 XSWildcard*
00682 XSObjectFactory::createXSWildcard(SchemaAttDef* const attDef,
00683                                   XSModel* const xsModel)
00684 {
00685     XSAnnotation* annot = (attDef->getBaseAttDecl())
00686         ? getAnnotationFromModel(xsModel, attDef->getBaseAttDecl())
00687         : getAnnotationFromModel(xsModel, attDef);
00688 
00689     XSWildcard* xsWildcard = new (fMemoryManager) XSWildcard
00690     (
00691         attDef
00692         , annot
00693         , xsModel
00694         , fMemoryManager
00695     );
00696     fDeleteVector->addElement(xsWildcard);
00697 
00698     return xsWildcard;
00699 }
00700 
00701 XSWildcard*
00702 XSObjectFactory::createXSWildcard(const ContentSpecNode* const rootNode,
00703                                   XSModel* const xsModel)
00704 {
00705     XSWildcard* xsWildcard = new (fMemoryManager) XSWildcard
00706     (
00707         rootNode
00708         , getAnnotationFromModel(xsModel, rootNode)
00709         , xsModel
00710         , fMemoryManager
00711     );
00712     fDeleteVector->addElement(xsWildcard);
00713 
00714     return xsWildcard;
00715 }
00716 
00717 XSModelGroupDefinition*
00718 XSObjectFactory::createXSModelGroupDefinition(XercesGroupInfo* const groupInfo,
00719                                               XSModel* const xsModel)
00720 {
00721     XSParticle* particle = createModelGroupParticle(
00722             groupInfo->getContentSpec(), xsModel);
00723 
00724     XSModelGroupDefinition* xsObj = new (fMemoryManager) XSModelGroupDefinition
00725     (
00726         groupInfo
00727         , particle
00728         , getAnnotationFromModel(xsModel, groupInfo)
00729         , xsModel
00730         , fMemoryManager
00731     );
00732     fDeleteVector->addElement(xsObj);
00733 
00734     // process local elements
00735     XMLSize_t elemCount = groupInfo->elementCount();
00736     for (XMLSize_t j=0; j<elemCount; j++)
00737     {
00738         SchemaElementDecl* elemDecl = groupInfo->elementAt(j);
00739 
00740         if (elemDecl->getEnclosingScope() == groupInfo->getScope())
00741             addOrFind(elemDecl, xsModel);
00742     }
00743 
00744     return xsObj;
00745 }
00746 
00747 
00748 XSAttributeGroupDefinition*
00749 XSObjectFactory::createXSAttGroupDefinition(XercesAttGroupInfo* const attGroupInfo,
00750                                             XSModel* const xsModel)
00751 {
00752     XSAttributeUseList* xsAttList = 0;
00753     XSWildcard*         xsWildcard = 0;
00754     XMLSize_t           attCount = attGroupInfo->attributeCount();
00755 
00756     if (attCount)
00757     {
00758         xsAttList = new (fMemoryManager) RefVectorOf<XSAttributeUse>(attCount, false, fMemoryManager);
00759         for (XMLSize_t i=0; i < attCount; i++)
00760         {
00761             SchemaAttDef* attDef = attGroupInfo->attributeAt(i);
00762             XSAttributeDeclaration* xsAttDecl = 0;
00763 
00764             if (attDef->getBaseAttDecl())
00765                 xsAttDecl = addOrFind(attDef->getBaseAttDecl(), xsModel);
00766             else
00767                 xsAttDecl = addOrFind(attDef, xsModel);
00768 
00769             if (xsAttDecl && (attDef->getDefaultType() != XMLAttDef::Prohibited)) // just for sanity
00770             {
00771                 XSAttributeUse* attUse = createXSAttributeUse(xsAttDecl, xsModel);
00772                 xsAttList->addElement(attUse);
00773                 processAttUse(attDef, attUse);
00774             }
00775         }
00776     }
00777 
00778     if (attGroupInfo->getCompleteWildCard())
00779         xsWildcard = createXSWildcard(attGroupInfo->getCompleteWildCard(), xsModel);
00780 
00781     XSAttributeGroupDefinition* xsObj = new (fMemoryManager) XSAttributeGroupDefinition
00782     (
00783         attGroupInfo
00784         , xsAttList
00785         , xsWildcard
00786         , getAnnotationFromModel(xsModel, attGroupInfo)
00787         , xsModel
00788         , fMemoryManager
00789     );
00790     fDeleteVector->addElement(xsObj);
00791 
00792     return xsObj;
00793 }
00794 
00795 XSAnnotation* XSObjectFactory::getAnnotationFromModel(XSModel* const xsModel,
00796                                                const void* const key)
00797 {
00798     XSNamespaceItemList* namespaceItemList = xsModel->getNamespaceItems();
00799 
00800     XSAnnotation* annot = 0;
00801     for (unsigned int i=0; i<namespaceItemList->size(); i++)
00802     {
00803         XSNamespaceItem* nsItem = namespaceItemList->elementAt(i);
00804         if (nsItem->fGrammar)
00805         {
00806             annot = nsItem->fGrammar->getAnnotation(key);
00807             if (annot)
00808                 return annot;
00809         }
00810     }
00811 
00812     if (xsModel->fParent)
00813         return getAnnotationFromModel(xsModel->fParent, key);
00814     return 0;
00815 }
00816 
00817 
00818 void XSObjectFactory::putObjectInMap(void* key, XSObject* const object)
00819 {
00820      fXercesToXSMap->put(key, object);
00821      fDeleteVector->addElement(object);
00822 }
00823 
00824 void XSObjectFactory::processFacets(DatatypeValidator* const dv,
00825                                     XSModel* const xsModel,
00826                                     XSSimpleTypeDefinition* const xsST)
00827 {
00828     // NOTE: XSMultiValueFacetList is not owned by XSModel!
00829     // NOTE: XSFacetList is not owned by XSModel!
00830     bool isFixed = false;
00831     int dvFacetsDefined = dv->getFacetsDefined();
00832     int dvFixedFacets = dv->getFixed();
00833     int definedFacets = 0;
00834     int fixedFacets = 0;
00835     XSMultiValueFacetList* xsMultiFacetList = 0;
00836     StringList* patternList = 0;
00837     XSFacetList* xsFacetList = new (fMemoryManager) RefVectorOf<XSFacet>(4, false, fMemoryManager);
00838 
00839     if (isMultiValueFacetDefined(dv))
00840         xsMultiFacetList = new (fMemoryManager) RefVectorOf<XSMultiValueFacet>(2, false, fMemoryManager);
00841 
00842     if (dvFacetsDefined & DatatypeValidator::FACET_ENUMERATION)
00843     {
00844         RefArrayVectorOf<XMLCh>* enumList = (RefArrayVectorOf<XMLCh>*) dv->getEnumString();
00845 
00846         if (dvFixedFacets & DatatypeValidator::FACET_ENUMERATION)
00847         {
00848             isFixed = true;
00849             fixedFacets |= XSSimpleTypeDefinition::FACET_ENUMERATION;
00850         }
00851 
00852         XSMultiValueFacet* mvFacet = new (fMemoryManager) XSMultiValueFacet(
00853             XSSimpleTypeDefinition::FACET_ENUMERATION , enumList, isFixed
00854             , getAnnotationFromModel(xsModel, enumList), xsModel, fMemoryManager
00855         );
00856 
00857         fDeleteVector->addElement(mvFacet);
00858         xsMultiFacetList->addElement(mvFacet);
00859         definedFacets |= XSSimpleTypeDefinition::FACET_ENUMERATION;
00860     }
00861 
00862     if (dv->getFacets())
00863     {
00864         RefHashTableOfEnumerator<KVStringPair> e(dv->getFacets(), false, fMemoryManager);
00865         while (e.hasMoreElements())
00866         {
00867             KVStringPair& pair = e.nextElement();
00868             XMLCh* key = pair.getKey();
00869             XSSimpleTypeDefinition::FACET facetType;
00870             XSAnnotation* annot = getAnnotationFromModel(xsModel, &pair);
00871 
00872             if (XMLString::equals(key, SchemaSymbols::fgELT_MAXINCLUSIVE))
00873             {
00874                 facetType = XSSimpleTypeDefinition::FACET_MAXINCLUSIVE;
00875                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXINCLUSIVE) != 0);
00876             }
00877             else if (XMLString::equals(key, SchemaSymbols::fgELT_MAXEXCLUSIVE))
00878             {
00879                 facetType = XSSimpleTypeDefinition::FACET_MAXEXCLUSIVE;
00880                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXEXCLUSIVE) !=0);
00881             }
00882             else if (XMLString::equals(key, SchemaSymbols::fgELT_MININCLUSIVE))
00883             {
00884                 facetType = XSSimpleTypeDefinition::FACET_MININCLUSIVE;
00885                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MININCLUSIVE) !=0);
00886             }
00887             else if (XMLString::equals(key, SchemaSymbols::fgELT_MINEXCLUSIVE))
00888             {
00889                 facetType = XSSimpleTypeDefinition::FACET_MINEXCLUSIVE;
00890                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MINEXCLUSIVE) != 0);
00891             }
00892             else if (XMLString::equals(key, SchemaSymbols::fgELT_LENGTH))
00893             {
00894                 facetType = XSSimpleTypeDefinition::FACET_LENGTH;
00895                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_LENGTH) != 0);
00896             }
00897             else if (XMLString::equals(key, SchemaSymbols::fgELT_MINLENGTH))
00898             {
00899                 facetType = XSSimpleTypeDefinition::FACET_MINLENGTH;
00900                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MINLENGTH) != 0);
00901             }
00902             else if (XMLString::equals(key, SchemaSymbols::fgELT_MAXLENGTH))
00903             {
00904                 facetType = XSSimpleTypeDefinition::FACET_MAXLENGTH;
00905                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXLENGTH) != 0);
00906             }
00907             else if (XMLString::equals(key, SchemaSymbols::fgELT_TOTALDIGITS))
00908             {
00909                 facetType = XSSimpleTypeDefinition::FACET_TOTALDIGITS;
00910                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_TOTALDIGITS) != 0);
00911             }
00912             else if (XMLString::equals(key, SchemaSymbols::fgELT_FRACTIONDIGITS))
00913             {
00914                 facetType = XSSimpleTypeDefinition::FACET_FRACTIONDIGITS;
00915                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_FRACTIONDIGITS) != 0);
00916             }
00917             else if (XMLString::equals(key, SchemaSymbols::fgELT_WHITESPACE))
00918             {
00919                 facetType = XSSimpleTypeDefinition::FACET_WHITESPACE;
00920                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_WHITESPACE) != 0);
00921             }
00922             else if (XMLString::equals(key, SchemaSymbols::fgELT_PATTERN))
00923             {
00924                 XMLStringTokenizer tokenizer(dv->getPattern(), regexSeparator, fMemoryManager);
00925                 patternList = new (fMemoryManager) RefArrayVectorOf<XMLCh>(
00926                     tokenizer.countTokens(), true, fMemoryManager
00927                 );
00928 
00929                 while (tokenizer.hasMoreTokens())
00930                     patternList->addElement(XMLString::replicate(tokenizer.nextToken(), fMemoryManager));
00931 
00932                 if (dvFixedFacets & DatatypeValidator::FACET_PATTERN)
00933                 {
00934                     isFixed = true;
00935                     fixedFacets |= XSSimpleTypeDefinition::FACET_PATTERN;
00936                 }
00937 
00938                 XSMultiValueFacet* mvFacet = new (fMemoryManager) XSMultiValueFacet(
00939                     XSSimpleTypeDefinition::FACET_PATTERN, patternList
00940                     , isFixed, annot, xsModel, fMemoryManager
00941                 );
00942                 fDeleteVector->addElement(mvFacet);
00943                 xsMultiFacetList->addElement(mvFacet);
00944                 definedFacets |= XSSimpleTypeDefinition::FACET_PATTERN;
00945                 continue;
00946             }
00947             else
00948             {
00949                 // REVISIT: hmm... what about XSSimpleTypeDefinition::FACET_NONE
00950                 // don't think I need to create an empty Facet?
00951                 continue;
00952             }
00953 
00954             XSFacet* xsFacet = new (fMemoryManager) XSFacet(
00955                 facetType, pair.getValue(), isFixed, annot, xsModel, fMemoryManager);
00956 
00957             fDeleteVector->addElement(xsFacet);
00958             xsFacetList->addElement(xsFacet);
00959             definedFacets |= facetType;
00960             if (isFixed)
00961                 fixedFacets |= facetType;
00962         }
00963     }
00964 
00965     // add whistespace facet if missing
00966     if ((definedFacets & XSSimpleTypeDefinition::FACET_WHITESPACE) == 0)
00967     {
00968         XSFacet* xsFacet = new (fMemoryManager) XSFacet(
00969             XSSimpleTypeDefinition::FACET_WHITESPACE
00970             , dv->getWSstring(dv->getWSFacet())
00971             , false, 0, xsModel, fMemoryManager);
00972 
00973         fDeleteVector->addElement(xsFacet);
00974         xsFacetList->addElement(xsFacet);
00975         definedFacets |= XSSimpleTypeDefinition::FACET_WHITESPACE;
00976     }
00977 
00978     // inherit facets from base
00979 
00980     if (xsST->getBaseType() && xsST->getBaseType()->getTypeCategory() == XSTypeDefinition::SIMPLE_TYPE)
00981     {
00982         XSSimpleTypeDefinition* baseST = (XSSimpleTypeDefinition*) xsST->getBaseType();
00983         XSFacetList* baseFacets = baseST->getFacets();
00984 
00985         for (unsigned int i=0; i<baseFacets->size(); i++)
00986         {
00987             XSFacet* bFacet = baseFacets->elementAt(i);
00988             if ((definedFacets & bFacet->getFacetKind()) == 0)
00989             {
00990                 definedFacets |= bFacet->getFacetKind();
00991                 xsFacetList->addElement(bFacet);
00992                 if (bFacet->isFixed())
00993                     fixedFacets |= bFacet->getFacetKind();
00994             }
00995         }
00996 
00997         if (baseST->getMultiValueFacets())
00998         {
00999             XSMultiValueFacetList* baseMVFacets = baseST->getMultiValueFacets();
01000             for (unsigned int j=0; j<baseMVFacets->size(); j++)
01001             {
01002                 XSMultiValueFacet* bFacet = baseMVFacets->elementAt(j);
01003                 if ((definedFacets & bFacet->getFacetKind()) == 0)
01004                 {
01005                     definedFacets |= bFacet->getFacetKind();
01006                     xsMultiFacetList->addElement(bFacet);
01007                     if (bFacet->isFixed())
01008                         fixedFacets |= bFacet->getFacetKind();
01009                 }
01010             }
01011         }
01012     }
01013 
01014     xsST->setFacetInfo(definedFacets, fixedFacets, xsFacetList, xsMultiFacetList, patternList);
01015 }
01016 
01017 void XSObjectFactory::processAttUse(SchemaAttDef* const attDef,
01018                                     XSAttributeUse* const xsAttUse)
01019 {
01020     bool isRequired = false;
01021     XSConstants::VALUE_CONSTRAINT constraintType = XSConstants::VALUE_CONSTRAINT_NONE;
01022 
01023     if (attDef->getDefaultType() == XMLAttDef::Default)
01024     {
01025         constraintType = XSConstants::VALUE_CONSTRAINT_DEFAULT;
01026     }
01027     else if ((attDef->getDefaultType() == XMLAttDef::Fixed) ||
01028              (attDef->getDefaultType() == XMLAttDef::Required_And_Fixed))
01029     {
01030         constraintType = XSConstants::VALUE_CONSTRAINT_FIXED;
01031     }
01032 
01033     if (attDef->getDefaultType() == XMLAttDef::Required ||
01034         attDef->getDefaultType() == XMLAttDef::Required_And_Fixed)
01035         isRequired = true;
01036 
01037     xsAttUse->set(isRequired, constraintType, attDef->getValue());
01038 }
01039 
01040 bool XSObjectFactory::isMultiValueFacetDefined(DatatypeValidator* const dv)
01041 {
01042     DatatypeValidator* tmpDV = dv;
01043 
01044     while (tmpDV)
01045     {
01046         if ((tmpDV->getFacetsDefined() & DatatypeValidator::FACET_PATTERN)
01047         ||  (tmpDV->getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION))
01048             return true;
01049 
01050         tmpDV = tmpDV->getBaseValidator();
01051     }
01052 
01053     return false;
01054 }
01055 
01056 XERCES_CPP_NAMESPACE_END