source: trunk/src/xmlpatterns/schema/qxsdparticlechecker.cpp@ 973

Last change on this file since 973 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 26.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qxsdparticlechecker_p.h"
43
44#include "qxsdelement_p.h"
45#include "qxsdmodelgroup_p.h"
46#include "qxsdschemahelper_p.h"
47#include "qxsdstatemachine_p.h"
48#include "qxsdstatemachinebuilder_p.h"
49#include "qxsdtypechecker_p.h"
50
51#include <QtCore/QFile>
52
53QT_BEGIN_NAMESPACE
54
55using namespace QPatternist;
56
57namespace QPatternist
58{
59 /**
60 * This template specialization is picked up by XsdStateMachine and allows us
61 * to print nice edge labels.
62 */
63 template <>
64 QString XsdStateMachine<XsdTerm::Ptr>::transitionTypeToString(XsdTerm::Ptr term) const
65 {
66 if (!term)
67 return QLatin1String("(empty)");
68
69 if (term->isElement()) {
70 return XsdElement::Ptr(term)->displayName(m_namePool);
71 } else if (term->isWildcard()) {
72 const XsdWildcard::Ptr wildcard(term);
73 return QLatin1String("(wildcard)");
74 } else {
75 return QString();
76 }
77 }
78}
79
80/**
81 * This method is used by the isUPAConform method to check whether @p term and @p otherTerm
82 * are the same resp. match each other.
83 */
84static bool termMatches(const XsdTerm::Ptr &term, const XsdTerm::Ptr &otherTerm, const NamePool::Ptr &namePool)
85{
86 if (term->isElement()) {
87 const XsdElement::Ptr element(term);
88
89 if (otherTerm->isElement()) {
90 // both, the term and the other term are elements
91
92 const XsdElement::Ptr otherElement(otherTerm);
93
94 // if they have the same name they match
95 if (element->name(namePool) == otherElement->name(namePool))
96 return true;
97
98 } else if (otherTerm->isWildcard()) {
99 // the term is an element and the other term a wildcard
100
101 const XsdWildcard::Ptr wildcard(otherTerm);
102
103 // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
104 QXmlName name = element->name(namePool);
105 if (name.namespaceURI() == StandardNamespaces::empty)
106 name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));
107
108 // if the wildcards namespace constraint allows the elements name, they match
109 if (XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool))
110 return true;
111 }
112 } else if (term->isWildcard()) {
113 const XsdWildcard::Ptr wildcard(term);
114
115 if (otherTerm->isElement()) {
116 // the term is a wildcard and the other term an element
117
118 const XsdElement::Ptr otherElement(otherTerm);
119
120 // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
121 QXmlName name = otherElement->name(namePool);
122 if (name.namespaceURI() == StandardNamespaces::empty)
123 name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));
124
125 // if the wildcards namespace constraint allows the elements name, they match
126 if (XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool))
127 return true;
128
129 } else if (otherTerm->isWildcard()) {
130 // both, the term and the other term are wildcards
131
132 const XsdWildcard::Ptr otherWildcard(otherTerm);
133
134 // check if the range of the wildcard overlaps.
135 const XsdWildcard::Ptr intersectionWildcard = XsdSchemaHelper::wildcardIntersection(wildcard, otherWildcard);
136 if (!intersectionWildcard ||
137 (intersectionWildcard && !(intersectionWildcard->namespaceConstraint()->variety() != XsdWildcard::NamespaceConstraint::Not && intersectionWildcard->namespaceConstraint()->namespaces().isEmpty())))
138 return true;
139 }
140 }
141
142 return false;
143}
144
145/**
146 * This method is used by the subsumes algorithm to check whether the @p derivedTerm is validly derived from the @p baseTerm.
147 *
148 * @param baseTerm The term of the base component (type or group).
149 * @param derivedTerm The term of the derived component (type or group).
150 * @param particles A hash to map the passed base and derived term to the particles they belong to.
151 * @param context The schema context.
152 * @param errorMsg The error message in the case that an error occurs.
153 */
154static bool derivedTermValid(const XsdTerm::Ptr &baseTerm, const XsdTerm::Ptr &derivedTerm, const QHash<XsdTerm::Ptr, XsdParticle::Ptr> &particles, const XsdSchemaContext::Ptr &context, QString &errorMsg)
155{
156 const NamePool::Ptr namePool(context->namePool());
157
158 // find the particles where the base and derived term belongs to
159 const XsdParticle::Ptr baseParticle = particles.value(baseTerm);
160 const XsdParticle::Ptr derivedParticle = particles.value(derivedTerm);
161
162 // check that an empty particle can not be derived from a non-empty particle
163 if (derivedParticle && baseParticle) {
164 if (XsdSchemaHelper::isParticleEmptiable(derivedParticle) && !XsdSchemaHelper::isParticleEmptiable(baseParticle)) {
165 errorMsg = QtXmlPatterns::tr("Empty particle cannot be derived from non-empty particle.");
166 return false;
167 }
168 }
169
170 if (baseTerm->isElement()) {
171 const XsdElement::Ptr element(baseTerm);
172
173 if (derivedTerm->isElement()) {
174 // if both terms are elements
175
176 const XsdElement::Ptr derivedElement(derivedTerm);
177
178 // check names are equal
179 if (element->name(namePool) != derivedElement->name(namePool)) {
180 errorMsg = QtXmlPatterns::tr("Derived particle is missing element %1.").arg(formatKeyword(element->displayName(namePool)));
181 return false;
182 }
183
184 // check value constraints are equal (if available)
185 if (element->valueConstraint() && element->valueConstraint()->variety() == XsdElement::ValueConstraint::Fixed) {
186 if (!derivedElement->valueConstraint()) {
187 errorMsg = QtXmlPatterns::tr("Derived element %1 is missing value constraint as defined in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
188 return false;
189 }
190
191 if (derivedElement->valueConstraint()->variety() != XsdElement::ValueConstraint::Fixed) {
192 errorMsg = QtXmlPatterns::tr("Derived element %1 has weaker value constraint than base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
193 return false;
194 }
195
196 const QSourceLocation dummyLocation(QUrl(QLatin1String("http://dummy.org")), 1, 1);
197 const XsdTypeChecker checker(context, QVector<QXmlName>(), dummyLocation);
198 if (!checker.valuesAreEqual(element->valueConstraint()->value(), derivedElement->valueConstraint()->value(), derivedElement->type())) {
199 errorMsg = QtXmlPatterns::tr("Fixed value constraint of element %1 differs from value constraint in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
200 return false;
201 }
202 }
203
204 // check that a derived element can not be nillable if the base element is not nillable
205 if (!element->isNillable() && derivedElement->isNillable()) {
206 errorMsg = QtXmlPatterns::tr("Derived element %1 cannot be nillable as base element is not nillable.").arg(formatKeyword(derivedElement->displayName(namePool)));
207 return false;
208 }
209
210 // check that the constraints of the derived element are more strict then the constraints of the base element
211 const XsdElement::BlockingConstraints baseConstraints = element->disallowedSubstitutions();
212 const XsdElement::BlockingConstraints derivedConstraints = derivedElement->disallowedSubstitutions();
213 if (((baseConstraints & XsdElement::RestrictionConstraint) && !(derivedConstraints & XsdElement::RestrictionConstraint)) ||
214 ((baseConstraints & XsdElement::ExtensionConstraint) && !(derivedConstraints & XsdElement::ExtensionConstraint)) ||
215 ((baseConstraints & XsdElement::SubstitutionConstraint) && !(derivedConstraints & XsdElement::SubstitutionConstraint))) {
216 errorMsg = QtXmlPatterns::tr("Block constraints of derived element %1 must not be more weaker than in the base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
217 return false;
218 }
219
220 // if the type of both elements is the same we can stop testing here
221 if (element->type()->name(namePool) == derivedElement->type()->name(namePool))
222 return true;
223
224 // check that the type of the derived element can validly derived from the type of the base element
225 if (derivedElement->type()->isSimpleType()) {
226 if (!XsdSchemaHelper::isSimpleDerivationOk(derivedElement->type(), element->type(), SchemaType::DerivationConstraints())) {
227 errorMsg = QtXmlPatterns::tr("Simple type of derived element %1 cannot be validly derived from base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
228 return false;
229 }
230 } else if (derivedElement->type()->isComplexType()) {
231 if (!XsdSchemaHelper::isComplexDerivationOk(derivedElement->type(), element->type(), SchemaType::DerivationConstraints())) {
232 errorMsg = QtXmlPatterns::tr("Complex type of derived element %1 cannot be validly derived from base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
233 return false;
234 }
235 }
236
237 // if both, derived and base element, have a complex type that contains a particle itself, apply the subsumes algorithm
238 // recursive on their particles
239 if (element->type()->isComplexType() && derivedElement->type()->isComplexType()) {
240 if (element->type()->isDefinedBySchema() && derivedElement->type()->isDefinedBySchema()) {
241 const XsdComplexType::Ptr baseType(element->type());
242 const XsdComplexType::Ptr derivedType(derivedElement->type());
243 if ((baseType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly ||
244 baseType->contentType()->variety() == XsdComplexType::ContentType::Mixed) &&
245 (derivedType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly ||
246 derivedType->contentType()->variety() == XsdComplexType::ContentType::Mixed)) {
247
248 return XsdParticleChecker::subsumes(baseType->contentType()->particle(), derivedType->contentType()->particle(), context, errorMsg);
249 }
250 }
251 }
252
253 return true;
254 } else if (derivedTerm->isWildcard()) {
255 // derive a wildcard from an element is not allowed
256 errorMsg = QtXmlPatterns::tr("Element %1 is missing in derived particle.").arg(formatKeyword(element->displayName(namePool)));
257 return false;
258 }
259 } else if (baseTerm->isWildcard()) {
260 const XsdWildcard::Ptr wildcard(baseTerm);
261
262 if (derivedTerm->isElement()) {
263 // the base term is a wildcard and derived term an element
264
265 const XsdElement::Ptr derivedElement(derivedTerm);
266
267 // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
268 QXmlName name = derivedElement->name(namePool);
269 if (name.namespaceURI() == StandardNamespaces::empty)
270 name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));
271
272 // check that name of the element is allowed by the wildcards namespace constraint
273 if (!XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool)) {
274 errorMsg = QtXmlPatterns::tr("Element %1 does not match namespace constraint of wildcard in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
275 return false;