| 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:GPL-EXCEPT$ |
| 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 General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "main.h" |
| 30 | |
| 31 | #include <QtCore/QDebug> |
| 32 | #include <QtCore/QFile> |
| 33 | #include <QtCore/QStringList> |
| 34 | #include <QtCore/QUrl> |
| 35 | #include <QtXmlPatterns/QXmlSchema> |
| 36 | #include <QtXmlPatterns/QXmlSchemaValidator> |
| 37 | |
| 38 | QT_USE_NAMESPACE |
| 39 | |
| 40 | int main(int argc, char **argv) |
| 41 | { |
| 42 | enum ExitCode |
| 43 | { |
| 44 | Valid = 0, |
| 45 | Invalid, |
| 46 | ParseError |
| 47 | }; |
| 48 | |
| 49 | enum ExecutionMode |
| 50 | { |
| 51 | InvalidMode, |
| 52 | SchemaOnlyMode, |
| 53 | InstanceOnlyMode, |
| 54 | SchemaAndInstanceMode |
| 55 | }; |
| 56 | |
| 57 | const QCoreApplication app(argc, argv); |
| 58 | QCoreApplication::setApplicationName(QLatin1String("xmlpatternsvalidator" )); |
| 59 | |
| 60 | QStringList arguments = QCoreApplication::arguments(); |
| 61 | if (arguments.size() != 2 && arguments.size() != 3) { |
| 62 | qDebug() << QXmlPatternistCLI::tr(sourceText: "usage: xmlpatternsvalidator (<schema url> | <instance url> <schema url> | <instance url>)" ); |
| 63 | return ParseError; |
| 64 | } |
| 65 | |
| 66 | // parse command line arguments |
| 67 | ExecutionMode mode = InvalidMode; |
| 68 | |
| 69 | QUrl schemaUri; |
| 70 | QUrl instanceUri; |
| 71 | |
| 72 | { |
| 73 | QUrl url = QUrl::fromUserInput(userInput: arguments[1]); |
| 74 | |
| 75 | if (arguments.size() == 2) { |
| 76 | // either it is a schema or instance document |
| 77 | |
| 78 | if (arguments[1].toLower().endsWith(s: QLatin1String(".xsd" ))) { |
| 79 | schemaUri = url; |
| 80 | mode = SchemaOnlyMode; |
| 81 | } else { |
| 82 | // as we could validate all types of xml documents, don't check the extension here |
| 83 | instanceUri = url; |
| 84 | mode = InstanceOnlyMode; |
| 85 | } |
| 86 | } else if (arguments.size() == 3) { |
| 87 | instanceUri = url; |
| 88 | schemaUri = QUrl::fromUserInput(userInput: arguments[2]); |
| 89 | |
| 90 | mode = SchemaAndInstanceMode; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Validate schema |
| 95 | QXmlSchema schema; |
| 96 | if (InstanceOnlyMode != mode) { |
| 97 | schema.load(source: schemaUri); |
| 98 | if (!schema.isValid()) |
| 99 | return Invalid; |
| 100 | } |
| 101 | |
| 102 | if (SchemaOnlyMode == mode) |
| 103 | return Valid; |
| 104 | |
| 105 | // Validate instance |
| 106 | QXmlSchemaValidator validator(schema); |
| 107 | if (validator.validate(source: instanceUri)) |
| 108 | return Valid; |
| 109 | |
| 110 | return Invalid; |
| 111 | } |
| 112 | |