Qt 4.8
Public Functions | Properties | List of all members
QXmlSchemaValidator Class Reference

The QXmlSchemaValidator class validates XML instance documents against a W3C XML Schema. More...

#include <qxmlschemavalidator.h>

Public Functions

QAbstractMessageHandlermessageHandler () const
 Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator. More...
 
QXmlNamePool namePool () const
 Returns the name pool used by this QXmlSchemaValidator for constructing QXmlName {names}. More...
 
QNetworkAccessManagernetworkAccessManager () const
 Returns the network manager, or 0 if it has not been set. More...
 
 QXmlSchemaValidator ()
 Constructs a schema validator. More...
 
 QXmlSchemaValidator (const QXmlSchema &schema)
 Constructs a schema validator that will use schema for validation. More...
 
QXmlSchema schema () const
 Returns the schema that is used for validation. More...
 
void setMessageHandler (QAbstractMessageHandler *handler)
 Changes the QAbstractMessageHandler{message handler} for this QXmlSchemaValidator to handler. More...
 
void setNetworkAccessManager (QNetworkAccessManager *networkmanager)
 Sets the network manager to manager. More...
 
void setSchema (const QXmlSchema &schema)
 Sets the schema that shall be used for further validation. More...
 
void setUriResolver (const QAbstractUriResolver *resolver)
 Sets the URI resolver to resolver. More...
 
const QAbstractUriResolveruriResolver () const
 Returns the schema's URI resolver. More...
 
bool validate (const QUrl &source) const
 Validates the XML instance document read from source against the schema. More...
 
bool validate (QIODevice *source, const QUrl &documentUri=QUrl()) const
 Validates the XML instance document read from source with the given documentUri against the schema. More...
 
bool validate (const QByteArray &data, const QUrl &documentUri=QUrl()) const
 Validates the XML instance document read from data with the given documentUri against the schema. More...
 
 ~QXmlSchemaValidator ()
 Destroys this QXmlSchemaValidator. More...
 

Properties

QXmlSchemaValidatorPrivate *const d
 

Detailed Description

The QXmlSchemaValidator class validates XML instance documents against a W3C XML Schema.

Note
This class or function is reentrant.
Since
4.6

The QXmlSchemaValidator class loads, parses an XML instance document and validates it against a W3C XML Schema that has been compiled with QXmlSchema.

The following example shows how to load a XML Schema from a local file, check whether it is a valid schema document and use it for validation of an XML instance document:

QUrl schemaUrl("file:///home/user/schema.xsd");
schema.load(schemaUrl);
if (schema.isValid()) {
QFile file("test.xml");
file.open(QIODevice::ReadOnly);
QXmlSchemaValidator validator(schema);
if (validator.validate(&file, QUrl::fromLocalFile(file.fileName())))
qDebug() << "instance document is valid";
else
qDebug() << "instance document is invalid";
}

XML Schema Version

This class implements schema validation according to the XML Schema 1.0 specification.

See also
QXmlSchema, {xmlpatterns/schema}{XML Schema Validation Example}

Definition at line 62 of file qxmlschemavalidator.h.

Constructors and Destructors

◆ QXmlSchemaValidator() [1/2]

QXmlSchemaValidator::QXmlSchemaValidator ( )

Constructs a schema validator.

The schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute.

Definition at line 90 of file qxmlschemavalidator.cpp.

92 {
93 }
The QXmlSchema class provides loading and validation of a W3C XML Schema.
Definition: qxmlschema.h:62
QXmlSchemaValidatorPrivate *const d

◆ QXmlSchemaValidator() [2/2]

QXmlSchemaValidator::QXmlSchemaValidator ( const QXmlSchema schema)

Constructs a schema validator that will use schema for validation.

If an empty QXmlSchema schema is passed to the validator, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute.

Definition at line 101 of file qxmlschemavalidator.cpp.

102  : d(new QXmlSchemaValidatorPrivate(schema))
103 {
104 }
QXmlSchemaValidatorPrivate *const d

◆ ~QXmlSchemaValidator()

QXmlSchemaValidator::~QXmlSchemaValidator ( )

Destroys this QXmlSchemaValidator.

Definition at line 109 of file qxmlschemavalidator.cpp.

110 {
111  delete d;
112 }
QXmlSchemaValidatorPrivate *const d

Functions

◆ messageHandler()

QAbstractMessageHandler * QXmlSchemaValidator::messageHandler ( ) const

Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator.

Definition at line 290 of file qxmlschemavalidator.cpp.

