| 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 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_XsdSchemaChecker_H |
| 51 | #define Patternist_XsdSchemaChecker_H |
| 52 | |
| 53 | #include <private/qschematype_p.h> |
| 54 | #include <private/qxsdattribute_p.h> |
| 55 | #include <private/qxsdattributegroup_p.h> |
| 56 | #include <private/qxsdelement_p.h> |
| 57 | #include <private/qxsdmodelgroup_p.h> |
| 58 | #include <private/qxsdnotation_p.h> |
| 59 | #include <private/qxsdschema_p.h> |
| 60 | #include <private/qxsdsimpletype_p.h> |
| 61 | |
| 62 | #include <QtCore/QExplicitlySharedDataPointer> |
| 63 | |
| 64 | QT_BEGIN_NAMESPACE |
| 65 | |
| 66 | namespace QPatternist |
| 67 | { |
| 68 | class XsdSchemaContext; |
| 69 | class XsdSchemaParserContext; |
| 70 | |
| 71 | /** |
| 72 | * @short Encapsulates the checking of schema valitity after reference resolving has finished. |
| 73 | * |
| 74 | * @ingroup Patternist_schema |
| 75 | * @author Tobias Koenig <tobias.koenig@nokia.com> |
| 76 | */ |
| 77 | class XsdSchemaChecker : public QSharedData |
| 78 | { |
| 79 | public: |
| 80 | typedef QExplicitlySharedDataPointer<XsdSchemaChecker> Ptr; |
| 81 | |
| 82 | /** |
| 83 | * Creates a new schema checker. |
| 84 | * |
| 85 | * @param context The context that is used for customization. |
| 86 | * @param parserContext The context that contains all the data structures. |
| 87 | */ |
| 88 | XsdSchemaChecker(const QExplicitlySharedDataPointer<XsdSchemaContext> &context, const XsdSchemaParserContext *parserContext); |
| 89 | |
| 90 | /** |
| 91 | * Destroys the schema checker. |
| 92 | */ |
| 93 | ~XsdSchemaChecker(); |
| 94 | |
| 95 | /** |
| 96 | * Starts a basic check process. |
| 97 | * |
| 98 | * This check only validates the basic super type inheritance |
| 99 | * of simple and complex types. |
| 100 | */ |
| 101 | void basicCheck(); |
| 102 | |
| 103 | /** |
| 104 | * Starts the real check process. |
| 105 | */ |
| 106 | void check(); |
| 107 | |
| 108 | /** |
| 109 | * Checks the constraining facets of all global and anonymous simple types for validity. |
| 110 | */ |
| 111 | void checkConstrainingFacets(); |
| 112 | |
| 113 | /** |
| 114 | * Adds the component location hash, so the checker is able to report meaning full |
| 115 | * error messages. |
| 116 | */ |
| 117 | void addComponentLocationHash(const QHash<NamedSchemaComponent::Ptr, QSourceLocation> &hash); |
| 118 | |
| 119 | private: |
| 120 | void checkSimpleRestrictionBaseType(); |
| 121 | |
| 122 | /** |
| 123 | * Checks that no simple or complex type inherits itself. |
| 124 | */ |
| 125 | void checkBasicCircularInheritances(); |
| 126 | |
| 127 | /** |
| 128 | * Checks the advanced circular inheritance. |
| 129 | */ |
| 130 | void checkCircularInheritances(); |
| 131 | |
| 132 | /** |
| 133 | * Checks for inheritance restrictions given by final or finalDefault |
| 134 | * attributes. |
| 135 | */ |
| 136 | void checkInheritanceRestrictions(); |
| 137 | |
| 138 | /** |
| 139 | * Checks for various constraints for simple types defined by schema. |
| 140 | */ |
| 141 | void checkBasicSimpleTypeConstraints(); |
| 142 | void checkSimpleTypeConstraints(); |
| 143 | |
| 144 | /** |
| 145 | * Checks for various constraints for complex types defined by schema. |
| 146 | */ |
| 147 | void checkBasicComplexTypeConstraints(); |
| 148 | void checkComplexTypeConstraints(); |
| 149 | |
| 150 | /** |
| 151 | * Checks for list and union derivation restrictions given by final or finalDefault |
| 152 | * attributes. |
| 153 | */ |
| 154 | void checkSimpleDerivationRestrictions(); |
| 155 | |
| 156 | /** |
| 157 | * Checks the set of constraining @p facets that belongs to @p simpleType for validity. |
| 158 | */ |
| 159 | void checkConstrainingFacets(const XsdFacet::Hash &facets, const XsdSimpleType::Ptr &simpleType); |
| 160 | |
| 161 | /** |
| 162 | * Checks for duplicated attribute uses (attributes with the same name) inside a complex type. |
| 163 | */ |
| 164 | void checkDuplicatedAttributeUses(); |
| 165 | |
| 166 | /** |
| 167 | * Check the element constraints. |
| 168 | */ |
| 169 | void checkElementConstraints(); |
| 170 | |
| 171 | /** |
| 172 | * Check the attribute constraints. |
| 173 | */ |
| 174 | void checkAttributeConstraints(); |
| 175 | |
| 176 | /** |
| 177 | * Check the attribute use constraints. |
| 178 | */ |
| 179 | void checkAttributeUseConstraints(); |
| 180 | |
| 181 | /** |
| 182 | * A map used to find duplicated elements inside a model group. |
| 183 | */ |
| 184 | typedef QHash<QXmlName, SchemaType::Ptr> DuplicatedElementMap; |
| 185 | |
| 186 | /** |
| 187 | * A map used to find duplicated wildcards inside a model group. |
| 188 | */ |
| 189 | typedef QHash<XsdWildcard::NamespaceConstraint::Variety, XsdWildcard::Ptr> DuplicatedWildcardMap; |
| 190 | |
| 191 | /** |
| 192 | * Check for duplicated elements and element wildcards in all complex type particles. |
| 193 | */ |
| 194 | void checkElementDuplicates(); |
| 195 | |
| 196 | /** |
| 197 | * Check for duplicated elements and element wildcards in the given @p particle. |
| 198 | * |
| 199 | * @param particle The particle to check. |
| 200 | * @param elementMap A map to find the duplicated elements. |
| 201 | * @param wildcardMap A map to find the duplicated element wildcards. |
| 202 | */ |
| 203 | void checkElementDuplicates(const XsdParticle::Ptr &particle, DuplicatedElementMap &elementMap, DuplicatedWildcardMap &wildcardMap); |
| 204 | |
| 205 | /** |
| 206 | * Setup fast lookup list for allowed facets of atomic simple types. |
| 207 | */ |
| 208 | void setupAllowedAtomicFacets(); |
| 209 | |
| 210 | /** |
| 211 | * Returns the source location of the given schema @p component or a dummy |
| 212 | * source location if the component is not found in the component location hash. |
| 213 | */ |
| 214 | QSourceLocation sourceLocation(const NamedSchemaComponent::Ptr &component) const; |
| 215 | |
| 216 | /** |
| 217 | * Returns the source location of the given schema @p type or a dummy |
| 218 | * source location if the type is not found in the component location hash. |
| 219 | */ |
| 220 | QSourceLocation sourceLocationForType(const SchemaType::Ptr &type) const; |
| 221 | |
| 222 | /** |
| 223 | * Checks that the string @p value is valid according the value space of @p type |
| 224 | * for the given @p component. |
| 225 | */ |
| 226 | bool isValidValue(const QString &value, const AnySimpleType::Ptr &type, QString &errorMsg) const; |
| 227 | |
| 228 | /** |
| 229 | * Returns the list of facets for the given @p type. |
| 230 | */ |
| 231 | XsdFacet::Hash facetsForType(const SchemaType::Ptr &type) const; |
| 232 | |
| 233 | /** |
| 234 | * Returns whether the given @p list of attribute uses contains two (or more) attribute |
| 235 | * uses that point to attributes with the same name. @p conflictingAttribute |
| 236 | * will contain the conflicting attribute in that case. |
| 237 | */ |
| 238 | bool hasDuplicatedAttributeUses(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const; |
| 239 | |
| 240 | /** |
| 241 | * Returns whether the given @p list of attribute uses contains two (or more) attribute |
| 242 | * uses that have a type inherited by xs:ID. |
| 243 | */ |
| 244 | bool hasMultipleIDAttributeUses(const XsdAttributeUse::List &list) const; |
| 245 | |
| 246 | /** |
| 247 | * Returns whether the given @p list of attribute uses contains an attribute |
| 248 | * uses that has a type inherited by xs:ID with a value constraint. @p conflictingAttribute |
| 249 | * will contain the conflicting attribute in that case. |
| 250 | */ |
| 251 | bool hasConstraintIDAttributeUse(const XsdAttributeUse::List &list, XsdAttribute::Ptr &conflictingAttribute) const; |
| 252 | |
| 253 | /** |
| 254 | * Checks whether the @p particle equals the @p otherParticle recursively. |
| 255 | */ |
| 256 | bool particleEqualsRecursively(const XsdParticle::Ptr &particle, const XsdParticle::Ptr &otherParticle) const; |
| 257 | |
| 258 | /** |
| 259 | * Checks whether the @p extension particle is a valid extension of the @p base particle. |
| 260 | */ |
| 261 | bool isValidParticleExtension(const XsdParticle::Ptr &extension, const XsdParticle::Ptr &base) const; |
| 262 | |
| 263 | /** |
| 264 | * Checks whether the @p sequence of elements is accepted by the given @p particle. |
| 265 | */ |
| 266 | bool elementSequenceAccepted(const XsdModelGroup::Ptr &sequence, const XsdParticle::Ptr &particle) const; |
| 267 | |
| 268 | QExplicitlySharedDataPointer<XsdSchemaContext> m_context; |
| 269 | NamePool::Ptr m_namePool; |
| 270 | XsdSchema::Ptr m_schema; |
| 271 | QHash<QXmlName, QSet<XsdFacet::Type> > m_allowedAtomicFacets; |
| 272 | QHash<NamedSchemaComponent::Ptr, QSourceLocation> m_componentLocationHash; |
| 273 | }; |
| 274 | } |
| 275 | |
| 276 | QT_END_NAMESPACE |
| 277 | |
| 278 | #endif |
| 279 | |