| 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 | #include "qxmlschema.h" |
| 41 | #include "qxmlschema_p.h" |
| 42 | |
| 43 | #include <QtCore/QIODevice> |
| 44 | #include <QtCore/QUrl> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | /*! |
| 49 | \class QXmlSchema |
| 50 | |
| 51 | \brief The QXmlSchema class provides loading and validation of a W3C XML Schema. |
| 52 | |
| 53 | \reentrant |
| 54 | \since 4.6 |
| 55 | \ingroup xml-tools |
| 56 | \inmodule QtXmlPatterns |
| 57 | |
| 58 | The QXmlSchema class loads, compiles and validates W3C XML Schema files |
| 59 | that can be used further for validation of XML instance documents via |
| 60 | \l{QXmlSchemaValidator}. |
| 61 | |
| 62 | The following example shows how to load a XML Schema file from the network |
| 63 | and test whether it is a valid schema document: |
| 64 | |
| 65 | \snippet qxmlschema/main.cpp 0 |
| 66 | |
| 67 | \section1 XML Schema Version |
| 68 | |
| 69 | This class is used to represent schemas that conform to the \l{XML Schema} 1.0 |
| 70 | specification. |
| 71 | |
| 72 | \sa QXmlSchemaValidator, {schema}{XML Schema Validation Example} |
| 73 | */ |
| 74 | |
| 75 | /*! |
| 76 | Constructs an invalid, empty schema that cannot be used until |
| 77 | load() is called. |
| 78 | */ |
| 79 | QXmlSchema::QXmlSchema() |
| 80 | : d(new QXmlSchemaPrivate(QXmlNamePool())) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | Constructs a QXmlSchema that is a copy of \a other. The new |
| 86 | instance will share resources with the existing schema |
| 87 | to the extent possible. |
| 88 | */ |
| 89 | QXmlSchema::QXmlSchema(const QXmlSchema &other) |
| 90 | : d(other.d) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | \since 5.4 |
| 96 | Copies the resources of \a other into this instance, sharing |
| 97 | them to the extent possible. |
| 98 | */ |
| 99 | QXmlSchema &QXmlSchema::operator =(const QXmlSchema &other) |
| 100 | { |
| 101 | d = other.d; |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | Destroys this QXmlSchema. |
| 107 | */ |
| 108 | QXmlSchema::~QXmlSchema() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | /*! |
| 113 | Sets this QXmlSchema to a schema loaded from the \a source |
| 114 | URI. |
| 115 | |
| 116 | If the schema \l {isValid()} {is invalid}, \c{false} is returned |
| 117 | and the behavior is undefined. |
| 118 | |
| 119 | Example: |
| 120 | |
| 121 | \snippet qxmlschema/main.cpp 0 |
| 122 | |
| 123 | \sa isValid() |
| 124 | */ |
| 125 | bool QXmlSchema::load(const QUrl &source) |
| 126 | { |
| 127 | d->load(source, targetNamespace: QString()); |
| 128 | return d->isValid(); |
| 129 | } |
| 130 | |
| 131 | /*! |
| 132 | Sets this QXmlSchema to a schema read from the \a source |
| 133 | device. The device must have been opened with at least |
| 134 | QIODevice::ReadOnly. |
| 135 | |
| 136 | \a documentUri represents the schema obtained from the \a source |
| 137 | device. It is the base URI of the schema, that is used |
| 138 | internally to resolve relative URIs that appear in the schema, and |
| 139 | for message reporting. |
| 140 | |
| 141 | If \a source is \c null or not readable, or if \a documentUri is not |
| 142 | a valid URI, behavior is undefined. |
| 143 | |
| 144 | If the schema \l {isValid()} {is invalid}, \c{false} is returned |
| 145 | and the behavior is undefined. |
| 146 | |
| 147 | Example: |
| 148 | |
| 149 | \snippet qxmlschema/main.cpp 1 |
| 150 | |
| 151 | \sa isValid() |
| 152 | */ |
| 153 | bool QXmlSchema::load(QIODevice *source, const QUrl &documentUri) |
| 154 | { |
| 155 | d->load(source, documentUri, targetNamespace: QString()); |
| 156 | return d->isValid(); |
| 157 | } |
| 158 | |
| 159 | /*! |
| 160 | Sets this QXmlSchema to a schema read from the \a data |
| 161 | |
| 162 | \a documentUri represents the schema obtained from the \a data. |
| 163 | It is the base URI of the schema, that is used internally to |
| 164 | resolve relative URIs that appear in the schema, and |
| 165 | for message reporting. |
| 166 | |
| 167 | If \a documentUri is not a valid URI, behavior is undefined. |
| 168 | \sa isValid() |
| 169 | |
| 170 | If the schema \l {isValid()} {is invalid}, \c{false} is returned |
| 171 | and the behavior is undefined. |
| 172 | |
| 173 | Example: |
| 174 | |
| 175 | \snippet qxmlschema/main.cpp 2 |
| 176 | |
| 177 | \sa isValid() |
| 178 | */ |
| 179 | bool QXmlSchema::load(const QByteArray &data, const QUrl &documentUri) |
| 180 | { |
| 181 | d->load(data, documentUri, targetNamespace: QString()); |
| 182 | return d->isValid(); |
| 183 | } |
| 184 | |
| 185 | /*! |
| 186 | Returns true if this schema is valid. Examples of invalid schemas |
| 187 | are ones that contain syntax errors or that do not conform the |
| 188 | W3C XML Schema specification. |
| 189 | */ |
| 190 | bool QXmlSchema::isValid() const |
| 191 | { |
| 192 | return d->isValid(); |
| 193 | } |
| 194 | |
| 195 | /*! |
| 196 | Returns the name pool used by this QXmlSchema for constructing \l |
| 197 | {QXmlName} {names}. There is no setter for the name pool, because |
| 198 | mixing name pools causes errors due to name confusion. |
| 199 | */ |
| 200 | QXmlNamePool QXmlSchema::namePool() const |
| 201 | { |
| 202 | return d->namePool(); |
| 203 | } |
| 204 | |
| 205 | /*! |
| 206 | Returns the document URI of the schema or an empty URI if no |
| 207 | schema has been set. |
| 208 | */ |
| 209 | QUrl QXmlSchema::documentUri() const |
| 210 | { |
| 211 | return d->documentUri(); |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | Changes the \l {QAbstractMessageHandler}{message handler} for this |
| 216 | QXmlSchema to \a handler. The schema sends all compile and |
| 217 | validation messages to this message handler. QXmlSchema does not take |
| 218 | ownership of \a handler. |
| 219 | |
| 220 | Normally, the default message handler is sufficient. It writes |
| 221 | compile and validation messages to \e stderr. The default message |
| 222 | handler includes color codes if \e stderr can render colors. |
| 223 | |
| 224 | When QXmlSchema calls QAbstractMessageHandler::message(), |
| 225 | the arguments are as follows: |
| 226 | |
| 227 | \table |
| 228 | \header |
| 229 | \li message() argument |
| 230 | \li Semantics |
| 231 | \row |
| 232 | \li QtMsgType type |
| 233 | \li Only QtWarningMsg and QtFatalMsg are used. The former |
| 234 | identifies a warning, while the latter identifies an error. |
| 235 | \row |
| 236 | \li const QString & description |
| 237 | \li An XHTML document which is the actual message. It is translated |
| 238 | into the current language. |
| 239 | \row |
| 240 | \li const QUrl &identifier |
| 241 | \li Identifies the error with a URI, where the fragment is |
| 242 | the error code, and the rest of the URI is the error namespace. |
| 243 | \row |
| 244 | \li const QSourceLocation & sourceLocation |
| 245 | \li Identifies where the error occurred. |
| 246 | \endtable |
| 247 | |
| 248 | */ |
| 249 | void QXmlSchema::setMessageHandler(QAbstractMessageHandler *handler) |
| 250 | { |
| 251 | d->setMessageHandler(handler); |
| 252 | } |
| 253 | |
| 254 | /*! |
| 255 | Returns the message handler that handles compile and validation |
| 256 | messages for this QXmlSchema. |
| 257 | */ |
| 258 | QAbstractMessageHandler *QXmlSchema::messageHandler() const |
| 259 | { |
| 260 | return d->messageHandler(); |
| 261 | } |
| 262 | |
| 263 | /*! |
| 264 | Sets the URI resolver to \a resolver. QXmlSchema does not take |
| 265 | ownership of \a resolver. |
| 266 | |
| 267 | \sa uriResolver() |
| 268 | */ |
| 269 | void QXmlSchema::setUriResolver(const QAbstractUriResolver *resolver) |
| 270 | { |
| 271 | d->setUriResolver(resolver); |
| 272 | } |
| 273 | |
| 274 | /*! |
| 275 | Returns the schema's URI resolver. If no URI resolver has been set, |
| 276 | Qt XML Patterns will use the URIs in schemas as they are. |
| 277 | |
| 278 | The URI resolver provides a level of abstraction, or \e{polymorphic |
| 279 | URIs}. A resolver can rewrite \e{logical} URIs to physical ones, or |
| 280 | it can translate obsolete or invalid URIs to valid ones. |
| 281 | |
| 282 | When Qt XML Patterns calls QAbstractUriResolver::resolve() the |
| 283 | absolute URI is the URI mandated by the schema specification, and the |
| 284 | relative URI is the URI specified by the user. |
| 285 | |
| 286 | \sa setUriResolver() |
| 287 | */ |
| 288 | const QAbstractUriResolver *QXmlSchema::uriResolver() const |
| 289 | { |
| 290 | return d->uriResolver(); |
| 291 | } |
| 292 | |
| 293 | /*! |
| 294 | Sets the network manager to \a manager. |
| 295 | QXmlSchema does not take ownership of \a manager. |
| 296 | |
| 297 | \sa networkAccessManager() |
| 298 | */ |
| 299 | void QXmlSchema::setNetworkAccessManager(QNetworkAccessManager *manager) |
| 300 | { |
| 301 | d->setNetworkAccessManager(manager); |
| 302 | } |
| 303 | |
| 304 | /*! |
| 305 | Returns the network manager, or 0 if it has not been set. |
| 306 | |
| 307 | \sa setNetworkAccessManager() |
| 308 | */ |
| 309 | QNetworkAccessManager *QXmlSchema::networkAccessManager() const |
| 310 | { |
| 311 | return d->networkAccessManager(); |
| 312 | } |
| 313 | |
| 314 | QT_END_NAMESPACE |
| 315 | |