summaryrefslogtreecommitdiffstats
path: root/src/xmlpatterns/schema/qxsdschemaresolver_p.h
blob: ef0154bdb3d4cad602c1c21ce25e1ec805ef5405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtXmlPatterns module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

#ifndef Patternist_XsdSchemaResolver_H
#define Patternist_XsdSchemaResolver_H

#include "qnamespacesupport_p.h"
#include "qschematype_p.h"
#include "qschematypefactory_p.h"
#include "qxsdalternative_p.h"
#include "qxsdattribute_p.h"
#include "qxsdattributegroup_p.h"
#include "qxsdelement_p.h"
#include "qxsdmodelgroup_p.h"
#include "qxsdnotation_p.h"
#include "qxsdreference_p.h"
#include "qxsdschema_p.h"
#include "qxsdschemachecker_p.h"
#include "qxsdsimpletype_p.h"

#include <QtCore/QExplicitlySharedDataPointer>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

namespace QPatternist
{
    class XsdSchemaContext;
    class XsdSchemaParserContext;

    /**
     * @short Encapsulates the resolving of type/element references in a schema after parsing has finished.
     *
     * This class collects task for resolving types or element references. After the parsing has finished,
     * one can start the resolve process by calling resolve().
     *
     * @ingroup Patternist_schema
     * @author Tobias Koenig <tobias.koenig@nokia.com>
     */
    class XsdSchemaResolver : public QSharedData
    {
        public:
            typedef QExplicitlySharedDataPointer<XsdSchemaResolver> Ptr;

            /**
             * Creates a new schema resolver.
             *
             * @param context The schema context used for error reporting etc..
             * @param parserContext The schema parser context where all objects to resolve belong to.
             */
            XsdSchemaResolver(const QExplicitlySharedDataPointer<XsdSchemaContext> &context, const XsdSchemaParserContext *parserContext);

            /**
             * Destroys the schema resolver.
             */
            ~XsdSchemaResolver();

            /**
             * Starts the resolve process.
             */
            void resolve();

            /**
             * Adds a resolve task for key references.
             *
             * The resolver will try to set the referencedKey property of @p keyRef to the <em>key</em> or <em>unique</em> object
             * of @p element that has the given @p name.
             */
            void addKeyReference(const XsdElement::Ptr &element, const XsdIdentityConstraint::Ptr &keyRef, const QXmlName &name, const QSourceLocation &location);

            /**
             * Adds a resolve task for the base type of restriction of a simple type.
             *
             * The resolver will set the base type of @p simpleType to the type named by @p baseName.
             */
            void addSimpleRestrictionBase(const XsdSimpleType::Ptr &simpleType, const QXmlName &baseName, const QSourceLocation &location);

            /**
             * Removes the resolve task for the base type of restriction of the simple @p type.
             */
            void removeSimpleRestrictionBase(const XsdSimpleType::Ptr &type);

            /**
             * Adds a resolve task for the list type of a simple type.
             *
             * The resolver will set the itemType property of @p simpleType to the type named by @p typeName.
             */
            void addSimpleListType(const XsdSimpleType::Ptr &simpleType, const QXmlName &typeName, const QSourceLocation &location);

            /**
             * Adds a resolve task for the member types of a simple type.
             *
             * The resolver will set the memberTypes property of @p simpleType to the types named by @p typeNames.
             */
            void addSimpleUnionTypes(const XsdSimpleType::Ptr &simpleType, const QList<QXmlName> &typeNames, const QSourceLocation &location);

            /**
             * Adds a resolve task for the type of an element.
             *
             * The resolver will set the type of the @p element to the type named by @p typeName.
             */
            void addElementType(const XsdElement::Ptr &element, const QXmlName &typeName, const QSourceLocation &location);

            /**
             * Adds a resolve task for the base type of a complex type.
             *
             * The resolver will set the base type of @p complexType to the type named by @p baseName.
             */
            void addComplexBaseType(const XsdComplexType::Ptr &complexType, const QXmlName &baseName, const QSourceLocation &location, const XsdFacet::Hash &facets = XsdFacet::Hash());

            /**
             * Removes the resolve task for the base type of the complex @p type.
             */
            void removeComplexBaseType(const XsdComplexType::Ptr &type);

