1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40QT_BEGIN_NAMESPACE
41
42using namespace QPatternist;
43
44bool XsdSchemaChecker::hasDuplicatedAttributeUses(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const
45{
46 const int length = list.count();
47
48 for (int i = 0; i < length; ++i) {
49 for (int j = 0; j < length; ++j) {
50 if (i == j)
51 continue;
52
53 if (list.at(i)->attribute()->name(namePool: m_namePool) == list.at(i: j)->attribute()->name(namePool: m_namePool)) {
54 conflictingAttribute = list.at(i)->attribute();
55 return true;
56 }
57 }
58 }
59
60 return false;
61}
62
63bool XsdSchemaChecker::hasMultipleIDAttributeUses(const XsdAttributeUse::List &list) const
64{
65 const int length = list.count();
66
67 bool hasIdDerivedAttribute = false;
68 for (int i = 0; i < length; ++i) {
69 if (BuiltinTypes::xsID->wxsTypeMatches(other: list.at(i)->attribute()->type())) {
70 if (hasIdDerivedAttribute)
71 return true;
72 else
73 hasIdDerivedAttribute = true;
74 }
75 }
76
77 return false;
78}
79
80bool XsdSchemaChecker::hasConstraintIDAttributeUse(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const
81{
82 const int length = list.count();
83
84 for (int i = 0; i < length; ++i) {
85 const XsdAttributeUse::Ptr attributeUse(list.at(i));
86 if (BuiltinTypes::xsID->wxsTypeMatches(other: attributeUse->attribute()->type())) {
87 if (attributeUse->valueConstraint()) {
88 conflictingAttribute = attributeUse->attribute();
89 return true;
90 }
91 }
92 }
93
94 return false;
95}
96
97bool XsdSchemaChecker::particleEqualsRecursively(const XsdParticle::Ptr &particle, const XsdParticle::Ptr &otherParticle) const
98{
99 // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend
100 //TODO: find out what 'properties' of a particle should be checked here...
101
102 if (particle->minimumOccurs() != otherParticle->minimumOccurs())
103 return false;
104
105 if (particle->maximumOccursUnbounded() != otherParticle->maximumOccursUnbounded())
106 return false;
107
108 if (particle->maximumOccurs() != otherParticle->maximumOccurs())
109 return false;
110
111 const XsdTerm::Ptr term = particle->term();
112 const XsdTerm::Ptr otherTerm = otherParticle->term();
113
114 if (term->isElement() && !(otherTerm->isElement()))
115 return false;
116
117 if (term->isModelGroup() && !(otherTerm->isModelGroup()))
118 return false;
119
120 if (term->isWildcard() && !(otherTerm->isWildcard()))
121 return false;
122
123 if (term->isElement()) {
124 const XsdElement::Ptr element = term;
125 const XsdElement::Ptr otherElement = otherTerm;
126
127 if (element->name(namePool: m_namePool) != otherElement->name(namePool: m_namePool))
128 return false;
129
130 if (element->type()->name(np: m_namePool) != otherElement->type()->name(np: m_namePool))
131 return false;
132 }
133
134 if (term->isModelGroup()) {
135 const XsdModelGroup::Ptr group = term;
136 const XsdModelGroup::Ptr otherGroup = otherTerm;
137
138 if (group->particles().count() != otherGroup->particles().count())
139 return false;
140
141 for (int i = 0; i < group->particles().count(); ++i) {
142 if (!particleEqualsRecursively(particle: group->particles().at(i), otherParticle: otherGroup->particles().at(i)))
143 return false;
144 }
145 }
146
147 if (term->isWildcard()) {
148 }
149
150 return true;
151}
152
153bool XsdSchemaChecker::isValidParticleExtension(const XsdParticle::Ptr &extension, const XsdParticle::Ptr &base) const
154{
155 // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend
156
157 // 1
158 if (extension == base)
159 return true;
160
161 // 2
162 if (extension->minimumOccurs() == 1 && extension->maximumOccurs() == 1 && extension->maximumOccursUnbounded() == false) {
163 if (extension->term()->isModelGroup()) {
164 const XsdModelGroup::Ptr modelGroup = extension->term();
165 if (modelGroup->compositor() == XsdModelGroup::SequenceCompositor) {
166 if (particleEqualsRecursively(particle: modelGroup->particles().first(), otherParticle: base))
167 return true;
168 }
169 }
170 }
171
172 // 3
173 if (extension->minimumOccurs() == base->minimumOccurs()) { // 3.1
174 if (extension->term()->isModelGroup() && base->term()->isModelGroup()) {
175 const XsdModelGroup::Ptr extensionGroup(extension->term());
176 const XsdModelGroup::Ptr baseGroup(base->term());
177
178 if (extensionGroup->compositor() == XsdModelGroup::AllCompositor && baseGroup->compositor() == XsdModelGroup::AllCompositor) {
179 const XsdParticle::List extensionParticles = extensionGroup->particles();
180 const XsdParticle::List baseParticles = baseGroup->particles();
181 for (int i = 0; i < baseParticles.count() && i < extensionParticles.count(); ++i) {
182 if (baseParticles.at(i) != extensionParticles.at(i))
183 return false;
184 }
185 }
186 }
187 }
188
189 return false;
190}
191
192QSet<XsdElement::Ptr> collectAllElements(const XsdParticle::Ptr &particle)
193{
194 QSet<XsdElement::Ptr> elements;
195
196 const XsdTerm::Ptr term(particle->term());
197 if (term->isElement()) {
198 elements.insert(value: XsdElement::Ptr(term));
199 } else if (term->isModelGroup()) {
200 const XsdModelGroup::Ptr group(term);
201
202 for (int i = 0; i < group->particles().count(); ++i)
203 elements.unite(other: collectAllElements(particle: group->particles().at(i)));
204 }
205
206 return elements;
207}
208
209QSet<XsdElement::Ptr> collectAllElements(const XsdSchema::Ptr &schema)
210{
211 QSet<XsdElement::Ptr> elements;
212
213 // collect global elements
214 const XsdElement::List elementList = schema->elements();
215 for (int i = 0; i < elementList.count(); ++i)
216 elements.insert(value: elementList.at(i));
217
218 // collect all elements from global groups
219 const XsdModelGroup::List groupList = schema->elementGroups();
220 for (int i = 0; i < groupList.count(); ++i) {
221 const XsdModelGroup::Ptr group(groupList.at(i));
222
223 for (int j = 0; j < group->particles().count(); ++j)
224 elements.unite(other: collectAllElements(particle: group->particles().at(i: j)));
225 }
226
227 // collect all elements from complex type definitions
228 SchemaType::List types;
229 types << schema->types() << schema->anonymousTypes();
230
231 for (int i = 0; i < types.count(); ++i) {
232 if (types.at(i)->isComplexType() && types.at(i)->isDefinedBySchema()) {
233 const XsdComplexType::Ptr complexType(types.at(i));
234 if (complexType->contentType()->particle())
235 elements.unite(other: collectAllElements(particle: complexType->contentType()->particle()));
236 }
237 }
238
239 return elements;
240}
241
242bool XsdSchemaChecker::elementSequenceAccepted(const XsdModelGroup::Ptr &sequence, const XsdParticle::Ptr &particle) const
243{
244 // @see http://www.w3.org/TR/xmlschema11-1/#cvc-accept
245
246 if (particle->term()->isWildcard()) { // 1
247 const XsdWildcard::Ptr wildcard(particle->term());
248
249 // 1.1
250 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs())
251 return false;
252
253 // 1.2
254 if (!particle->maximumOccursUnbounded()) {
255 if ((unsigned int)sequence->particles().count() > particle->maximumOccurs())
256 return false;
257 }
258
259 // 1.3
260 const XsdParticle::List particles(sequence->particles());
261 for (int i = 0; i < particles.count(); ++i) {
262 if (particles.at(i)->term()->isElement()) {
263 if (!XsdSchemaHelper::wildcardAllowsExpandedName(name: XsdElement::Ptr(particles.at(i)->term())->name(namePool: m_namePool), wildcard, namePool: m_namePool))
264 return false;
265 }
266 }
267 } else if (particle->term()->isElement()) { // 2
268 const XsdElement::Ptr element(particle->term());
269
270 // 2.1
271 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs())
272 return false;
273
274 // 2.2
275 if (!particle->maximumOccursUnbounded()) {
276 if ((unsigned int)sequence->particles().count() > particle->maximumOccurs())
277 return false;
278 }
279
280 // 2.3
281 const XsdParticle::List particles(sequence->particles());
282 for (int i = 0; i < particles.count(); ++i) {
283 bool isValid = false;
284 Q_UNUSED(isValid)
285 if (particles.at(i)->term()->isElement()) {
286 const XsdElement::Ptr seqElement(particles.at(i)->term());
287
288 // 2.3.1
289 if (element->name(namePool: m_namePool) == seqElement->name(namePool: m_namePool))
290 isValid = true;
291
292 // 2.3.2
293 if (element->scope() && element->scope()->variety() == XsdElement::Scope::Global) {
294 if (!(element->disallowedSubstitutions() & NamedSchemaComponent::SubstitutionConstraint)) {
295 //TODO: continue
296 }
297 }
298 }
299 }
300 }
301
302 return true;
303}
304
305QT_END_NAMESPACE
306

source code of qtxmlpatterns/src/xmlpatterns/schema/qxsdschemachecker_helper.cpp