1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
44 using namespace QPatternist;
46 bool XsdSchemaChecker::hasDuplicatedAttributeUses(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const
48 const int length = list.count();
50 for (int i = 0; i < length; ++i) {
51 for (int j = 0; j < length; ++j) {
55 if (list.at(i)->attribute()->name(m_namePool) == list.at(j)->attribute()->name(m_namePool)) {
56 conflictingAttribute = list.at(i)->attribute();
65 bool XsdSchemaChecker::hasMultipleIDAttributeUses(const XsdAttributeUse::List &list) const
67 const int length = list.count();
69 bool hasIdDerivedAttribute = false;
70 for (int i = 0; i < length; ++i) {
71 if (BuiltinTypes::xsID->wxsTypeMatches(list.at(i)->attribute()->type())) {
72 if (hasIdDerivedAttribute)
75 hasIdDerivedAttribute = true;
82 bool XsdSchemaChecker::hasConstraintIDAttributeUse(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const
84 const int length = list.count();
86 for (int i = 0; i < length; ++i) {
87 const XsdAttributeUse::Ptr attributeUse(list.at(i));
88 if (BuiltinTypes::xsID->wxsTypeMatches(attributeUse->attribute()->type())) {
89 if (attributeUse->valueConstraint()) {
90 conflictingAttribute = attributeUse->attribute();
99 bool XsdSchemaChecker::particleEqualsRecursively(const XsdParticle::Ptr &particle, const XsdParticle::Ptr &otherParticle) const
101 // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend
102 //TODO: find out what 'properties' of a particle should be checked here...
104 if (particle->minimumOccurs() != otherParticle->minimumOccurs())
107 if (particle->maximumOccursUnbounded() != otherParticle->maximumOccursUnbounded())
110 if (particle->maximumOccurs() != otherParticle->maximumOccurs())
113 const XsdTerm::Ptr term = particle->term();
114 const XsdTerm::Ptr otherTerm = otherParticle->term();
116 if (term->isElement() && !(otherTerm->isElement()))
119 if (term->isModelGroup() && !(otherTerm->isModelGroup()))
122 if (term->isWildcard() && !(otherTerm->isWildcard()))
125 if (term->isElement()) {
126 const XsdElement::Ptr element = term;
127 const XsdElement::Ptr otherElement = otherTerm;
129 if (element->name(m_namePool) != otherElement->name(m_namePool))
132 if (element->type()->name(m_namePool) != otherElement->type()->name(m_namePool))
136 if (term->isModelGroup()) {
137 const XsdModelGroup::Ptr group = term;
138 const XsdModelGroup::Ptr otherGroup = otherTerm;
140 if (group->particles().count() != otherGroup->particles().count())
143 for (int i = 0; i < group->particles().count(); ++i) {
144 if (!particleEqualsRecursively(group->particles().at(i), otherGroup->particles().at(i)))
149 if (term->isWildcard()) {
155 bool XsdSchemaChecker::isValidParticleExtension(const XsdParticle::Ptr &extension, const XsdParticle::Ptr &base) const
157 // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend
160 if (extension == base)
164 if (extension->minimumOccurs() == 1 && extension->maximumOccurs() == 1 && extension->maximumOccursUnbounded() == false) {
165 if (extension->term()->isModelGroup()) {
166 const XsdModelGroup::Ptr modelGroup = extension->term();
167 if (modelGroup->compositor() == XsdModelGroup::SequenceCompositor) {
168 if (particleEqualsRecursively(modelGroup->particles().first(), base))
175 if (extension->minimumOccurs() == base->minimumOccurs()) { // 3.1
176 if (extension->term()->isModelGroup() && base->term()->isModelGroup()) {
177 const XsdModelGroup::Ptr extensionGroup(extension->term());
178 const XsdModelGroup::Ptr baseGroup(base->term());
180 if (extensionGroup->compositor() == XsdModelGroup::AllCompositor && baseGroup->compositor() == XsdModelGroup::AllCompositor) {
181 const XsdParticle::List extensionParticles = extensionGroup->particles();
182 const XsdParticle::List baseParticles = baseGroup->particles();
183 for (int i = 0; i < baseParticles.count() && i < extensionParticles.count(); ++i) {
184 if (baseParticles.at(i) != extensionParticles.at(i))
194 QSet<XsdElement::Ptr> collectAllElements(const XsdParticle::Ptr &particle)
196 QSet<XsdElement::Ptr> elements;
198 const XsdTerm::Ptr term(particle->term());
199 if (term->isElement()) {
200 elements.insert(XsdElement::Ptr(term));
201 } else if (term->isModelGroup()) {
202 const XsdModelGroup::Ptr group(term);
204 for (int i = 0; i < group->particles().count(); ++i)
205 elements.unite(collectAllElements(group->particles().at(i)));
211 QSet<XsdElement::Ptr> collectAllElements(const XsdSchema::Ptr &schema)
213 QSet<XsdElement::Ptr> elements;
215 // collect global elements
216 const XsdElement::List elementList = schema->elements();
217 for (int i = 0; i < elementList.count(); ++i)
218 elements.insert(elementList.at(i));
220 // collect all elements from global groups
221 const XsdModelGroup::List groupList = schema->elementGroups();
222 for (int i = 0; i < groupList.count(); ++i) {
223 const XsdModelGroup::Ptr group(groupList.at(i));
225 for (int j = 0; j < group->particles().count(); ++j)
226 elements.unite(collectAllElements(group->particles().at(j)));
229 // collect all elements from complex type definitions
230 SchemaType::List types;
231 types << schema->types() << schema->anonymousTypes();
233 for (int i = 0; i < types.count(); ++i) {
234 if (types.at(i)->isComplexType() && types.at(i)->isDefinedBySchema()) {
235 const XsdComplexType::Ptr complexType(types.at(i));
236 if (complexType->contentType()->particle())
237 elements.unite(collectAllElements(complexType->contentType()->particle()));
244 bool XsdSchemaChecker::elementSequenceAccepted(const XsdModelGroup::Ptr &sequence, const XsdParticle::Ptr &particle) const
246 // @see http://www.w3.org/TR/xmlschema11-1/#cvc-accept
248 if (particle->term()->isWildcard()) { // 1
249 const XsdWildcard::Ptr wildcard(particle->term());
252 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs())
256 if (!particle->maximumOccursUnbounded()) {
257 if ((unsigned int)sequence->particles().count() > particle->maximumOccurs())
262 const XsdParticle::List particles(sequence->particles());
263 for (int i = 0; i < particles.count(); ++i) {
264 if (particles.at(i)->term()->isElement()) {
265 if (!XsdSchemaHelper::wildcardAllowsExpandedName(XsdElement::Ptr(particles.at(i)->term())->name(m_namePool), wildcard, m_namePool))
269 } else if (particle->term()->isElement()) { // 2
270 const XsdElement::Ptr element(particle->term());
273 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs())
277 if (!particle->maximumOccursUnbounded()) {
278 if ((unsigned int)sequence->particles().count() > particle->maximumOccurs())
283 const XsdParticle::List particles(sequence->particles());
284 for (int i = 0; i < particles.count(); ++i) {
285 bool isValid = false;
287 if (particles.at(i)->term()->isElement()) {
288 const XsdElement::Ptr seqElement(particles.at(i)->term());
291 if (element->name(m_namePool) == seqElement->name(m_namePool))
295 if (element->scope() && element->scope()->variety() == XsdElement::Scope::Global) {
296 if (!(element->disallowedSubstitutions() & NamedSchemaComponent::SubstitutionConstraint)) {