            /**
             * Adds a resolve task for the content type of a complex type.
             *
             * The resolver will set the content type properties for @p complexType based on the
             * given explicit @p content and effective @p mixed value.
             */
            void addComplexContentType(const XsdComplexType::Ptr &complexType, const XsdParticle::Ptr &content, bool mixed);

            /**
             * Adds a resolve task for the type of an attribute.
             *
             * The resolver will set the type of the @p attribute to the type named by @p typeName.
             */
            void addAttributeType(const XsdAttribute::Ptr &attribute, const QXmlName &typeName, const QSourceLocation &location);

            /**
             * Adds a resolve task for the type of an alternative.
             *
             * The resolver will set the type of the @p alternative to the type named by @p typeName.
             */
            void addAlternativeType(const XsdAlternative::Ptr &alternative, const QXmlName &typeName, const QSourceLocation &location);

            /**
             * Adds a resolve task for the type of an alternative.
             *
             * The resolver will set the type of the @p alternative to the type of the @p element after
             * the type of the @p element has been resolved.
             */
            void addAlternativeType(const XsdAlternative::Ptr &alternative, const XsdElement::Ptr &element);

            /**
             * Adds a resolve task for the substituion group affiliations of an element.
             *
             * The resolver will set the substitution group affiliations of the @p element to the
             * top-level element named by @p elementNames.
             */
            void addSubstitutionGroupAffiliation(const XsdElement::Ptr &element, const QList<QXmlName> &elementName, const QSourceLocation &location);

            /**
             * Adds a resolve task for an element that has no type specified, only a substitution group
             * affiliation.
             *
             * The resolver will set the type of the substitution group affiliation as type for the element.
             */
            void addSubstitutionGroupType(const XsdElement::Ptr &element);

            /**
             * Adds the component location hash, so the resolver is able to report meaning full
             * error messages.
             */
            void addComponentLocationHash(const QHash<NamedSchemaComponent::Ptr, QSourceLocation> &hash);

            /**
             * Add a resolve task for enumeration facet values.
             *
             * In case the enumeration is of type QName or NOTATION, we have to resolve the QName later,
             * so we store the namespace bindings together with the facet value here and resolve it as soon as
             * we have all type information available.
             */
            void addEnumerationFacetValue(const AtomicValue::Ptr &facetValue, const NamespaceSupport &namespaceSupport);

            /**
             * Add a check job for redefined groups.
             *
             * When an element group is redefined, we have to check whether the redefined group is a valid
             * restriction of the group it redefines. As we need all type information for that, we keep them
             * here for later checking.
             */
            void addRedefinedGroups(const XsdModelGroup::Ptr &redefinedGroup, const XsdModelGroup::Ptr &group);

            /**
             * Add a check job for redefined attribute groups.
             *
             * When an attribute group is redefined, we have to check whether the redefined group is a valid
             * restriction of the group it redefines. As we need all type information for that, we keep them
             * here for later checking.
             */
            void addRedefinedAttributeGroups(const XsdAttributeGroup::Ptr &redefinedGroup, const XsdAttributeGroup::Ptr &group);

            /**
             * Adds a check for nested <em>all</em> groups.
             */
            void addAllGroupCheck(const XsdReference::Ptr &reference);

            /**
             * Copies the data to resolve to an @p other resolver.
             *
             * @note That functionality is only used by the redefine algorithm in the XsdSchemaParser.
             */
            void copyDataTo(const XsdSchemaResolver::Ptr &other) const;

            /**
             * Returns the to resolve base type name for the given @p type.
             *
             * @note That functionality is only used by the redefine algorithm in the XsdSchemaParser.
             */
            QXmlName baseTypeNameOfType(const SchemaType::Ptr &type) const;

            /**
             * Returns the to resolve type name for the given @p attribute.
             *
             * @note That functionality is only used by the redefine algorithm in the XsdSchemaParser.
             */
            QXmlName typeNameOfAttribute(const XsdAttribute::Ptr &attribute) const;

            /**
             * Sets the defaultOpenContent object from the schema parser.
             */
            void setDefaultOpenContent(const XsdComplexType::OpenContent::Ptr &openContent, bool appliesToEmpty);

        private:
            /**
             * Resolves key references.
             */
            void resolveKeyReferences();

            /**
             * Resolves the base types of simple types derived by restriction.
             */
            void resolveSimpleRestrictionBaseTypes();

            /**
             * Resolves the other properties except the base type
             * of all simple restrictions.
             */
            void resolveSimpleRestrictions();