Referenced by validate().

291 {
292  if (d->m_userMessageHandler)
293  return d->m_userMessageHandler;
294 
295  return d->m_messageHandler.data()->value;
296 }
QAbstractMessageHandler * m_userMessageHandler
QPatternist::ReferenceCountedValue< QAbstractMessageHandler >::Ptr m_messageHandler
QXmlSchemaValidatorPrivate *const d

◆ namePool()

QXmlNamePool QXmlSchemaValidator::namePool ( ) const

Returns the name pool used by this QXmlSchemaValidator for constructing QXmlName {names}.

There is no setter for the name pool, because mixing name pools causes errors due to name confusion.

Definition at line 233 of file qxmlschemavalidator.cpp.

234 {
235  return d->m_namePool;
236 }
QXmlSchemaValidatorPrivate *const d

◆ networkAccessManager()

QNetworkAccessManager * QXmlSchemaValidator::networkAccessManager ( ) const

Returns the network manager, or 0 if it has not been set.

See also
setNetworkAccessManager()

Definition at line 344 of file qxmlschemavalidator.cpp.

Referenced by validate().

345 {
348 
349  return d->m_networkAccessManager.data()->value;
350 }
QNetworkAccessManager * m_userNetworkAccessManager
QPatternist::ReferenceCountedValue< QNetworkAccessManager >::Ptr m_networkAccessManager
QXmlSchemaValidatorPrivate *const d

◆ schema()

QXmlSchema QXmlSchemaValidator::schema ( ) const

Returns the schema that is used for validation.

Definition at line 241 of file qxmlschemavalidator.cpp.

242 {
243  return d->m_originalSchema;
244 }
QXmlSchemaValidatorPrivate *const d

◆ setMessageHandler()

void QXmlSchemaValidator::setMessageHandler ( QAbstractMessageHandler handler)

Changes the QAbstractMessageHandler{message handler} for this QXmlSchemaValidator to handler.

The schema validator sends all parsing and validation messages to this message handler. QXmlSchemaValidator does not take ownership of handler.

Normally, the default message handler is sufficient. It writes compile and validation messages to stderr. The default message handler includes color codes if stderr can render colors.

When QXmlSchemaValidator calls QAbstractMessageHandler::message(), the arguments are as follows:

message() argument Semantics
QtMsgType type Only QtWarningMsg and QtFatalMsg are used. The former identifies a warning, while the latter identifies an error.
const QString & description An XHTML document which is the actual message. It is translated into the current language.
const QUrl &identifier Identifies the error with a URI, where the fragment is the error code, and the rest of the URI is the error namespace.
const QSourceLocation & sourceLocation Identifies where the error occurred.

Definition at line 281 of file qxmlschemavalidator.cpp.

282 {
284 }
QAbstractMessageHandler * m_userMessageHandler
QImageIOHandler * handler
QXmlSchemaValidatorPrivate *const d

◆ setNetworkAccessManager()

void QXmlSchemaValidator::setNetworkAccessManager ( QNetworkAccessManager manager)

Sets the network manager to manager.

QXmlSchemaValidator does not take ownership of manager.

See also
networkAccessManager()

Definition at line 334 of file qxmlschemavalidator.cpp.

335 {
336  d->m_userNetworkAccessManager = manager;
337 }
QNetworkAccessManager * m_userNetworkAccessManager
QXmlSchemaValidatorPrivate *const d

◆ setSchema()

void QXmlSchemaValidator::setSchema ( const QXmlSchema schema)

Sets the schema that shall be used for further validation.

If the schema is empty, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute.

Definition at line 120 of file qxmlschemavalidator.cpp.

121 {
122  d->setSchema(schema);
123 }
QXmlSchemaValidatorPrivate *const d
void setSchema(const QXmlSchema &schema)

◆ setUriResolver()

void QXmlSchemaValidator::setUriResolver ( const QAbstractUriResolver resolver)

Sets the URI resolver to resolver.

QXmlSchemaValidator does not take ownership of resolver.

See also
uriResolver()

Definition at line 304 of file qxmlschemavalidator.cpp.

305 {
306  d->m_uriResolver = resolver;
307 }
const QAbstractUriResolver * m_uriResolver
QXmlSchemaValidatorPrivate *const d

◆ uriResolver()

const QAbstractUriResolver * QXmlSchemaValidator::uriResolver ( ) const

Returns the schema's URI resolver.

If no URI resolver has been set, QtXmlPatterns will use the URIs in instance documents as they are.

