Qt 4.8
qxmlschemavalidator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qxmlschemavalidator.h"
43 #include "qxmlschemavalidator_p.h"
44 
46 #include "qxmlschema.h"
47 #include "qxmlschema_p.h"
49 
50 #include <QtCore/QBuffer>
51 #include <QtCore/QIODevice>
52 #include <QtCore/QUrl>
53 
55 
92 {
93 }
94 
102  : d(new QXmlSchemaValidatorPrivate(schema))
103 {
104 }
105 
110 {
111  delete d;
112 }
113 
121 {
122  d->setSchema(schema);
123 }
124 
136 bool QXmlSchemaValidator::validate(const QByteArray &data, const QUrl &documentUri) const
137 {
138  QByteArray localData(data);
139 
140  QBuffer buffer(&localData);
141  buffer.open(QIODevice::ReadOnly);
142 
143  return validate(&buffer, documentUri);
144 }
145 
156 bool QXmlSchemaValidator::validate(const QUrl &source) const
157 {
158  d->m_context->setMessageHandler(messageHandler());
159  d->m_context->setUriResolver(uriResolver());
160  d->m_context->setNetworkAccessManager(networkAccessManager());
161 
164  if (reply)
165  return validate(reply.data(), source);
166  else
167  return false;
168 }
169 
181 bool QXmlSchemaValidator::validate(QIODevice *source, const QUrl &documentUri) const
182 {
183  if (!source) {
184  qWarning("A null QIODevice pointer cannot be passed.");
185  return false;
186  }
187 
188  if (!source->isReadable()) {
189  qWarning("The device must be readable.");
190  return false;
191  }
192 
193  const QUrl normalizedUri = QPatternist::XPathHelper::normalizeQueryURI(documentUri);
194 
195  d->m_context->setMessageHandler(messageHandler());
196  d->m_context->setUriResolver(uriResolver());
197  d->m_context->setNetworkAccessManager(networkAccessManager());
198 
200  d->m_context->networkAccessManager()));
201 
203 
204  QPatternist::Item item;
205  try {
206  item = loader.openDocument(source, normalizedUri, d->m_context);
207  } catch (QPatternist::Exception exception) {
208  Q_UNUSED(exception);
209  return false;
210  }
211 
212  const QAbstractXmlNodeModel *model = item.asNode().model();
213 
215 
216  QPatternist::XsdValidatingInstanceReader reader(validatedModel, normalizedUri, d->m_context);
217  if (d->m_schema)
219  try {
220  reader.read();
221  } catch (QPatternist::Exception exception) {
222  Q_UNUSED(exception);
223  return false;
224  }
225 
226  return true;
227 }
228 
234 {
235  return d->m_namePool;
236 }
237 
242 {
243  return d->m_originalSchema;
244 }
245 
282 {
284 }
285 
291 {
292  if (d->m_userMessageHandler)
293  return d->m_userMessageHandler;
294 
295  return d->m_messageHandler.data()->value;
296 }
297 
305 {
306  d->m_uriResolver = resolver;
307 }
308 
324 {
325  return d->m_uriResolver;
326 }
327 
335 {
336  d->m_userNetworkAccessManager = manager;
337 }
338 
345 {
348 
349  return d->m_networkAccessManager.data()->value;
350 }
351 
double d
Definition: qnumeric_p.h:62
void addSchema(const XsdSchema::Ptr &schema, const QUrl &url)
QXmlNamePool namePool() const
Returns the name pool used by this QXmlSchemaValidator for constructing QXmlName {names}.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
Handles requests for documents, and instantiates them as AccelTree instances.
QAbstractMessageHandler * m_userMessageHandler
QPatternist::XsdSchema::Ptr m_schema
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
A smart pointer very similar to std::auto_ptr.
Definition: qautoptr_p.h:73
QNetworkAccessManager * m_userNetworkAccessManager
QPatternist::ReferenceCountedValue< QNetworkAccessManager >::Ptr m_networkAccessManager
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setUriResolver(const QAbstractUriResolver *resolver)
Sets the URI resolver to resolver.
Builds an AccelTree from a stream of XML/Item events received through the NodeBuilder interface...
Definition: qacceltree_p.h:69
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QXmlSchema class provides loading and validation of a W3C XML Schema.
Definition: qxmlschema.h:62
const QAbstractUriResolver * uriResolver() const
Returns the schema&#39;s URI resolver.
The QXmlNamePool class is a table of shared strings referenced by instances of QXmlName.
Definition: qxmlnamepool.h:69
T * data() const
Definition: qautoptr_p.h:149
void setMessageHandler(QAbstractMessageHandler *handler)
Changes the QAbstractMessageHandler{message handler} for this QXmlSchemaValidator to handler...
QPatternist::XsdSchemaContext::Ptr m_context
The QAbstractUriResolver class is a callback interface for resolving Uniform Resource Identifiers...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QNetworkAccessManager * networkAccessManager() const
Returns the network manager, or 0 if it has not been set.
The QAbstractXmlNodeModel class is an abstract base class for modeling non-XML data to look like XML ...
The resource loader will report no error and return an empty QNetworkReply.
void setSchema(const QXmlSchema &schema)
Sets the schema that shall be used for further validation.
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
QXmlSchemaValidator()
Constructs a schema validator.
The QNetworkAccessManager class allows the application to send network requests and receive replies...
QXmlSchema schema() const
Returns the schema that is used for validation.
static QNetworkReply * load(const QUrl &uri, QNetworkAccessManager *const networkManager, const ReportContext::Ptr &context, ErrorHandling handling=FailOnError)
Helper function that do NetworkAccessDelegator::get(), but does it blocked.
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
The QAbstractMessageHandler class provides a callback interface for handling messages.
A delegate class that wraps around a QAbstractXmlNodeModel and provides additional validation specifi...
~QXmlSchemaValidator()
Destroys this QXmlSchemaValidator.
QAbstractMessageHandler * messageHandler() const
Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator...
const QAbstractUriResolver * m_uriResolver
bool validate(const QUrl &source) const
Validates the XML instance document read from source against the schema.
The validating schema instance reader.
void setNetworkAccessManager(QNetworkAccessManager *networkmanager)
Sets the network manager to manager.
A value based class that hands out QNetworkAccessManager appropriately for variable bindings...
QImageIOHandler * handler
QPatternist::ReferenceCountedValue< QAbstractMessageHandler >::Ptr m_messageHandler
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QUrl normalizeQueryURI(const QUrl &uri)
QXmlSchemaValidatorPrivate *const d
void setSchema(const QXmlSchema &schema)