1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qxmlschema.h"
43 #include "qxmlschema_p.h"
45 #include <QtCore/QIODevice>
46 #include <QtCore/QUrl>
53 \brief The QXmlSchema class provides loading and validation of a W3C XML Schema.
58 \inmodule QtXmlPatterns
60 The QXmlSchema class loads, compiles and validates W3C XML Schema files
61 that can be used further for validation of XML instance documents via
62 \l{QXmlSchemaValidator}.
64 The following example shows how to load a XML Schema file from the network
65 and test whether it is a valid schema document:
67 \snippet doc/src/snippets/qxmlschema/main.cpp 0
69 \section1 XML Schema Version
71 This class is used to represent schemas that conform to the \l{XML Schema} 1.0
74 \sa QXmlSchemaValidator, {xmlpatterns/schema}{XML Schema Validation Example}
78 Constructs an invalid, empty schema that cannot be used until
81 QXmlSchema::QXmlSchema()
82 : d(new QXmlSchemaPrivate(QXmlNamePool()))
87 Constructs a QXmlSchema that is a copy of \a other. The new
88 instance will share resources with the existing schema
89 to the extent possible.
91 QXmlSchema::QXmlSchema(const QXmlSchema &other)
97 Destroys this QXmlSchema.
99 QXmlSchema::~QXmlSchema()
104 Sets this QXmlSchema to a schema loaded from the \a source
107 If the schema \l {isValid()} {is invalid}, \c{false} is returned
108 and the behavior is undefined.
112 \snippet doc/src/snippets/qxmlschema/main.cpp 0
116 bool QXmlSchema::load(const QUrl &source)
118 d->load(source, QString());
123 Sets this QXmlSchema to a schema read from the \a source
124 device. The device must have been opened with at least
127 \a documentUri represents the schema obtained from the \a source
128 device. It is the base URI of the schema, that is used
129 internally to resolve relative URIs that appear in the schema, and
130 for message reporting.
132 If \a source is \c null or not readable, or if \a documentUri is not
133 a valid URI, behavior is undefined.
135 If the schema \l {isValid()} {is invalid}, \c{false} is returned
136 and the behavior is undefined.
140 \snippet doc/src/snippets/qxmlschema/main.cpp 1
144 bool QXmlSchema::load(QIODevice *source, const QUrl &documentUri)
146 d->load(source, documentUri, QString());
151 Sets this QXmlSchema to a schema read from the \a data
153 \a documentUri represents the schema obtained from the \a data.
154 It is the base URI of the schema, that is used internally to
155 resolve relative URIs that appear in the schema, and
156 for message reporting.
158 If \a documentUri is not a valid URI, behavior is undefined.
161 If the schema \l {isValid()} {is invalid}, \c{false} is returned
162 and the behavior is undefined.
166 \snippet doc/src/snippets/qxmlschema/main.cpp 2
170 bool QXmlSchema::load(const QByteArray &data, const QUrl &documentUri)
172 d->load(data, documentUri, QString());
177 Returns true if this schema is valid. Examples of invalid schemas
178 are ones that contain syntax errors or that do not conform the
179 W3C XML Schema specification.
181 bool QXmlSchema::isValid() const
187 Returns the name pool used by this QXmlSchema for constructing \l
188 {QXmlName} {names}. There is no setter for the name pool, because
189 mixing name pools causes errors due to name confusion.
191 QXmlNamePool QXmlSchema::namePool() const
193 return d->namePool();
197 Returns the document URI of the schema or an empty URI if no
200 QUrl QXmlSchema::documentUri() const
202 return d->documentUri();
206 Changes the \l {QAbstractMessageHandler}{message handler} for this
207 QXmlSchema to \a handler. The schema sends all compile and
208 validation messages to this message handler. QXmlSchema does not take
209 ownership of \a handler.
211 Normally, the default message handler is sufficient. It writes
212 compile and validation messages to \e stderr. The default message
213 handler includes color codes if \e stderr can render colors.
215 When QXmlSchema calls QAbstractMessageHandler::message(),
216 the arguments are as follows:
220 \li message() argument
224 \li Only QtWarningMsg and QtFatalMsg are used. The former
225 identifies a warning, while the latter identifies an error.
227 \li const QString & description
228 \li An XHTML document which is the actual message. It is translated
229 into the current language.
231 \li const QUrl &identifier
232 \li Identifies the error with a URI, where the fragment is
233 the error code, and the rest of the URI is the error namespace.
235 \li const QSourceLocation & sourceLocation
236 \li Identifies where the error occurred.
240 void QXmlSchema::setMessageHandler(QAbstractMessageHandler *handler)
242 d->setMessageHandler(handler);
246 Returns the message handler that handles compile and validation
247 messages for this QXmlSchema.
249 QAbstractMessageHandler *QXmlSchema::messageHandler() const
251 return d->messageHandler();
255 Sets the URI resolver to \a resolver. QXmlSchema does not take
256 ownership of \a resolver.
260 void QXmlSchema::setUriResolver(const QAbstractUriResolver *resolver)
262 d->setUriResolver(resolver);
266 Returns the schema's URI resolver. If no URI resolver has been set,
267 QtXmlPatterns will use the URIs in schemas as they are.
269 The URI resolver provides a level of abstraction, or \e{polymorphic
270 URIs}. A resolver can rewrite \e{logical} URIs to physical ones, or
271 it can translate obsolete or invalid URIs to valid ones.
273 When QtXmlPatterns calls QAbstractUriResolver::resolve() the
274 absolute URI is the URI mandated by the schema specification, and the
275 relative URI is the URI specified by the user.
279 const QAbstractUriResolver *QXmlSchema::uriResolver() const
281 return d->uriResolver();
285 Sets the network manager to \a manager.
286 QXmlSchema does not take ownership of \a manager.
288 \sa networkAccessManager()
290 void QXmlSchema::setNetworkAccessManager(QNetworkAccessManager *manager)
292 d->setNetworkAccessManager(manager);
296 Returns the network manager, or 0 if it has not been set.
298 \sa setNetworkAccessManager()
300 QNetworkAccessManager *QXmlSchema::networkAccessManager() const
302 return d->networkAccessManager();