[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | /*!
|
---|
| 43 | \example xmlpatterns/schema
|
---|
| 44 | \title XML Schema Validation Example
|
---|
| 45 |
|
---|
| 46 | This example shows how to use QtXmlPatterns to validate XML with
|
---|
| 47 | a W3C XML Schema.
|
---|
| 48 |
|
---|
| 49 | \tableofcontents
|
---|
| 50 |
|
---|
| 51 | \section1 Introduction
|
---|
| 52 |
|
---|
| 53 | The example application shows different XML schema definitions and
|
---|
| 54 | for every definition two XML instance documents, one that is valid
|
---|
| 55 | according to the schema and one that is not.
|
---|
| 56 | The user can select the valid or invalid instance document, change
|
---|
| 57 | it and validate it again.
|
---|
| 58 |
|
---|
| 59 | \section2 The User Interface
|
---|
| 60 |
|
---|
| 61 | The UI for this example was created using \l{Qt Designer Manual} {Qt
|
---|
| 62 | Designer}:
|
---|
| 63 |
|
---|
| 64 | \image schema-example.png
|
---|
| 65 |
|
---|
| 66 | The UI consists of three parts, at the top the XML schema \l{QComboBox} {selection}
|
---|
| 67 | and the schema \l{QTextBrowser} {viewer}, below the XML instance \l{QComboBox} {selection}
|
---|
| 68 | and the instance \l{QTextEdit} {editor} and at the bottom the validation status \l{QLabel} {label}
|
---|
| 69 | next to the validation \l{QPushButton} {button}.
|
---|
| 70 |
|
---|
| 71 | \section2 Validating XML Instance Documents
|
---|
| 72 |
|
---|
| 73 | You can select one of the three predefined XML schemas and for each schema
|
---|
| 74 | an valid or invalid instance document. A click on the 'Validate' button will
|
---|
| 75 | validate the content of the XML instance editor against the schema from the
|
---|
| 76 | XML schema viewer. As you can modify the content of the instance editor, different
|
---|
| 77 | instances can be tested and validation error messages analysed.
|
---|
| 78 |
|
---|
| 79 | \section1 Code Walk-Through
|
---|
| 80 |
|
---|
| 81 | The example's main() function creates the standard instance of
|
---|
| 82 | QApplication. Then it creates an instance of the mainwindow class, shows it,
|
---|
| 83 | and starts the Qt event loop:
|
---|
| 84 |
|
---|
| 85 | \snippet examples/xmlpatterns/schema/main.cpp 0
|
---|
| 86 |
|
---|
| 87 | \section2 The UI Class: MainWindow
|
---|
| 88 |
|
---|
| 89 | The example's UI is a conventional Qt GUI application inheriting
|
---|
| 90 | QMainWindow and the class generated by \l{Qt Designer Manual} {Qt
|
---|
| 91 | Designer}:
|
---|
| 92 |
|
---|
| 93 | \snippet examples/xmlpatterns/schema/mainwindow.h 0
|
---|
| 94 |
|
---|
| 95 | The constructor fills the schema and instance \l{QComboBox} selections with the predefined
|
---|
| 96 | schemas and instances and connects their \l{QComboBox::currentIndexChanged()} {currentIndexChanged()}
|
---|
| 97 | signals to the window's \c{schemaSelected()} resp. \c{instanceSelected()} slot.
|
---|
| 98 | Furthermore the signal-slot connections for the validation \l{QPushButton} {button}
|
---|
| 99 | and the instance \l{QTextEdit} {editor} are set up.
|
---|
| 100 |
|
---|
| 101 | The call to \c{schemaSelected(0)} and \c{instanceSelected(0)} will trigger the validation
|
---|
| 102 | of the initial Contact Schema example.
|
---|
| 103 |
|
---|
| 104 | \snippet examples/xmlpatterns/schema/mainwindow.cpp 0
|
---|
| 105 |
|
---|
| 106 | In the \c{schemaSelected()} slot the content of the instance \l{QComboBox} {selection}
|
---|
| 107 | is adapted to the selected schema and the corresponding schema is loaded from the
|
---|
| 108 | \l{The Qt Resource System} {resource file} and displayed in the schema \l{QTextBrowser} {viewer}.
|
---|
| 109 | At the end of the method a revalidation is triggered.
|
---|
| 110 |
|
---|
| 111 | \snippet examples/xmlpatterns/schema/mainwindow.cpp 1
|
---|
| 112 |
|
---|
| 113 | In the \c{instanceSelected()} slot the selected instance is loaded from the
|
---|
| 114 | \l{The Qt Resource System} {resource file} and loaded into the instance \l{QTextEdit} {editor}
|
---|
| 115 | an the revalidation is triggered again.
|
---|
| 116 |
|
---|
| 117 | \snippet examples/xmlpatterns/schema/mainwindow.cpp 2
|
---|
| 118 |
|
---|
| 119 | The \c{validate()} slot does the actual work in this example.
|
---|
| 120 | At first it stores the content of the schema \l{QTextBrowser} {viewer} and the
|
---|
| 121 | \l{QTextEdit} {editor} into temporary \l{QByteArray} {variables}.
|
---|
| 122 | Then it instanciates a \c{MessageHandler} object which inherits from
|
---|
| 123 | \l{QAbstractMessageHandler} {QAbstractMessageHandler} and is a convenience
|
---|
| 124 | class to store error messages from the XmlPatterns system.
|
---|
| 125 |
|
---|
| 126 | \snippet examples/xmlpatterns/schema/mainwindow.cpp 4
|
---|
| 127 |
|
---|
| 128 | After the \l{QXmlSchema} {QXmlSchema} is instanciated and the message handler set
|
---|
| 129 | on it, the \l{QXmlSchema::load()} {load()} method is called with the schema data as argument.
|
---|
| 130 | If the schema is invalid or a parsing error has occured, \l{QXmlSchema::isValid()} {isValid()}
|
---|
| 131 | returns \c{false} and the error is flagged in \c{errorOccurred}.
|
---|
| 132 | If the loading was successful, a \l{QXmlSchemaValidator} {QXmlSchemaValidator} is
|
---|
| 133 | instanciated and the schema passed in the constructor.
|
---|
| 134 | A call to \l{QXmlSchemaValidator::validate()} {validate()} will validate the passed
|
---|
| 135 | XML instance data against the XML schema. The return value of that method signals
|
---|
| 136 | whether the validation was successful.
|
---|
| 137 | Depending on the success the status \l{QLabel} {label} is set to 'validation successful'
|
---|
| 138 | or the error message stored in the \c{MessageHandler}
|
---|
| 139 |
|
---|
| 140 | The rest of the code does only some fancy coloring and eyecandy.
|
---|
| 141 |
|
---|
| 142 | \snippet examples/xmlpatterns/schema/mainwindow.cpp 3
|
---|
| 143 | */
|
---|