            /**
             * Resolves the other properties except the base type
             * of the given simple restriction.
             *
             * @param simpleType The restricted type to resolve.
             * @param visitedTypes A set of already resolved types, used for termination of recursion.
             */
            void resolveSimpleRestrictions(const XsdSimpleType::Ptr &simpleType, QSet<XsdSimpleType::Ptr> &visitedTypes);

            /**
             * Resolves the item type property of simple types derived by list.
             */
            void resolveSimpleListType();

            /**
             * Resolves the member types property of simple types derived by union.
             */
            void resolveSimpleUnionTypes();

            /**
             * Resolves element types.
             */
            void resolveElementTypes();

            /**
             * Resolves base type of complex types.
             */
            void resolveComplexBaseTypes();

            /**
             * Resolves the simple content model of a complex type
             * depending on its base type.
             */
            void resolveSimpleContentComplexTypes();

            /**
             * Resolves the complex content model of a complex type
             * depending on its base type.
             */
            void resolveComplexContentComplexTypes();

            /**
             * Resolves the simple content model of a complex type
             * depending on its base type.
             *
             * @param complexType The complex type to resolve.
             * @param visitedTypes A set of already resolved types, used for termination of recursion.
             */
            void resolveSimpleContentComplexTypes(const XsdComplexType::Ptr &complexType, QSet<XsdComplexType::Ptr> &visitedTypes);

            /**
             * Resolves the complex content model of a complex type
             * depending on its base type.
             *
             * @param complexType The complex type to resolve.
             * @param visitedTypes A set of already resolved types, used for termination of recursion.
             */
            void resolveComplexContentComplexTypes(const XsdComplexType::Ptr &complexType, QSet<XsdComplexType::Ptr> &visitedTypes);

            /**
             * Resolves attribute types.
             */
            void resolveAttributeTypes();

            /**
             * Resolves alternative types.
             */
            void resolveAlternativeTypes();

            /**
             * Resolves substitution group affiliations.
             */
            void resolveSubstitutionGroupAffiliations();

            /**
             * Resolves substitution groups.
             */
            void resolveSubstitutionGroups();

            /**
             * Resolves all XsdReferences in the schema by their corresponding XsdElement or XsdModelGroup terms.
             */
            void resolveTermReferences();

            /**
             * Resolves all XsdReferences in the @p particle recursive by their corresponding XsdElement or XsdModelGroup terms.
             */
            void resolveTermReference(const XsdParticle::Ptr &particle, QSet<QXmlName> visitedGroups);

            /**
             * Resolves all XsdAttributeReferences in the schema by their corresponding XsdAttributeUse objects.
             */
            void resolveAttributeTermReferences();

            /**
             * Resolves all XsdAttributeReferences in the list of @p attributeUses by their corresponding XsdAttributeUse objects.
             */
            XsdAttributeUse::List resolveAttributeTermReferences(const XsdAttributeUse::List &attributeUses, XsdWildcard::Ptr &wildcard, QSet<QXmlName> visitedAttributeGroups);

            /**
             * Resolves the attribute inheritance of complex types.
             *
             * @note This method must be called after all base types have been resolved.
             */
            void resolveAttributeInheritance();

            /**
             * Resolves the attribute inheritance of the given complex types.
             *
             * @param complexType The complex type to resolve.
             * @param visitedTypes A set of already resolved types, used for termination of recursion.
             *
             * @note This method must be called after all base types have been resolved.
             */
            void resolveAttributeInheritance(const XsdComplexType::Ptr &complexType, QSet<XsdComplexType::Ptr> &visitedTypes);

            /**
             * Resolves the enumeration facet values for QName and NOTATION based facets.
             */
            void resolveEnumerationFacetValues();

            /**
             * Returns the source location of the given schema @p component or a dummy
             * source location if the component is not found in the component location hash.
             */
            QSourceLocation sourceLocation(const NamedSchemaComponent::Ptr component) const;

            /**
             * Returns the facets that are marked for the given complex @p type with a simple
             * type restriction.
             */
            XsdFacet::Hash complexTypeFacets(const XsdComplexType::Ptr &complexType) const;

            /**
             * Finds the primitive type for the given simple @p type.
             *
             * The type is found by walking up the inheritance tree, until one of the builtin
             * primitive type definitions is reached.
             */
            AnySimpleType::Ptr findPrimitiveType(const AnySimpleType::Ptr &type, QSet<AnySimpleType::Ptr> &visitedTypes);

