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: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 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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qacceltreeresourceloader_p.h"
41#include "qxmlschema.h"
42#include "qxmlschema_p.h"
43
44#include <QtCore/QBuffer>
45#include <QtCore/QIODevice>
46#include <QtCore/QUrl>
47
48QT_BEGIN_NAMESPACE
49
50QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlNamePool &namePool)
51 : m_namePool(namePool)
52 , m_userMessageHandler(0)
53 , m_uriResolver(0)
54 , m_userNetworkAccessManager(0)
55 , m_schemaContext(new QPatternist::XsdSchemaContext(m_namePool.d))
56 , m_schemaParserContext(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext))
57 , m_schemaIsValid(false)
58{
59 m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
60 m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
61}
62
63QXmlSchemaPrivate::QXmlSchemaPrivate(const QPatternist::XsdSchemaContext::Ptr &schemaContext)
64 : m_namePool(QXmlNamePool(schemaContext->namePool().data()))
65 , m_userMessageHandler(0)
66 , m_uriResolver(0)
67 , m_userNetworkAccessManager(0)
68 , m_schemaContext(schemaContext)
69 , m_schemaParserContext(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext))
70 , m_schemaIsValid(false)
71{
72 m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
73 m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
74}
75
76QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlSchemaPrivate &other)
77 : QSharedData(other)
78{
79 m_namePool = other.m_namePool;
80 m_userMessageHandler = other.m_userMessageHandler;
81 m_uriResolver = other.m_uriResolver;
82 m_userNetworkAccessManager = other.m_userNetworkAccessManager;
83 m_messageHandler = other.m_messageHandler;
84 m_networkAccessManager = other.m_networkAccessManager;
85
86 m_schemaContext = other.m_schemaContext;
87 m_schemaParserContext = other.m_schemaParserContext;
88 m_schemaIsValid = other.m_schemaIsValid;
89 m_documentUri = other.m_documentUri;
90}
91
92void QXmlSchemaPrivate::load(const QUrl &source, const QString &targetNamespace)
93{
94 m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(uri: source);
95
96 m_schemaContext->setMessageHandler(messageHandler());
97 m_schemaContext->setUriResolver(uriResolver());
98 m_schemaContext->setNetworkAccessManager(networkAccessManager());
99
100 const QPatternist::AutoPtr<QNetworkReply> reply(
101 QPatternist::AccelTreeResourceLoader::load(
102 uri: m_documentUri, networkManager: m_schemaContext->networkAccessManager(),
103 context: m_schemaContext, handling: QPatternist::AccelTreeResourceLoader::ContinueOnError));
104 if (reply)
105 load(source: reply.data(), documentUri: source, targetNamespace);
106}
107
108void QXmlSchemaPrivate::load(const QByteArray &data, const QUrl &documentUri, const QString &targetNamespace)
109{
110 QByteArray localData(data);
111
112 QBuffer buffer(&localData);
113 buffer.open(openMode: QIODevice::ReadOnly);
114
115 load(source: &buffer, documentUri, targetNamespace);
116}
117
118void QXmlSchemaPrivate::load(QIODevice *source, const QUrl &documentUri, const QString &targetNamespace)
119{
120 m_schemaParserContext = QPatternist::XsdSchemaParserContext::Ptr(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext));
121 m_schemaIsValid = false;
122
123 if (!source) {
124 qWarning(msg: "A null QIODevice pointer cannot be passed.");
125 return;
126 }
127
128 if (!source->isReadable()) {
129 qWarning(msg: "The device must be readable.");
130 return;
131 }
132
133 m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(uri: documentUri);
134 m_schemaContext->setMessageHandler(messageHandler());
135 m_schemaContext->setUriResolver(uriResolver());
136 m_schemaContext->setNetworkAccessManager(networkAccessManager());
137
138 QPatternist::XsdSchemaParser parser(m_schemaContext, m_schemaParserContext, source);
139 parser.setDocumentURI(documentUri);
140 parser.setTargetNamespace(targetNamespace);
141
142 try {
143 parser.parse();
144 m_schemaParserContext->resolver()->resolve();
145
146 m_schemaIsValid = true;
147 } catch (QPatternist::Exception) {
148 m_schemaIsValid = false;
149 }
150}
151
152bool QXmlSchemaPrivate::isValid() const
153{
154 return m_schemaIsValid;
155}
156
157QXmlNamePool QXmlSchemaPrivate::namePool() const
158{
159 return m_namePool;
160}
161
162QUrl QXmlSchemaPrivate::documentUri() const
163{
164 return m_documentUri;
165}
166
167void QXmlSchemaPrivate::setMessageHandler(QAbstractMessageHandler *handler)
168{
169 m_userMessageHandler = handler;
170}
171
172QAbstractMessageHandler *QXmlSchemaPrivate::messageHandler() const
173{
174 if (m_userMessageHandler)
175 return m_userMessageHandler;
176
177 return m_messageHandler.data()->value;
178}
179
180void QXmlSchemaPrivate::setUriResolver(const QAbstractUriResolver *resolver)
181{
182 m_uriResolver = resolver;
183}
184
185const QAbstractUriResolver *QXmlSchemaPrivate::uriResolver() const
186{
187 return m_uriResolver;
188}
189
190void QXmlSchemaPrivate::setNetworkAccessManager(QNetworkAccessManager *networkmanager)
191{
192 m_userNetworkAccessManager = networkmanager;
193}
194
195QNetworkAccessManager *QXmlSchemaPrivate::networkAccessManager() const
196{
197 if (m_userNetworkAccessManager)
198 return m_userNetworkAccessManager;
199
200 return m_networkAccessManager.data()->value;
201}
202
203QT_END_NAMESPACE
204

source code of qtxmlpatterns/src/xmlpatterns/api/qxmlschema_p.cpp