src/xmlpatterns/api/qxmlschema.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qxmlschema.h"
       
    43 #include "qxmlschema_p.h"
       
    44 
       
    45 #include <QtCore/QIODevice>
       
    46 #include <QtCore/QUrl>
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 /*!
       
    51   \class QXmlSchema
       
    52 
       
    53   \brief The QXmlSchema class provides loading and validation of a W3C XML Schema.
       
    54 
       
    55   \reentrant
       
    56   \since 4.6
       
    57   \ingroup xml-tools
       
    58 
       
    59   The QXmlSchema class loads, compiles and validates W3C XML Schema files
       
    60   that can be used further for validation of XML instance documents via
       
    61   \l{QXmlSchemaValidator}.
       
    62 
       
    63   The following example shows how to load a XML Schema file from the network
       
    64   and test whether it is a valid schema document:
       
    65 
       
    66   \snippet doc/src/snippets/qxmlschema/main.cpp 0
       
    67 
       
    68   \sa QXmlSchemaValidator, {xmlpatterns/schema}{XML Schema Validation Example}
       
    69 */
       
    70 
       
    71 /*!
       
    72   Constructs an invalid, empty schema that cannot be used until
       
    73   load() is called.
       
    74  */
       
    75 QXmlSchema::QXmlSchema()
       
    76     : d(new QXmlSchemaPrivate(QXmlNamePool()))
       
    77 {
       
    78 }
       
    79 
       
    80 /*!
       
    81   Constructs a QXmlSchema that is a copy of \a other. The new
       
    82   instance will share resources with the existing schema
       
    83   to the extent possible.
       
    84  */
       
    85 QXmlSchema::QXmlSchema(const QXmlSchema &other)
       
    86     : d(other.d)
       
    87 {
       
    88 }
       
    89 
       
    90 /*!
       
    91   Destroys this QXmlSchema.
       
    92  */
       
    93 QXmlSchema::~QXmlSchema()
       
    94 {
       
    95 }
       
    96 
       
    97 /*!
       
    98   Sets this QXmlSchema to a schema loaded from the \a source
       
    99   URI.
       
   100 
       
   101   If the schema \l {isValid()} {is invalid}, \c{false} is returned
       
   102   and the behavior is undefined.
       
   103 
       
   104   Example:
       
   105 
       
   106   \snippet doc/src/snippets/qxmlschema/main.cpp 0
       
   107 
       
   108   \sa isValid()
       
   109  */
       
   110 bool QXmlSchema::load(const QUrl &source)
       
   111 {
       
   112     d->load(source, QString());
       
   113     return d->isValid();
       
   114 }
       
   115 
       
   116 /*!
       
   117   Sets this QXmlSchema to a schema read from the \a source
       
   118   device. The device must have been opened with at least
       
   119   QIODevice::ReadOnly.
       
   120 
       
   121   \a documentUri represents the schema obtained from the \a source
       
   122   device. It is the base URI of the schema, that is used
       
   123   internally to resolve relative URIs that appear in the schema, and
       
   124   for message reporting.
       
   125 
       
   126   If \a source is \c null or not readable, or if \a documentUri is not
       
   127   a valid URI, behavior is undefined.
       
   128 
       
   129   If the schema \l {isValid()} {is invalid}, \c{false} is returned
       
   130   and the behavior is undefined.
       
   131 
       
   132   Example:
       
   133 
       
   134   \snippet doc/src/snippets/qxmlschema/main.cpp 1
       
   135 
       
   136   \sa isValid()
       
   137  */
       
   138 bool QXmlSchema::load(QIODevice *source, const QUrl &documentUri)
       
   139 {
       
   140     d->load(source, documentUri, QString());
       
   141     return d->isValid();
       
   142 }
       
   143 
       
   144 /*!
       
   145   Sets this QXmlSchema to a schema read from the \a data
       
   146 
       
   147   \a documentUri represents the schema obtained from the \a data.
       
   148   It is the base URI of the schema, that is used internally to
       
   149   resolve relative URIs that appear in the schema, and
       
   150   for message reporting.
       
   151 
       
   152   If \a documentUri is not a valid URI, behavior is undefined.
       
   153   \sa isValid()
       
   154 
       
   155   If the schema \l {isValid()} {is invalid}, \c{false} is returned
       
   156   and the behavior is undefined.
       
   157 
       
   158   Example:
       
   159 
       
   160   \snippet doc/src/snippets/qxmlschema/main.cpp 2
       
   161 
       
   162   \sa isValid()
       
   163  */
       
   164 bool QXmlSchema::load(const QByteArray &data, const QUrl &documentUri)
       
   165 {
       
   166     d->load(data, documentUri, QString());
       
   167     return d->isValid();
       
   168 }
       
   169 
       
   170 /*!
       
   171   Returns true if this schema is valid. Examples of invalid schemas
       
   172   are ones that contain syntax errors or that do not conform the
       
   173   W3C XML Schema specification.
       
   174  */
       
   175 bool QXmlSchema::isValid() const
       
   176 {
       
   177     return d->isValid();
       
   178 }
       
   179 
       
   180 /*!
       
   181   Returns the name pool used by this QXmlSchema for constructing \l
       
   182   {QXmlName} {names}. There is no setter for the name pool, because
       
   183   mixing name pools causes errors due to name confusion.
       
   184  */
       
   185 QXmlNamePool QXmlSchema::namePool() const
       
   186 {
       
   187     return d->namePool();
       
   188 }
       
   189 
       
   190 /*!
       
   191   Returns the document URI of the schema or an empty URI if no
       
   192   schema has been set.
       
   193  */
       
   194 QUrl QXmlSchema::documentUri() const
       
   195 {
       
   196     return d->documentUri();
       
   197 }
       
   198 
       
   199 /*!
       
   200   Changes the \l {QAbstractMessageHandler}{message handler} for this
       
   201   QXmlSchema to \a handler. The schema sends all compile and
       
   202   validation messages to this message handler. QXmlSchema does not take
       
   203   ownership of \a handler.
       
   204 
       
   205   Normally, the default message handler is sufficient. It writes
       
   206   compile and validation messages to \e stderr. The default message
       
   207   handler includes color codes if \e stderr can render colors.
       
   208 
       
   209   When QXmlSchema calls QAbstractMessageHandler::message(),
       
   210   the arguments are as follows:
       
   211 
       
   212   \table
       
   213   \header
       
   214     \o message() argument
       
   215     \o Semantics
       
   216   \row
       
   217     \o QtMsgType type
       
   218     \o Only QtWarningMsg and QtFatalMsg are used. The former
       
   219        identifies a warning, while the latter identifies an error.
       
   220   \row
       
   221     \o const QString & description
       
   222     \o An XHTML document which is the actual message. It is translated
       
   223        into the current language.
       
   224   \row
       
   225     \o const QUrl &identifier
       
   226     \o Identifies the error with a URI, where the fragment is
       
   227        the error code, and the rest of the URI is the error namespace.
       
   228   \row
       
   229     \o const QSourceLocation & sourceLocation
       
   230     \o Identifies where the error occurred.
       
   231   \endtable
       
   232 
       
   233  */
       
   234 void QXmlSchema::setMessageHandler(QAbstractMessageHandler *handler)
       
   235 {
       
   236     d->setMessageHandler(handler);
       
   237 }
       
   238 
       
   239 /*!
       
   240     Returns the message handler that handles compile and validation
       
   241     messages for this QXmlSchema.
       
   242  */
       
   243 QAbstractMessageHandler *QXmlSchema::messageHandler() const
       
   244 {
       
   245     return d->messageHandler();
       
   246 }
       
   247 
       
   248 /*!
       
   249   Sets the URI resolver to \a resolver. QXmlSchema does not take
       
   250   ownership of \a resolver.
       
   251 
       
   252   \sa uriResolver()
       
   253  */
       
   254 void QXmlSchema::setUriResolver(const QAbstractUriResolver *resolver)
       
   255 {
       
   256     d->setUriResolver(resolver);
       
   257 }
       
   258 
       
   259 /*!
       
   260   Returns the schema's URI resolver. If no URI resolver has been set,
       
   261   QtXmlPatterns will use the URIs in schemas as they are.
       
   262 
       
   263   The URI resolver provides a level of abstraction, or \e{polymorphic
       
   264   URIs}. A resolver can rewrite \e{logical} URIs to physical ones, or
       
   265   it can translate obsolete or invalid URIs to valid ones.
       
   266 
       
   267   When QtXmlPatterns calls QAbstractUriResolver::resolve() the
       
   268   absolute URI is the URI mandated by the schema specification, and the
       
   269   relative URI is the URI specified by the user.
       
   270 
       
   271   \sa setUriResolver()
       
   272  */
       
   273 const QAbstractUriResolver *QXmlSchema::uriResolver() const
       
   274 {
       
   275     return d->uriResolver();
       
   276 }
       
   277 
       
   278 /*!
       
   279   Sets the network manager to \a manager.
       
   280   QXmlSchema does not take ownership of \a manager.
       
   281 
       
   282   \sa networkAccessManager()
       
   283  */
       
   284 void QXmlSchema::setNetworkAccessManager(QNetworkAccessManager *manager)
       
   285 {
       
   286     d->setNetworkAccessManager(manager);
       
   287 }
       
   288 
       
   289 /*!
       
   290   Returns the network manager, or 0 if it has not been set.
       
   291 
       
   292   \sa setNetworkAccessManager()
       
   293  */
       
   294 QNetworkAccessManager *QXmlSchema::networkAccessManager() const
       
   295 {
       
   296     return d->networkAccessManager();
       
   297 }
       
   298 
       
   299 QT_END_NAMESPACE