1 /****************************************************************************
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
46 // This file is not part of the Qt API. It exists purely as an
47 // implementation detail. This header file may change from version to
48 // version without notice, or even be removed.
52 #ifndef Patternist_XsdValidatingInstanceReader_H
53 #define Patternist_XsdValidatingInstanceReader_H
55 #include <private/qxsdidchelper_p.h>
56 #include <private/qxsdinstancereader_p.h>
57 #include <private/qxsdstatemachine_p.h>
58 #include <private/qxsdvalidatedxmlnodemodel_p.h>
60 #include <QtCore/QStack>
71 * @short The validating schema instance reader.
73 * This class reads in a xml instance document from a QAbstractXmlNodeModel and
74 * validates it against a given xml schema.
76 * @ingroup Patternist_schema
77 * @author Tobias Koenig <tobias.koenig@nokia.com>
79 class XsdValidatingInstanceReader : public XsdInstanceReader
82 typedef QExplicitlySharedDataPointer<XsdValidatingInstanceReader> Ptr;
85 * Creates a new validating instance reader that reads the data from
88 * @param model The model the data shall be read from.
89 * @param documentUri The uri of the document the model is from.
90 * @param context The context that is used to report errors etc.
92 XsdValidatingInstanceReader(XsdValidatedXmlNodeModel *model, const QUrl &documentUri, const XsdSchemaContext::Ptr &context);
95 * Adds a new @p schema to the pool of schemas that shall be used
97 * The schema is located at the given @p url.
99 void addSchema(const XsdSchema::Ptr &schema, const QUrl &url);
102 * Reads and validates the instance document.
108 * Loads a schema with the given @p targetNamespace from the given @p location
109 * and adds it to the pool of schemas that are used for validation.
111 * This method is used to load schemas defined in the xsi:schemaLocation or
112 * xsi:noNamespaceSchemaLocation attributes in the instance document.
114 bool loadSchema(const QString &targetNamespace, const QUrl &location);
117 * Reports an error via the report context.
119 void error(const QString &msg) const;
122 * Validates the current element tag of the instance document.
124 * @param hasStateMachine Used to remember whether this element represents the start tag
125 * of a complex type and therefor pushes a new state machine on the stack.
126 * @param element Used to remember which element has been validated in this step.
128 bool validate(bool &hasStateMachine, XsdElement::Ptr &element);
131 * Validates the current tag of the instance document against the given element @p declaration.
133 * @param declaration The element declaration to validate against.
134 * @param hasStateMachine Used to remember whether this element represents the start tag
135 * of a complex type and therefor pushes a new state machine on the stack.
137 bool validateElement(const XsdElement::Ptr &declaration, bool &hasStateMachine);
140 * Validates the current tag of the instance document against the given @p type of the element @p declaration.
142 * @param declaration The element declaration to validate against.
143 * @param type The type to validate against.
144 * @param isNilled Defines whether the element is nilled by the instance document.
145 * @param hasStateMachine Used to remember whether this element represents the start tag
146 * of a complex type and therefor pushes a new state machine on the stack.
148 * @note The @p type can differ from the element @p declaration type if the instance document has defined
149 * it via xsi:type attribute.
151 bool validateElementType(const XsdElement::Ptr &declaration, const SchemaType::Ptr &type, bool isNilled, bool &hasStateMachine);
154 * Validates the current tag of the instance document against the given simple @p type of the element @p declaration.
156 * @param declaration The element declaration to validate against.
157 * @param type The type to validate against.
158 * @param isNilled Defines whether the element is nilled by the instance document.
160 * @note The @p type can differ from the element @p declaration type if the instance document has defined
161 * it via xsi:type attribute.
163 bool validateElementSimpleType(const XsdElement::Ptr &declaration, const SchemaType::Ptr &type, bool isNilled);
166 * Validates the current tag of the instance document against the given complex @p type of the element @p declaration.
168 * @param declaration The element declaration to validate against.
169 * @param type The type to validate against.
170 * @param isNilled Defines whether the element is nilled by the instance document.
171 * @param hasStateMachine Used to remember whether this element represents the start tag
172 * of a complex type and therefor pushes a new state machine on the stack.
174 * @note The @p type can differ from the element @p declaration type if the instance document has defined
175 * it via xsi:type attribute.
177 bool validateElementComplexType(const XsdElement::Ptr &declaration, const SchemaType::Ptr &type, bool isNilled, bool &hasStateMachine);
180 * Validates the given @p value against the attribute use @p declaration.
182 bool validateAttribute(const XsdAttributeUse::Ptr &declaration, const QString &value);
185 * Validates the given @p value against the attribute @p declaration.
187 bool validateAttribute(const XsdAttribute::Ptr &declaration, const QString &value);
190 * Validates the given @p attributeName against the @p wildcard.
192 bool validateAttributeWildcard(const QXmlName &attributeName, const XsdWildcard::Ptr &wildcard);
195 * Validates the identity constraints of an @p element.
197 bool validateIdentityConstraint(const XsdElement::Ptr &element, const QXmlItem ¤tItem);
200 * Validates the <em>unique</em> identity @p constraint of the @p element.
202 bool validateUniqueIdentityConstraint(const XsdElement::Ptr &element, const XsdIdentityConstraint::Ptr &constraint, const TargetNode::Set &qualifiedNodeSet);
205 * Validates the <em>key</em> identity @p constraint of the @p element.
207 bool validateKeyIdentityConstraint(const XsdElement::Ptr &element, const XsdIdentityConstraint::Ptr &constraint, const TargetNode::Set &targetNodeSet, const TargetNode::Set &qualifiedNodeSet);
210 * Validates the <em>keyref</em> identity @p constraint of the @p element.
212 bool validateKeyRefIdentityConstraint(const XsdElement::Ptr &element, const XsdIdentityConstraint::Ptr &constraint, const TargetNode::Set &qualifiedNodeSet);
215 * Selects two sets of nodes that match the given identity @p constraint.
217 * @param element The element the identity constraint belongs to.
218 * @param currentItem The current element that will be used as focus for the XQuery.
219 * @param constraint The constraint (selector and fields) that describe the two sets.
220 * @param targetNodeSet The target node set as defined by the schema specification.
221 * @param qualifiedNodeSet The qualified node set as defined by the schema specification.
223 bool selectNodeSets(const XsdElement::Ptr &element, const QXmlItem ¤tItem, const XsdIdentityConstraint::Ptr &constraint, TargetNode::Set &targetNodeSet, TargetNode::Set &qualifiedNodeSet);
226 * Creates an QXmlQuery object with the defined @p namespaceBindings that has the @p contextNode as focus
227 * and will execute @p query.
229 QXmlQuery createXQuery(const QList<QXmlName> &namespaceBindings, const QXmlItem &contextNode, const QString &query) const;
232 * Returns the element declaration with the given @p name from the pool of all schemas.
234 XsdElement::Ptr elementByName(const QXmlName &name) const;
237 * Returns the attribute declaration with the given @p name from the pool of all schemas.
239 XsdAttribute::Ptr attributeByName(const QXmlName &name) const;
242 * Returns the type declaration with the given @p name from the pool of all schemas.
244 SchemaType::Ptr typeByName(const QXmlName &name) const;
247 * Adds the ID/IDREF binding to the validated model and checks for duplicates.
249 void addIdIdRefBinding(const QString &id, const NamedSchemaComponent::Ptr &binding);
252 * Helper method that reads an attribute of type xs:QName and does
255 QString qNameAttribute(const QXmlName &attributeName);
258 * Returns the xs:anyType that is used to build up the state machine.
259 * We need that as the BuiltinTypes::xsAnyType is not a XsdComplexType.
261 XsdComplexType::Ptr anyType();
264 * Helper method that creates a state machine for the given @p particle
265 * and pushes it on the state machine stack.
267 void createAndPushStateMachine(const XsdParticle::Ptr &particle);
269 typedef QHash<QUrl, QStringList> MergedSchemas;
270 typedef QHashIterator<QUrl, QStringList> MergedSchemasIterator;
272 XsdValidatedXmlNodeModel::Ptr m_model;
273 MergedSchemas m_mergedSchemas;
274 XsdSchema::Ptr m_schema;
275 const NamePool::Ptr m_namePool;
276 const QXmlName m_xsiNilName;
277 const QXmlName m_xsiTypeName;
278 const QXmlName m_xsiSchemaLocationName;
279 const QXmlName m_xsiNoNamespaceSchemaLocationName;
281 QStack<XsdStateMachine<XsdTerm::Ptr> > m_stateMachines;
283 XsdComplexType::Ptr m_anyType;
284 QSet<QString> m_processedNamespaces;
285 QSet<QString> m_processedSchemaLocations;
286 QSet<QString> m_idRefs;
287 QHash<QXmlName, TargetNode::Set> m_idcKeys;
288 SchemaType::Ptr m_idRefsType;