The URI resolver provides a level of abstraction, or polymorphic URIs. A resolver can rewrite logical URIs to physical ones, or it can translate obsolete or invalid URIs to valid ones.

When QtXmlPatterns calls QAbstractUriResolver::resolve() the absolute URI is the URI mandated by the schema specification, and the relative URI is the URI specified by the user.

See also
setUriResolver()

Definition at line 323 of file qxmlschemavalidator.cpp.

Referenced by validate().

324 {
325  return d->m_uriResolver;
326 }
const QAbstractUriResolver * m_uriResolver
QXmlSchemaValidatorPrivate *const d

◆ validate() [1/3]

bool QXmlSchemaValidator::validate ( const QUrl source) const

Validates the XML instance document read from source against the schema.

Returns true if the XML instance document is valid according to the schema, false otherwise.

Example:

const QXmlSchema schema = getSchema();
const QUrl url("http://www.schema-example.org/test.xml");
QXmlSchemaValidator validator(schema);
if (validator.validate(url))
qDebug() << "instance document is valid";
else
qDebug() << "instance document is invalid";

Definition at line 156 of file qxmlschemavalidator.cpp.

Referenced by validate().

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 }
A smart pointer very similar to std::auto_ptr.
Definition: qautoptr_p.h:73
const QAbstractUriResolver * uriResolver() const
Returns the schema&#39;s URI resolver.
QPatternist::XsdSchemaContext::Ptr m_context
QNetworkAccessManager * networkAccessManager() const
Returns the network manager, or 0 if it has not been set.
The resource loader will report no error and return an empty QNetworkReply.
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.
QAbstractMessageHandler * messageHandler() const
Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator...
bool validate(const QUrl &source) const
Validates the XML instance document read from source against the schema.
QXmlSchemaValidatorPrivate *const d

◆ validate() [2/3]

bool QXmlSchemaValidator::validate ( QIODevice source,
const QUrl documentUri = QUrl() 
) const

Validates the XML instance document read from source with the given documentUri against the schema.

Returns true if the XML instance document is valid according to the schema, false otherwise.

Example:

const QXmlSchema schema = getSchema();
QFile file("test.xml");
file.open(QIODevice::ReadOnly);
QXmlSchemaValidator validator(schema);
if (validator.validate(&file, QUrl::fromLocalFile(file.fileName())))
qDebug() << "instance document is valid";
else
qDebug() << "instance document is invalid";

Definition at line 181 of file qxmlschemavalidator.cpp.

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)
218  reader.addSchema(d->m_schema, d->m_schemaDocumentUri);
219  try {
220  reader.read();
221  } catch (QPatternist::Exception exception) {
222  Q_UNUSED(exception);
223  return false;
224  }
225 
226  return true;
227 }
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.
QPatternist::XsdSchema::Ptr m_schema
Builds an AccelTree from a stream of XML/Item events received through the NodeBuilder interface...
Definition: qacceltree_p.h:69
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
const QAbstractUriResolver * uriResolver() const
Returns the schema&#39;s URI resolver.
QPatternist::XsdSchemaContext::Ptr m_context
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 ...
Q_CORE_EXPORT void qWarning(const char *,...)
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
A delegate class that wraps around a QAbstractXmlNodeModel and provides additional validation specifi...
QAbstractMessageHandler * messageHandler() const
Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator...
The validating schema instance reader.
A value based class that hands out QNetworkAccessManager appropriately for variable bindings...
#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

◆ validate() [3/3]

bool QXmlSchemaValidator::validate ( const QByteArray data,
const QUrl documentUri = QUrl() 
) const

Validates the XML instance document read from data with the given documentUri against the schema.

Returns true if the XML instance document is valid according to the schema, false otherwise.

Example:

const QXmlSchema schema = getSchema();
QByteArray data("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<test></test>");
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
QXmlSchemaValidator validator(schema);
if (validator.validate(&buffer))
qDebug() << "instance document is valid";
else
qDebug() << "instance document is invalid";

Definition at line 136 of file qxmlschemavalidator.cpp.

137 {
138  QByteArray localData(data);
139 
140  QBuffer buffer(&localData);
141  buffer.open(QIODevice::ReadOnly);
142 
143  return validate(&buffer, documentUri);
144 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
bool validate(const QUrl &source) const
Validates the XML instance document read from source against the schema.

Properties

◆ d

QXmlSchemaValidatorPrivate* const QXmlSchemaValidator::d
private

The documentation for this class was generated from the following files: