1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id $
20  */
21 
22 
23 // ---------------------------------------------------------------------------
24 //  Includes
25 // ---------------------------------------------------------------------------
26 #include <xercesc/internal/XSObjectFactory.hpp>
27 #include <xercesc/framework/psvi/XSModel.hpp>
28 #include <xercesc/framework/psvi/XSParticle.hpp>
29 #include <xercesc/framework/psvi/XSModelGroup.hpp>
30 #include <xercesc/framework/psvi/XSElementDeclaration.hpp>
31 #include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
32 #include <xercesc/framework/psvi/XSSimpleTypeDefinition.hpp>
33 #include <xercesc/framework/psvi/XSModelGroupDefinition.hpp>
34 #include <xercesc/framework/psvi/XSAttributeGroupDefinition.hpp>
35 #include <xercesc/framework/psvi/XSWildcard.hpp>
36 #include <xercesc/framework/psvi/XSNamespaceItem.hpp>
37 #include <xercesc/framework/psvi/XSIDCDefinition.hpp>
38 #include <xercesc/framework/psvi/XSAttributeUse.hpp>
39 #include <xercesc/framework/psvi/XSAttributeDeclaration.hpp>
40 #include <xercesc/framework/psvi/XSNotationDeclaration.hpp>
41 #include <xercesc/framework/psvi/XSFacet.hpp>
42 #include <xercesc/framework/psvi/XSMultiValueFacet.hpp>
43 #include <xercesc/framework/psvi/XSAnnotation.hpp>
44 #include <xercesc/validators/common/ContentSpecNode.hpp>
45 #include <xercesc/validators/datatype/DatatypeValidator.hpp>
46 #include <xercesc/validators/schema/SchemaAttDefList.hpp>
47 #include <xercesc/validators/schema/SchemaGrammar.hpp>
48 #include <xercesc/validators/schema/XercesGroupInfo.hpp>
49 #include <xercesc/validators/schema/XercesAttGroupInfo.hpp>
50 #include <xercesc/validators/schema/identity/IdentityConstraint.hpp>
51 #include <xercesc/validators/schema/identity/IC_KeyRef.hpp>
52 #include <xercesc/validators/schema/identity/XercesXPath.hpp>
53 #include <xercesc/util/XMLStringTokenizer.hpp>
54 
55 XERCES_CPP_NAMESPACE_BEGIN
56 
57 static XMLCh regexSeparator[] = {chPipe, chNull};
58 
59 
60 // ---------------------------------------------------------------------------
61 //  XSObjectFactory: Constructors and Destructor
62 // ---------------------------------------------------------------------------
XSObjectFactory(MemoryManager * const manager)63 XSObjectFactory::XSObjectFactory(MemoryManager* const manager)
64     : fMemoryManager(manager)
65     , fXercesToXSMap(0)
66     , fDeleteVector(0)
67 {
68     fDeleteVector = new (manager) RefVectorOf<XSObject>(20, true, manager);
69     fXercesToXSMap = new (manager) RefHashTableOf<XSObject, PtrHasher> (
70         109, false, manager);
71 }
72 
~XSObjectFactory()73 XSObjectFactory::~XSObjectFactory()
74 {
75     delete fXercesToXSMap;
76     delete fDeleteVector;
77 }
78 
79 // ---------------------------------------------------------------------------
80 //  XSObjectFactory: factory methods
81 // ---------------------------------------------------------------------------
82 XSParticle*
createModelGroupParticle(const ContentSpecNode * const rootNode,XSModel * const xsModel)83 XSObjectFactory::createModelGroupParticle(const ContentSpecNode* const rootNode,
84                                           XSModel* const xsModel)
85 {
86     if (rootNode == 0)
87         return 0;
88 
89     ContentSpecNode::NodeTypes nodeType = rootNode->getType();
90     if (nodeType == ContentSpecNode::All
91         || nodeType == ContentSpecNode::ModelGroupChoice
92         || nodeType == ContentSpecNode::ModelGroupSequence)
93     {
94         XSParticleList* particleList = new (fMemoryManager) RefVectorOf<XSParticle> (4, true, fMemoryManager);
95         XSAnnotation* annot = getAnnotationFromModel(xsModel, rootNode);
96         XSModelGroup* modelGroup = 0;
97 
98         if (nodeType == ContentSpecNode::All)
99         {
100             modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_ALL, particleList, annot, xsModel, fMemoryManager);
101             buildAllParticles(rootNode, particleList, xsModel);
102         }
103         else
104         {
105             if (nodeType == ContentSpecNode::ModelGroupChoice)
106                 modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_CHOICE, particleList, annot, xsModel, fMemoryManager);
107             else
108                 modelGroup = new (fMemoryManager) XSModelGroup(XSModelGroup::COMPOSITOR_SEQUENCE, particleList, annot, xsModel, fMemoryManager);
109 
110             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
111             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
112         }
113 
114         int m = rootNode->getMaxOccurs();
115         XSParticle* groupParticle = new (fMemoryManager) XSParticle
116         (
117             XSParticle::TERM_MODELGROUP
118             , xsModel
119             , modelGroup
120             , (XMLSize_t)rootNode->getMinOccurs()
121             , (XMLSize_t)m
122             , m == -1
123             , fMemoryManager
124         );
125 
126         return groupParticle;
127     }
128     else
129         return 0;
130 }
131 
buildAllParticles(const ContentSpecNode * const rootNode,XSParticleList * const particleList,XSModel * const xsModel)132 void XSObjectFactory::buildAllParticles(const ContentSpecNode* const rootNode,
133                                  XSParticleList* const particleList,
134                                  XSModel* const xsModel)
135 {
136     // Get the type of spec node our current node is
137     const ContentSpecNode::NodeTypes nodeType = rootNode->getType();
138 
139     if (nodeType == ContentSpecNode::All)
140     {
141         const ContentSpecNode* rightNode = rootNode->getSecond();
142 
143         buildAllParticles(rootNode->getFirst(), particleList, xsModel);
144         if (rightNode)
145             buildAllParticles(rightNode, particleList, xsModel);
146     }
147     else if (nodeType == ContentSpecNode::Leaf)
148     {
149         XSParticle* elemParticle = createElementParticle(rootNode, xsModel);
150         if (elemParticle)
151             particleList->addElement(elemParticle);
152     }
153 }
154 
buildChoiceSequenceParticles(const ContentSpecNode * const rootNode,XSParticleList * const particleList,XSModel * const xsModel)155 void XSObjectFactory::buildChoiceSequenceParticles(const ContentSpecNode* const rootNode,
156                                             XSParticleList* const particleList,
157                                             XSModel* const xsModel)
158 {
159     if (rootNode)
160     {
161         const ContentSpecNode::NodeTypes nodeType = rootNode->getType();
162 
163         if (nodeType == ContentSpecNode::Sequence)
164         {
165             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
166             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
167         }
168         else if (nodeType == ContentSpecNode::Choice)
169         {
170             buildChoiceSequenceParticles(rootNode->getFirst(), particleList, xsModel);
171             buildChoiceSequenceParticles(rootNode->getSecond(), particleList, xsModel);
172         }
173         else if ((nodeType & 0x0f) == ContentSpecNode::Any
174                  || (nodeType & 0x0f) == ContentSpecNode::Any_Other
175                  || (nodeType & 0x0f) == ContentSpecNode::Any_NS
176                  || nodeType == ContentSpecNode::Any_NS_Choice)
177         {
178             XSParticle* wildcardParticle = createWildcardParticle(rootNode, xsModel);
179             if (wildcardParticle)
180                 particleList->addElement(wildcardParticle);
181         }
182         else if (nodeType == ContentSpecNode::Leaf)
183         {
184             XSParticle* elemParticle = createElementParticle(rootNode, xsModel);
185             if (elemParticle)
186                 particleList->addElement(elemParticle);
187         }
188         // must be a model group
189         else
190         {
191             XSParticle* xsParticle = createModelGroupParticle(rootNode, xsModel);
192             if (xsParticle)
193                 particleList->addElement(xsParticle);
194         }
195     }
196 }
197 
198 XSParticle*
createElementParticle(const ContentSpecNode * const rootNode,XSModel * const xsModel)199 XSObjectFactory::createElementParticle(const ContentSpecNode* const rootNode,
200                                        XSModel* const xsModel)
201 {
202     if (rootNode->getElementDecl())
203     {
204         XSElementDeclaration* xsElemDecl = addOrFind(
205             (SchemaElementDecl*) rootNode->getElementDecl(), xsModel);
206 
207         if (xsElemDecl)
208         {
209             int m = rootNode->getMaxOccurs();
210             XSParticle* particle = new (fMemoryManager) XSParticle
211             (
212                 XSParticle::TERM_ELEMENT
213                 , xsModel
214                 , xsElemDecl
215                 , (XMLSize_t)rootNode->getMinOccurs()
216                 , (XMLSize_t)m
217                 , m == -1
218                 , fMemoryManager
219             );
220 
221             return particle;
222         }
223     }
224 
225     return 0;
226 }
227 
228 XSParticle*
createWildcardParticle(const ContentSpecNode * const rootNode,XSModel * const xsModel)229 XSObjectFactory::createWildcardParticle(const ContentSpecNode* const rootNode,
230                                         XSModel* const xsModel)
231 {
232     XSWildcard* xsWildcard = createXSWildcard(rootNode, xsModel);
233     if (xsWildcard)
234     {
235         int m = rootNode->getMaxOccurs();
236         XSParticle* particle = new (fMemoryManager) XSParticle
237         (
238             XSParticle::TERM_WILDCARD
239             , xsModel
240             , xsWildcard
241             , (XMLSize_t)rootNode->getMinOccurs()
242             , (XMLSize_t)m
243             , m == -1
244             , fMemoryManager
245         );
246 
247         return particle;
248     }
249 
250     return 0;
251 }
252 
253 XSAttributeDeclaration*
addOrFind(SchemaAttDef * const attDef,XSModel * const xsModel,XSComplexTypeDefinition * const enclosingTypeDef)254 XSObjectFactory::addOrFind(SchemaAttDef* const attDef,
255                            XSModel* const xsModel,
256                            XSComplexTypeDefinition* const enclosingTypeDef)
257 {
258     XSAttributeDeclaration* xsObj = (XSAttributeDeclaration*) xsModel->getXSObject(attDef);
259     if (xsObj)
260     {
261         if (xsObj->getScope() == XSConstants::SCOPE_LOCAL
262             && xsObj->getEnclosingCTDefinition() == 0
263             && enclosingTypeDef)
264             xsObj->setEnclosingCTDefinition(enclosingTypeDef);
265     }
266     else
267     {
268         XSSimpleTypeDefinition* xsType = 0;
269         if (attDef->getDatatypeValidator())
270             xsType = addOrFind(attDef->getDatatypeValidator(), xsModel);
271 
272         XSConstants::SCOPE scope = XSConstants::SCOPE_ABSENT;
273         XSComplexTypeDefinition* enclosingCTDefinition = 0;
274 
275         if (attDef->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
276             scope = XSConstants::SCOPE_GLOBAL;
277         else if (attDef->getPSVIScope() == PSVIDefs::SCP_LOCAL)
278         {
279             scope = XSConstants::SCOPE_LOCAL;
280             enclosingCTDefinition = enclosingTypeDef;
281         }
282 
283         xsObj = new (fMemoryManager) XSAttributeDeclaration
284         (
285             attDef
286             , xsType
287             , getAnnotationFromModel(xsModel, attDef)
288             , xsModel
289             , scope
290             , enclosingCTDefinition
291             , fMemoryManager
292         );
293         putObjectInMap(attDef, xsObj);
294     }
295 
296     return xsObj;
297 }
298 
299 XSSimpleTypeDefinition*
addOrFind(DatatypeValidator * const validator,XSModel * const xsModel,bool isAnySimpleType)300 XSObjectFactory::addOrFind(DatatypeValidator* const validator,
301                            XSModel* const xsModel,
302                            bool isAnySimpleType)
303 {
304     XSSimpleTypeDefinition* xsObj = (XSSimpleTypeDefinition*) xsModel->getXSObject(validator);
305     if (!xsObj)
306     {
307         XSTypeDefinition* baseType = 0;
308         XSSimpleTypeDefinitionList* memberTypes = 0;
309         XSSimpleTypeDefinition* primitiveOrItemType = 0;
310         XSSimpleTypeDefinition::VARIETY typeVariety = XSSimpleTypeDefinition::VARIETY_ATOMIC;
311         bool primitiveTypeSelf = false;
312 
313         //REVISIT: the getFixed method is protected so added friend XSObjectFactory
314         //         to DatatypeValidator class...
315         DatatypeValidator::ValidatorType dvType = validator->getType();
316         DatatypeValidator* baseDV = validator->getBaseValidator();
317 
318         if (dvType == DatatypeValidator::Union)
319         {
320             typeVariety = XSSimpleTypeDefinition::VARIETY_UNION;
321             RefVectorOf<DatatypeValidator>* membersDV = ((UnionDatatypeValidator*)validator)->getMemberTypeValidators();
322             XMLSize_t size = membersDV->size();
323             if (size)
324             {
325                 memberTypes = new (fMemoryManager) RefVectorOf<XSSimpleTypeDefinition>(size, false, fMemoryManager);
326                 for (XMLSize_t i=0; i<size; i++)
327                     memberTypes->addElement(addOrFind(membersDV->elementAt(i), xsModel));
328             }
329 
330             if (baseDV)
331             {
332                 baseType = addOrFind(baseDV, xsModel);
333             }
334             else
335             {
336                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
337                 (
338                     SchemaSymbols::fgDT_ANYSIMPLETYPE
339                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
340                 );
341             }
342         }
343         else if (dvType == DatatypeValidator::List)
344         {
345             typeVariety = XSSimpleTypeDefinition::VARIETY_LIST;
346             if (baseDV->getType() == DatatypeValidator::List)
347             {
348                 baseType = addOrFind(baseDV, xsModel);
349                 primitiveOrItemType = ((XSSimpleTypeDefinition*) baseType)->getItemType();
350             }
351             else
352             {
353                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
354                 (
355                     SchemaSymbols::fgDT_ANYSIMPLETYPE
356                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
357                 );
358                 primitiveOrItemType = addOrFind(baseDV, xsModel);
359             }
360         }
361         else if (!isAnySimpleType)
362         {
363             if (baseDV)
364             {
365                 baseType = addOrFind(baseDV, xsModel);
366                 primitiveOrItemType = ((XSSimpleTypeDefinition*) baseType)->getPrimitiveType();
367             }
368             else // built-in
369             {
370                 baseType = (XSSimpleTypeDefinition*) xsModel->getTypeDefinition
371                 (
372                     SchemaSymbols::fgDT_ANYSIMPLETYPE
373                     , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
374                 );
375                 primitiveTypeSelf = true;
376             }
377         }
378         else
379         {
380             baseType = xsModel->getTypeDefinition(SchemaSymbols::fgATTVAL_ANYTYPE, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
381         }
382 
383         xsObj = new (fMemoryManager) XSSimpleTypeDefinition
384         (
385             validator
386             , typeVariety
387             , baseType
388             , primitiveOrItemType
389             , memberTypes
390             , getAnnotationFromModel(xsModel, validator)
391             , xsModel
392             , fMemoryManager
393         );
394         putObjectInMap(validator, xsObj);
395 
396         if (primitiveTypeSelf)
397             xsObj->setPrimitiveType(xsObj);
398 
399         // process facets
400         processFacets(validator, xsModel, xsObj);
401     }
402 
403     return xsObj;
404 }
405 
406 XSElementDeclaration*
addOrFind(SchemaElementDecl * const elemDecl,XSModel * const xsModel,XSComplexTypeDefinition * const enclosingTypeDef)407 XSObjectFactory::addOrFind(SchemaElementDecl* const elemDecl,
408                            XSModel* const xsModel,
409                            XSComplexTypeDefinition* const enclosingTypeDef)
410 {
411     XSElementDeclaration* xsObj = (XSElementDeclaration*) xsModel->getXSObject(elemDecl);
412     if (xsObj)
413     {
414         if (!xsObj->getEnclosingCTDefinition() && enclosingTypeDef)
415             xsObj->setEnclosingCTDefinition(enclosingTypeDef);
416     }
417     else
418     {
419         XSElementDeclaration*        xsSubElem = 0;
420         XSTypeDefinition*            xsType = 0;
421         XSNamedMap<XSIDCDefinition>* icMap = 0;
422 
423         if (elemDecl->getSubstitutionGroupElem())
424             xsSubElem = addOrFind(elemDecl->getSubstitutionGroupElem(), xsModel);
425 
426         // defer checking for complexTypeInfo until later as it could
427         // eventually need this elemement
428         // but don't check simple type unless no complexTypeInfo present
429         if (!elemDecl->getComplexTypeInfo() && elemDecl->getDatatypeValidator())
430             xsType = addOrFind(elemDecl->getDatatypeValidator(), xsModel);
431 
432         XMLSize_t count = elemDecl->getIdentityConstraintCount();
433         if (count)
434         {
435             //REVISIT: size of hash table....
436             icMap = new (fMemoryManager) XSNamedMap<XSIDCDefinition>
437             (
438                 count
439                 , 29
440                 , xsModel->getURIStringPool()
441                 , false
442                 , fMemoryManager
443             );
444 
445             for (XMLSize_t i = 0; i < count; i++)
446             {
447                 XSIDCDefinition* icDef = addOrFind
448                 (
449                     elemDecl->getIdentityConstraintAt(i)
450                     , xsModel
451                 );
452                 if (icDef)
453                 {
454                     icMap->addElement
455                     (
456                         icDef
457                         , icDef->getName()
458                         , icDef->getNamespace()
459                     );
460                 }
461             }
462         }
463 
464         XSConstants::SCOPE elemScope = XSConstants::SCOPE_ABSENT;
465         if (elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
466             elemScope = XSConstants::SCOPE_LOCAL;
467         else if (elemDecl->getPSVIScope() == PSVIDefs::SCP_GLOBAL)
468             elemScope = XSConstants::SCOPE_GLOBAL;
469 
470         xsObj = new (fMemoryManager) XSElementDeclaration
471         (
472             elemDecl
473             , xsType
474             , xsSubElem
475             , getAnnotationFromModel(xsModel, elemDecl)
476             , icMap
477             , xsModel
478             , elemScope
479             , enclosingTypeDef
480             , fMemoryManager
481         );
482         putObjectInMap(elemDecl, xsObj);
483 
484         if (elemDecl->getComplexTypeInfo())
485         {
486             xsType = addOrFind(elemDecl->getComplexTypeInfo(), xsModel);
487             xsObj->setTypeDefinition(xsType);
488         }
489         else if (!xsType)
490         {
491             xsType = xsModel->getTypeDefinition
492             (
493                 SchemaSymbols::fgATTVAL_ANYTYPE
494                 , SchemaSymbols::fgURI_SCHEMAFORSCHEMA
495             );
496             xsObj->setTypeDefinition(xsType);
497         }
498     }
499 
500     return xsObj;
501 }
502 
503 XSComplexTypeDefinition*
addOrFind(ComplexTypeInfo * const typeInfo,XSModel * const xsModel)504 XSObjectFactory::addOrFind(ComplexTypeInfo* const typeInfo,
505                            XSModel* const xsModel)
506 {
507     XSComplexTypeDefinition* xsObj = (XSComplexTypeDefinition*) xsModel->getXSObject(typeInfo);
508     if (!xsObj)
509     {
510         XSWildcard*             xsWildcard = 0;
511         XSSimpleTypeDefinition* xsSimpleType = 0;
512         XSAttributeUseList*     xsAttList = 0;
513         XSTypeDefinition*       xsBaseType = 0;
514         XSParticle*             xsParticle = 0;
515 
516         if (typeInfo->getAttWildCard())
517             xsWildcard = createXSWildcard(typeInfo->getAttWildCard(), xsModel);
518 
519         if ((typeInfo->getContentType() == SchemaElementDecl::Simple) &&
520             (typeInfo->getDatatypeValidator()))
521             xsSimpleType = addOrFind(typeInfo->getDatatypeValidator(), xsModel);
522 
523         XMLSize_t attCount=0;
524         if (typeInfo->hasAttDefs())
525         {
526             SchemaAttDefList& attDefList = (SchemaAttDefList&) typeInfo->getAttDefList();
527             attCount = attDefList.getAttDefCount();
528             xsAttList = new (fMemoryManager) RefVectorOf<XSAttributeUse>(attCount, false, fMemoryManager);
529             // create list now put fill it in after we put complextype into map
530             // otherwise we may encounter an infinite loop: complextype needs to
531             // addorfind attdef, which does an addorfind on the enclosingCTdefintion.
532         }
533 
534         // compute fBase
535         bool isAnyType = false;
536         if (typeInfo->getBaseComplexTypeInfo() == typeInfo) // case of anyType
537             isAnyType = true;
538         else if (typeInfo->getBaseComplexTypeInfo())
539             xsBaseType = addOrFind(typeInfo->getBaseComplexTypeInfo(), xsModel);
540         else if (typeInfo->getBaseDatatypeValidator())
541             xsBaseType = addOrFind(typeInfo->getBaseDatatypeValidator(), xsModel);
542         else // base is anyType
543             xsBaseType = xsModel->getTypeDefinition(SchemaSymbols::fgATTVAL_ANYTYPE, SchemaSymbols::fgURI_SCHEMAFORSCHEMA);
544 
545         // compute particle
546         ContentSpecNode* contentSpec = typeInfo->getContentSpec();
547         if (contentSpec)
548             xsParticle = createModelGroupParticle(contentSpec, xsModel);
549 
550         xsObj = new (fMemoryManager) XSComplexTypeDefinition
551         (
552             typeInfo
553             , xsWildcard
554             , xsSimpleType
555             , xsAttList
556             , xsBaseType
557             , xsParticle
558             , getAnnotationFromModel(xsModel, typeInfo)
559             , xsModel
560             , fMemoryManager
561         );
562         putObjectInMap(typeInfo, xsObj);
563 
564         if (isAnyType)
565             xsObj->setBaseType(xsObj);
566 
567         if (typeInfo->hasAttDefs())
568         {
569             // now create the xsattributedeclarations...
570             SchemaAttDefList& attDefList = (SchemaAttDefList&) typeInfo->getAttDefList();
571             for(unsigned int i=0; i<attCount; i++)
572             {
573                 XSAttributeDeclaration* xsAttDecl = 0;
574                 SchemaAttDef& attDef = (SchemaAttDef&) attDefList.getAttDef(i);
575 
576                 if (attDef.getBaseAttDecl())
577                 {
578                     xsAttDecl = addOrFind(attDef.getBaseAttDecl(), xsModel);
579                     fXercesToXSMap->put(&attDef, xsAttDecl);
580                 }
581                 else
582                     xsAttDecl = addOrFind(&attDef, xsModel, xsObj);
583 
584                 if (attDef.getDefaultType() != XMLAttDef::Prohibited) {
585 
586                     XSAttributeUse* attUse = createXSAttributeUse(xsAttDecl, xsModel);
587                     xsAttList->addElement(attUse);
588                     processAttUse(&attDef, attUse);
589                 }
590             }
591         }
592 
593         // process local elements
594         XMLSize_t elemCount = typeInfo->elementCount();
595         for (XMLSize_t j=0; j<elemCount; j++)
596         {
597             SchemaElementDecl* elemDecl = typeInfo->elementAt(j);
598 
599             if (elemDecl->getEnclosingScope() == typeInfo->getScopeDefined()
600                 && elemDecl->getPSVIScope() == PSVIDefs::SCP_LOCAL)
601                 addOrFind(elemDecl, xsModel, xsObj);
602         }
603     }
604 
605     return xsObj;
606 }
607 
addOrFind(IdentityConstraint * const ic,XSModel * const xsModel)608 XSIDCDefinition* XSObjectFactory::addOrFind(IdentityConstraint* const ic,
609                                             XSModel* const xsModel)
610 {
611     XSIDCDefinition* xsObj = (XSIDCDefinition*) xsModel->getXSObject(ic);
612     if (!xsObj)
613     {
614         XSIDCDefinition* keyIC = 0;
615         StringList*      stringList = 0;
616         XMLSize_t        fieldCount = ic->getFieldCount();
617 
618         if (fieldCount)
619         {
620             stringList = new (fMemoryManager) RefArrayVectorOf<XMLCh>(
621                 fieldCount, true, fMemoryManager);
622 
623             for(XMLSize_t i=0; i<fieldCount; i++)
624             {
625                 XMLCh* expr = XMLString::replicate
626                 (
627                     ic->getFieldAt(i)->getXPath()->getExpression()
628                     , fMemoryManager
629                 );
630                 stringList->addElement(expr);
631             }
632         }
633 
634         if (ic->getType() == IdentityConstraint::ICType_KEYREF)
635             keyIC = addOrFind(((IC_KeyRef*) ic)->getKey(), xsModel);
636 
637         xsObj= new (fMemoryManager) XSIDCDefinition
638         (
639             ic
640             , keyIC
641             , getAnnotationFromModel(xsModel, ic)
642             , stringList
643             , xsModel
644             , fMemoryManager
645         );
646         putObjectInMap(ic, xsObj);
647     }
648 
649     return xsObj;
650 }
651 
addOrFind(XMLNotationDecl * const notDecl,XSModel * const xsModel)652 XSNotationDeclaration* XSObjectFactory::addOrFind(XMLNotationDecl* const notDecl,
653                                                   XSModel* const xsModel)
654 {
655     XSNotationDeclaration* xsObj = (XSNotationDeclaration*) xsModel->getXSObject(notDecl);
656     if (!xsObj)
657     {
658         xsObj = new (fMemoryManager) XSNotationDeclaration
659         (
660             notDecl
661             , getAnnotationFromModel(xsModel, notDecl)
662             , xsModel
663             , fMemoryManager
664         );
665         putObjectInMap(notDecl, xsObj);
666     }
667 
668     return xsObj;
669 }
670 
671 XSAttributeUse*
createXSAttributeUse(XSAttributeDeclaration * const xsAttDecl,XSModel * const xsModel)672 XSObjectFactory::createXSAttributeUse(XSAttributeDeclaration* const xsAttDecl,
673                                XSModel* const xsModel)
674 {
675     XSAttributeUse* attrUse = new (fMemoryManager) XSAttributeUse(xsAttDecl, xsModel, fMemoryManager);
676     fDeleteVector->addElement(attrUse);
677 
678     return attrUse;
679 }
680 
681 XSWildcard*
createXSWildcard(SchemaAttDef * const attDef,XSModel * const xsModel)682 XSObjectFactory::createXSWildcard(SchemaAttDef* const attDef,
683                                   XSModel* const xsModel)
684 {
685     XSAnnotation* annot = (attDef->getBaseAttDecl())
686         ? getAnnotationFromModel(xsModel, attDef->getBaseAttDecl())
687         : getAnnotationFromModel(xsModel, attDef);
688 
689     XSWildcard* xsWildcard = new (fMemoryManager) XSWildcard
690     (
691         attDef
692         , annot
693         , xsModel
694         , fMemoryManager
695     );
696     fDeleteVector->addElement(xsWildcard);
697 
698     return xsWildcard;
699 }
700 
701 XSWildcard*
createXSWildcard(const ContentSpecNode * const rootNode,XSModel * const xsModel)702 XSObjectFactory::createXSWildcard(const ContentSpecNode* const rootNode,
703                                   XSModel* const xsModel)
704 {
705     XSWildcard* xsWildcard = new (fMemoryManager) XSWildcard
706     (
707         rootNode
708         , getAnnotationFromModel(xsModel, rootNode)
709         , xsModel
710         , fMemoryManager
711     );
712     fDeleteVector->addElement(xsWildcard);
713 
714     return xsWildcard;
715 }
716 
717 XSModelGroupDefinition*
createXSModelGroupDefinition(XercesGroupInfo * const groupInfo,XSModel * const xsModel)718 XSObjectFactory::createXSModelGroupDefinition(XercesGroupInfo* const groupInfo,
719                                               XSModel* const xsModel)
720 {
721     XSParticle* particle = createModelGroupParticle(
722             groupInfo->getContentSpec(), xsModel);
723 
724     XSModelGroupDefinition* xsObj = new (fMemoryManager) XSModelGroupDefinition
725     (
726         groupInfo
727         , particle
728         , getAnnotationFromModel(xsModel, groupInfo)
729         , xsModel
730         , fMemoryManager
731     );
732     fDeleteVector->addElement(xsObj);
733 
734     // process local elements
735     XMLSize_t elemCount = groupInfo->elementCount();
736     for (XMLSize_t j=0; j<elemCount; j++)
737     {
738         SchemaElementDecl* elemDecl = groupInfo->elementAt(j);
739 
740         if (elemDecl->getEnclosingScope() == groupInfo->getScope())
741             addOrFind(elemDecl, xsModel);
742     }
743 
744     return xsObj;
745 }
746 
747 
748 XSAttributeGroupDefinition*
createXSAttGroupDefinition(XercesAttGroupInfo * const attGroupInfo,XSModel * const xsModel)749 XSObjectFactory::createXSAttGroupDefinition(XercesAttGroupInfo* const attGroupInfo,
750                                             XSModel* const xsModel)
751 {
752     XSAttributeUseList* xsAttList = 0;
753     XSWildcard*         xsWildcard = 0;
754     XMLSize_t           attCount = attGroupInfo->attributeCount();
755 
756     if (attCount)
757     {
758         xsAttList = new (fMemoryManager) RefVectorOf<XSAttributeUse>(attCount, false, fMemoryManager);
759         for (XMLSize_t i=0; i < attCount; i++)
760         {
761             SchemaAttDef* attDef = attGroupInfo->attributeAt(i);
762             XSAttributeDeclaration* xsAttDecl = 0;
763 
764             if (attDef->getBaseAttDecl())
765                 xsAttDecl = addOrFind(attDef->getBaseAttDecl(), xsModel);
766             else
767                 xsAttDecl = addOrFind(attDef, xsModel);
768 
769             if (xsAttDecl && (attDef->getDefaultType() != XMLAttDef::Prohibited)) // just for sanity
770             {
771                 XSAttributeUse* attUse = createXSAttributeUse(xsAttDecl, xsModel);
772                 xsAttList->addElement(attUse);
773                 processAttUse(attDef, attUse);
774             }
775         }
776     }
777 
778     if (attGroupInfo->getCompleteWildCard())
779         xsWildcard = createXSWildcard(attGroupInfo->getCompleteWildCard(), xsModel);
780 
781     XSAttributeGroupDefinition* xsObj = new (fMemoryManager) XSAttributeGroupDefinition
782     (
783         attGroupInfo
784         , xsAttList
785         , xsWildcard
786         , getAnnotationFromModel(xsModel, attGroupInfo)
787         , xsModel
788         , fMemoryManager
789     );
790     fDeleteVector->addElement(xsObj);
791 
792     return xsObj;
793 }
794 
getAnnotationFromModel(XSModel * const xsModel,const void * const key)795 XSAnnotation* XSObjectFactory::getAnnotationFromModel(XSModel* const xsModel,
796                                                const void* const key)
797 {
798     XSNamespaceItemList* namespaceItemList = xsModel->getNamespaceItems();
799 
800     XSAnnotation* annot = 0;
801     for (unsigned int i=0; i<namespaceItemList->size(); i++)
802     {
803         XSNamespaceItem* nsItem = namespaceItemList->elementAt(i);
804         if (nsItem->fGrammar)
805         {
806             annot = nsItem->fGrammar->getAnnotation(key);
807             if (annot)
808                 return annot;
809         }
810     }
811 
812     if (xsModel->fParent)
813         return getAnnotationFromModel(xsModel->fParent, key);
814     return 0;
815 }
816 
817 
putObjectInMap(void * key,XSObject * const object)818 void XSObjectFactory::putObjectInMap(void* key, XSObject* const object)
819 {
820      fXercesToXSMap->put(key, object);
821      fDeleteVector->addElement(object);
822 }
823 
processFacets(DatatypeValidator * const dv,XSModel * const xsModel,XSSimpleTypeDefinition * const xsST)824 void XSObjectFactory::processFacets(DatatypeValidator* const dv,
825                                     XSModel* const xsModel,
826                                     XSSimpleTypeDefinition* const xsST)
827 {
828     // NOTE: XSMultiValueFacetList is not owned by XSModel!
829     // NOTE: XSFacetList is not owned by XSModel!
830     bool isFixed = false;
831     int dvFacetsDefined = dv->getFacetsDefined();
832     int dvFixedFacets = dv->getFixed();
833     int definedFacets = 0;
834     int fixedFacets = 0;
835     XSMultiValueFacetList* xsMultiFacetList = 0;
836     StringList* patternList = 0;
837     XSFacetList* xsFacetList = new (fMemoryManager) RefVectorOf<XSFacet>(4, false, fMemoryManager);
838 
839     if (isMultiValueFacetDefined(dv))
840         xsMultiFacetList = new (fMemoryManager) RefVectorOf<XSMultiValueFacet>(2, false, fMemoryManager);
841 
842     if (dvFacetsDefined & DatatypeValidator::FACET_ENUMERATION)
843     {
844         RefArrayVectorOf<XMLCh>* enumList = (RefArrayVectorOf<XMLCh>*) dv->getEnumString();
845 
846         if (dvFixedFacets & DatatypeValidator::FACET_ENUMERATION)
847         {
848             isFixed = true;
849             fixedFacets |= XSSimpleTypeDefinition::FACET_ENUMERATION;
850         }
851 
852         XSMultiValueFacet* mvFacet = new (fMemoryManager) XSMultiValueFacet(
853             XSSimpleTypeDefinition::FACET_ENUMERATION , enumList, isFixed
854             , getAnnotationFromModel(xsModel, enumList), xsModel, fMemoryManager
855         );
856 
857         fDeleteVector->addElement(mvFacet);
858         xsMultiFacetList->addElement(mvFacet);
859         definedFacets |= XSSimpleTypeDefinition::FACET_ENUMERATION;
860     }
861 
862     if (dv->getFacets())
863     {
864         RefHashTableOfEnumerator<KVStringPair> e(dv->getFacets(), false, fMemoryManager);
865         while (e.hasMoreElements())
866         {
867             KVStringPair& pair = e.nextElement();
868             XMLCh* key = pair.getKey();
869             XSSimpleTypeDefinition::FACET facetType;
870             XSAnnotation* annot = getAnnotationFromModel(xsModel, &pair);
871 
872             if (XMLString::equals(key, SchemaSymbols::fgELT_MAXINCLUSIVE))
873             {
874                 facetType = XSSimpleTypeDefinition::FACET_MAXINCLUSIVE;
875                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXINCLUSIVE) != 0);
876             }
877             else if (XMLString::equals(key, SchemaSymbols::fgELT_MAXEXCLUSIVE))
878             {
879                 facetType = XSSimpleTypeDefinition::FACET_MAXEXCLUSIVE;
880                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXEXCLUSIVE) !=0);
881             }
882             else if (XMLString::equals(key, SchemaSymbols::fgELT_MININCLUSIVE))
883             {
884                 facetType = XSSimpleTypeDefinition::FACET_MININCLUSIVE;
885                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MININCLUSIVE) !=0);
886             }
887             else if (XMLString::equals(key, SchemaSymbols::fgELT_MINEXCLUSIVE))
888             {
889                 facetType = XSSimpleTypeDefinition::FACET_MINEXCLUSIVE;
890                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MINEXCLUSIVE) != 0);
891             }
892             else if (XMLString::equals(key, SchemaSymbols::fgELT_LENGTH))
893             {
894                 facetType = XSSimpleTypeDefinition::FACET_LENGTH;
895                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_LENGTH) != 0);
896             }
897             else if (XMLString::equals(key, SchemaSymbols::fgELT_MINLENGTH))
898             {
899                 facetType = XSSimpleTypeDefinition::FACET_MINLENGTH;
900                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MINLENGTH) != 0);
901             }
902             else if (XMLString::equals(key, SchemaSymbols::fgELT_MAXLENGTH))
903             {
904                 facetType = XSSimpleTypeDefinition::FACET_MAXLENGTH;
905                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_MAXLENGTH) != 0);
906             }
907             else if (XMLString::equals(key, SchemaSymbols::fgELT_TOTALDIGITS))
908             {
909                 facetType = XSSimpleTypeDefinition::FACET_TOTALDIGITS;
910                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_TOTALDIGITS) != 0);
911             }
912             else if (XMLString::equals(key, SchemaSymbols::fgELT_FRACTIONDIGITS))
913             {
914                 facetType = XSSimpleTypeDefinition::FACET_FRACTIONDIGITS;
915                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_FRACTIONDIGITS) != 0);
916             }
917             else if (XMLString::equals(key, SchemaSymbols::fgELT_WHITESPACE))
918             {
919                 facetType = XSSimpleTypeDefinition::FACET_WHITESPACE;
920                 isFixed = ((dvFixedFacets & DatatypeValidator::FACET_WHITESPACE) != 0);
921             }
922             else if (XMLString::equals(key, SchemaSymbols::fgELT_PATTERN))
923             {
924                 XMLStringTokenizer tokenizer(dv->getPattern(), regexSeparator, fMemoryManager);
925                 patternList = new (fMemoryManager) RefArrayVectorOf<XMLCh>(
926                     tokenizer.countTokens(), true, fMemoryManager
927                 );
928 
929                 while (tokenizer.hasMoreTokens())
930                     patternList->addElement(XMLString::replicate(tokenizer.nextToken(), fMemoryManager));
931 
932                 if (dvFixedFacets & DatatypeValidator::FACET_PATTERN)
933                 {
934                     isFixed = true;
935                     fixedFacets |= XSSimpleTypeDefinition::FACET_PATTERN;
936                 }
937 
938                 XSMultiValueFacet* mvFacet = new (fMemoryManager) XSMultiValueFacet(
939                     XSSimpleTypeDefinition::FACET_PATTERN, patternList
940                     , isFixed, annot, xsModel, fMemoryManager
941                 );
942                 fDeleteVector->addElement(mvFacet);
943                 xsMultiFacetList->addElement(mvFacet);
944                 definedFacets |= XSSimpleTypeDefinition::FACET_PATTERN;
945                 continue;
946             }
947             else
948             {
949                 // REVISIT: hmm... what about XSSimpleTypeDefinition::FACET_NONE
950                 // don't think I need to create an empty Facet?
951                 continue;
952             }
953 
954             XSFacet* xsFacet = new (fMemoryManager) XSFacet(
955                 facetType, pair.getValue(), isFixed, annot, xsModel, fMemoryManager);
956 
957             fDeleteVector->addElement(xsFacet);
958             xsFacetList->addElement(xsFacet);
959             definedFacets |= facetType;
960             if (isFixed)
961                 fixedFacets |= facetType;
962         }
963     }
964 
965     // add whistespace facet if missing
966     if ((definedFacets & XSSimpleTypeDefinition::FACET_WHITESPACE) == 0)
967     {
968         XSFacet* xsFacet = new (fMemoryManager) XSFacet(
969             XSSimpleTypeDefinition::FACET_WHITESPACE
970             , dv->getWSstring(dv->getWSFacet())
971             , false, 0, xsModel, fMemoryManager);
972 
973         fDeleteVector->addElement(xsFacet);
974         xsFacetList->addElement(xsFacet);
975         definedFacets |= XSSimpleTypeDefinition::FACET_WHITESPACE;
976     }
977 
978     // inherit facets from base
979 
980     if (xsST->getBaseType() && xsST->getBaseType()->getTypeCategory() == XSTypeDefinition::SIMPLE_TYPE)
981     {
982         XSSimpleTypeDefinition* baseST = (XSSimpleTypeDefinition*) xsST->getBaseType();
983         XSFacetList* baseFacets = baseST->getFacets();
984 
985         for (unsigned int i=0; i<baseFacets->size(); i++)
986         {
987             XSFacet* bFacet = baseFacets->elementAt(i);
988             if ((definedFacets & bFacet->getFacetKind()) == 0)
989             {
990                 definedFacets |= bFacet->getFacetKind();
991                 xsFacetList->addElement(bFacet);
992                 if (bFacet->isFixed())
993                     fixedFacets |= bFacet->getFacetKind();
994             }
995         }
996 
997         if (baseST->getMultiValueFacets())
998         {
999             XSMultiValueFacetList* baseMVFacets = baseST->getMultiValueFacets();
1000             for (unsigned int j=0; j<baseMVFacets->size(); j++)
1001             {
1002                 XSMultiValueFacet* bFacet = baseMVFacets->elementAt(j);
1003                 if ((definedFacets & bFacet->getFacetKind()) == 0)
1004                 {
1005                     definedFacets |= bFacet->getFacetKind();
1006                     xsMultiFacetList->addElement(bFacet);
1007                     if (bFacet->isFixed())
1008                         fixedFacets |= bFacet->getFacetKind();
1009                 }
1010             }
1011         }
1012     }
1013 
1014     xsST->setFacetInfo(definedFacets, fixedFacets, xsFacetList, xsMultiFacetList, patternList);
1015 }
1016 
processAttUse(SchemaAttDef * const attDef,XSAttributeUse * const xsAttUse)1017 void XSObjectFactory::processAttUse(SchemaAttDef* const attDef,
1018                                     XSAttributeUse* const xsAttUse)
1019 {
1020     bool isRequired = false;
1021     XSConstants::VALUE_CONSTRAINT constraintType = XSConstants::VALUE_CONSTRAINT_NONE;
1022 
1023     if (attDef->getDefaultType() == XMLAttDef::Default)
1024     {
1025         constraintType = XSConstants::VALUE_CONSTRAINT_DEFAULT;
1026     }
1027     else if ((attDef->getDefaultType() == XMLAttDef::Fixed) ||
1028              (attDef->getDefaultType() == XMLAttDef::Required_And_Fixed))
1029     {
1030         constraintType = XSConstants::VALUE_CONSTRAINT_FIXED;
1031     }
1032 
1033     if (attDef->getDefaultType() == XMLAttDef::Required ||
1034         attDef->getDefaultType() == XMLAttDef::Required_And_Fixed)
1035         isRequired = true;
1036 
1037     xsAttUse->set(isRequired, constraintType, attDef->getValue());
1038 }
1039 
isMultiValueFacetDefined(DatatypeValidator * const dv)1040 bool XSObjectFactory::isMultiValueFacetDefined(DatatypeValidator* const dv)
1041 {
1042     DatatypeValidator* tmpDV = dv;
1043 
1044     while (tmpDV)
1045     {
1046         if ((tmpDV->getFacetsDefined() & DatatypeValidator::FACET_PATTERN)
1047         ||  (tmpDV->getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION))
1048             return true;
1049 
1050         tmpDV = tmpDV->getBaseValidator();
1051     }
1052 
1053     return false;
1054 }
1055 
1056 XERCES_CPP_NAMESPACE_END
1057