            /**
             * Checks the redefined groups.
             */
            void checkRedefinedGroups();

            /**
             * Checks the redefined attribute groups.
             */
            void checkRedefinedAttributeGroups();

            class KeyReference
            {
                public:
                    XsdElement::Ptr element;
                    XsdIdentityConstraint::Ptr keyRef;
                    QXmlName reference;
                    QSourceLocation location;
            };

            class SimpleRestrictionBase
            {
                public:
                    XsdSimpleType::Ptr simpleType;
                    QXmlName baseName;
                    QSourceLocation location;
            };

            class SimpleListType
            {
                public:
                    XsdSimpleType::Ptr simpleType;
                    QXmlName typeName;
                    QSourceLocation location;
            };

            class SimpleUnionType
            {
                public:
                    XsdSimpleType::Ptr simpleType;
                    QList<QXmlName> typeNames;
                    QSourceLocation location;
            };

            class ElementType
            {
                public:
                    XsdElement::Ptr element;
                    QXmlName typeName;
                    QSourceLocation location;
            };

            class ComplexBaseType
            {
                public:
                    XsdComplexType::Ptr complexType;
                    QXmlName baseName;
                    QSourceLocation location;
                    XsdFacet::Hash facets;
            };

            class ComplexContentType
            {
                public:
                    XsdComplexType::Ptr complexType;
                    XsdParticle::Ptr explicitContent;
                    bool effectiveMixed;
            };

            class AttributeType
            {
                public:
                    XsdAttribute::Ptr attribute;
                    QXmlName typeName;
                    QSourceLocation location;
            };

            class AlternativeType
            {
                public:
                    XsdAlternative::Ptr alternative;
                    QXmlName typeName;
                    QSourceLocation location;
            };

            class AlternativeTypeElement
            {
                public:
                    XsdAlternative::Ptr alternative;
                    XsdElement::Ptr element;
            };

            class SubstitutionGroupAffiliation
            {
                public:
                    XsdElement::Ptr element;
                    QList<QXmlName> elementNames;
                    QSourceLocation location;
            };

            class RedefinedGroups
            {
                public:
                    XsdModelGroup::Ptr redefinedGroup;
                    XsdModelGroup::Ptr group;
            };

            class RedefinedAttributeGroups
            {
                public:
                    XsdAttributeGroup::Ptr redefinedGroup;
                    XsdAttributeGroup::Ptr group;
            };

            QVector<KeyReference>                                m_keyReferences;
            QVector<SimpleRestrictionBase>                       m_simpleRestrictionBases;
            QVector<SimpleListType>                              m_simpleListTypes;
            QVector<SimpleUnionType>                             m_simpleUnionTypes;
            QVector<ElementType>                                 m_elementTypes;
            QVector<ComplexBaseType>                             m_complexBaseTypes;
            QVector<ComplexContentType>                          m_complexContentTypes;
            QVector<AttributeType>                               m_attributeTypes;
            QVector<AlternativeType>                             m_alternativeTypes;
            QVector<AlternativeTypeElement>                      m_alternativeTypeElements;
            QVector<SubstitutionGroupAffiliation>                m_substitutionGroupAffiliations;
            QVector<XsdElement::Ptr>                             m_substitutionGroupTypes;
            QVector<RedefinedGroups>                             m_redefinedGroups;
            QVector<RedefinedAttributeGroups>                    m_redefinedAttributeGroups;
            QHash<AtomicValue::Ptr, NamespaceSupport>            m_enumerationFacetValues;
            QSet<XsdReference::Ptr>                              m_allGroups;

            QExplicitlySharedDataPointer<XsdSchemaContext>       m_context;
            QExplicitlySharedDataPointer<XsdSchemaChecker>       m_checker;
            NamePool::Ptr                                        m_namePool;
            XsdSchema::Ptr                                       m_schema;
            QHash<NamedSchemaComponent::Ptr, QSourceLocation>    m_componentLocationHash;
            XsdComplexType::OpenContent::Ptr                     m_defaultOpenContent;
            bool                                                 m_defaultOpenContentAppliesToEmpty;
            SchemaType::List                                     m_predefinedSchemaTypes;
    };
}

QT_END_NAMESPACE

QT_END_HEADER

#endif