source: icXML/icXML-devel/src/xercesc/validators/schema/SubstitutionGroupComparator.cpp @ 2736

Last change on this file since 2736 was 2736, checked in by cameron, 6 years ago

icxml versions of SchemaValidator?.cpp, ComplexTypeInfo?.hpp

File size: 7.6 KB
Line 
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: SubstitutionGroupComparator.cpp 794273 2009-07-15 14:13:07Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <icxercesc/framework/XMLGrammarPool.hpp>
27#include <xercesc/framework/XMLSchemaDescription.hpp>
28#include <xercesc/framework/psvi/XSAnnotation.hpp>
29#include <icxercesc/validators/schema/SubstitutionGroupComparator.hpp>
30#include <xercesc/validators/common/Grammar.hpp>
31#include <xercesc/validators/schema/SchemaGrammar.hpp>
32#include <icxercesc/validators/schema/ComplexTypeInfo.hpp>
33#include <icxercesc/validators/schema/SchemaSymbols.hpp>
34
35XERCES_CPP_NAMESPACE_BEGIN
36
37bool SubstitutionGroupComparator::isEquivalentTo(const QName* const anElement
38                                               , const QName* const exemplar)
39{
40    if (!anElement && !exemplar)
41        return true;
42
43    if ((!anElement && exemplar) || (anElement && !exemplar))
44        return false;
45
46
47    if (XMLString::equals(anElement->getLocalPart(), exemplar->getLocalPart()) &&
48        (anElement->getURI() == exemplar->getURI()))
49        return true; // they're the same!
50
51    if (!fGrammarResolver || !fStringPool )
52    {
53        ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::SubGrpComparator_NGR, anElement->getMemoryManager());
54    }
55
56    unsigned int uriId = anElement->getURI();
57    if (uriId == XMLContentModel::gEOCFakeId ||
58        uriId == XMLContentModel::gEpsilonFakeId ||
59        uriId == XMLElementDecl::fgPCDataElemId ||
60        uriId == XMLElementDecl::fgInvalidElemId)
61        return false;
62
63    const XMLCh* uri = fStringPool->getValueForId(uriId);
64    const XMLCh* localpart = anElement->getLocalPart();
65
66    // In addition to simply trying to find a chain between anElement and exemplar,
67    // we need to make sure that no steps in the chain are blocked.
68    // That is, at every step, we need to make sure that the element
69    // being substituted for will permit being substituted
70    // for, and whether the type of the element will permit derivations in
71    // instance documents of this sort.
72
73    if (!uri)
74        return false;
75
76    SchemaGrammar *sGrammar = (SchemaGrammar*) fGrammarResolver->getGrammar(uri);
77    if (!sGrammar || sGrammar->getGrammarType() == Grammar::DTDGrammarType)
78        return false;
79
80    SchemaElementDecl* anElementDecl = (SchemaElementDecl*) sGrammar->getElemDecl(uriId, localpart, 0, Grammar::TOP_LEVEL_SCOPE);
81    if (!anElementDecl)
82        return false;
83
84    SchemaElementDecl* pElemDecl = anElementDecl->getSubstitutionGroupElem();
85    bool foundIt = false;
86
87    while (pElemDecl) //(substitutionGroupFullName)
88    {
89        if (XMLString::equals(pElemDecl->getBaseName(), exemplar->getLocalPart()) &&
90            (pElemDecl->getURI() == exemplar->getURI()))
91        {
92            // time to check for block value on element
93            if((pElemDecl->getBlockSet() & SchemaSymbols::XSD_SUBSTITUTION) != 0)
94                return false;
95
96            foundIt = true;
97            break;
98        }
99
100        pElemDecl = pElemDecl->getSubstitutionGroupElem();
101    }//while
102
103    if (!foundIt)
104        return false;
105
106    // this will contain anElement's complexType information.
107    ComplexTypeInfo *aComplexType = anElementDecl->getComplexTypeInfo();
108    int exemplarBlockSet = pElemDecl->getBlockSet();
109
110    if(!aComplexType)
111    {
112        // check on simpleType case
113        DatatypeValidator *anElementDV = anElementDecl->getDatatypeValidator();
114        DatatypeValidator *exemplarDV = pElemDecl->getDatatypeValidator();
115
116        return((anElementDV == 0) ||
117            ((anElementDV == exemplarDV) ||
118            ((exemplarBlockSet & SchemaSymbols::XSD_RESTRICTION) == 0)));
119    }
120
121    // 2.3 The set of all {derivation method}s involved in the derivation of D's {type definition} from C's {type definition} does not intersect with the union of the blocking constraint, C's {prohibited substitutions} (if C is complex, otherwise the empty set) and the {prohibited substitutions} (respectively the empty set) of any intermediate {type definition}s in the derivation of D's {type definition} from C's {type definition}.
122    // prepare the combination of {derivation method} and
123    // {disallowed substitution}
124    int devMethod = 0;
125    int blockConstraint = exemplarBlockSet;
126
127    ComplexTypeInfo *exemplarComplexType = pElemDecl->getComplexTypeInfo();
128    ComplexTypeInfo *tempType = aComplexType;;
129
130    while (tempType != 0 &&
131        tempType != exemplarComplexType)
132    {
133        devMethod |= tempType->getDerivedBy();
134        tempType = tempType->getBaseComplexTypeInfo();
135        if (tempType) {
136            blockConstraint |= tempType->getBlockSet();
137        }
138    }
139    if (tempType != exemplarComplexType) {
140        return false;
141    }
142    if ((devMethod & blockConstraint) != 0) {
143        return false;
144    }
145
146    return true;
147}
148
149
150bool SubstitutionGroupComparator::isAllowedByWildcard(SchemaGrammar* const pGrammar,
151                                                      QName* const element,
152                                                      unsigned int wuri, bool wother)
153{
154    // whether the uri is allowed directly by the wildcard
155    unsigned int uriId = element->getURI();
156
157    // Here we assume that empty string has id 1.
158    //
159    if ((!wother && uriId == wuri) ||
160        (wother &&
161         uriId != 1 &&
162         uriId != wuri &&
163         uriId != XMLContentModel::gEOCFakeId &&
164         uriId != XMLContentModel::gEpsilonFakeId &&
165         uriId != XMLElementDecl::fgPCDataElemId &&
166         uriId != XMLElementDecl::fgInvalidElemId))
167    {
168        return true;
169    }
170
171    // get all elements that can substitute the current element
172    RefHash2KeysTableOf<ElemVector>* theValidSubstitutionGroups = pGrammar->getValidSubstitutionGroups();
173
174    if (!theValidSubstitutionGroups)
175        return false;
176
177    ValueVectorOf<SchemaElementDecl*>* subsElements = theValidSubstitutionGroups->get(element->getLocalPart(), uriId);
178
179    if (!subsElements)
180        return false;
181
182    // then check whether there exists one element that is allowed by the wildcard
183    XMLSize_t size = subsElements->size();
184
185    for (XMLSize_t i = 0; i < size; i++)
186    {
187        unsigned int subUriId = subsElements->elementAt(i)->getElementName()->getURI();
188
189        // Here we assume that empty string has id 1.
190        //
191        if ((!wother && subUriId == wuri) ||
192            (wother &&
193             subUriId != 1 &&
194             subUriId != wuri &&
195             subUriId != XMLContentModel::gEOCFakeId &&
196             subUriId != XMLContentModel::gEpsilonFakeId &&
197             subUriId != XMLElementDecl::fgPCDataElemId &&
198             subUriId != XMLElementDecl::fgInvalidElemId))
199        {
200            return true;
201        }
202    }
203    return false;
204}
205
206XERCES_CPP_NAMESPACE_END
207
208/**
209  * End of file SubstitutionGroupComparator.cpp
210  */
Note: See TracBrowser for help on using the repository browser.