1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qacceltreeresourceloader_p.h"
43 #include "qxmlschema.h"
44 #include "qxmlschema_p.h"
46 #include <QtCore/QBuffer>
47 #include <QtCore/QIODevice>
48 #include <QtCore/QUrl>
52 QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlNamePool &namePool)
53 : m_namePool(namePool)
54 , m_userMessageHandler(0)
56 , m_userNetworkAccessManager(0)
57 , m_schemaContext(new QPatternist::XsdSchemaContext(m_namePool.d))
58 , m_schemaParserContext(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext))
59 , m_schemaIsValid(false)
61 m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
62 m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
65 QXmlSchemaPrivate::QXmlSchemaPrivate(const QPatternist::XsdSchemaContext::Ptr &schemaContext)
66 : m_namePool(QXmlNamePool(schemaContext->namePool().data()))
67 , m_userMessageHandler(0)
69 , m_userNetworkAccessManager(0)
70 , m_schemaContext(schemaContext)
71 , m_schemaParserContext(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext))
72 , m_schemaIsValid(false)
74 m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
75 m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
78 QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlSchemaPrivate &other)
81 m_namePool = other.m_namePool;
82 m_userMessageHandler = other.m_userMessageHandler;
83 m_uriResolver = other.m_uriResolver;
84 m_userNetworkAccessManager = other.m_userNetworkAccessManager;
85 m_messageHandler = other.m_messageHandler;
86 m_networkAccessManager = other.m_networkAccessManager;
88 m_schemaContext = other.m_schemaContext;
89 m_schemaParserContext = other.m_schemaParserContext;
90 m_schemaIsValid = other.m_schemaIsValid;
91 m_documentUri = other.m_documentUri;
94 void QXmlSchemaPrivate::load(const QUrl &source, const QString &targetNamespace)
96 m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(source);
98 m_schemaContext->setMessageHandler(messageHandler());
99 m_schemaContext->setUriResolver(uriResolver());
100 m_schemaContext->setNetworkAccessManager(networkAccessManager());
102 const QPatternist::AutoPtr<QNetworkReply> reply(QPatternist::AccelTreeResourceLoader::load(source, m_schemaContext->networkAccessManager(),
103 m_schemaContext, QPatternist::AccelTreeResourceLoader::ContinueOnError));
105 load(reply.data(), source, targetNamespace);
108 void QXmlSchemaPrivate::load(const QByteArray &data, const QUrl &documentUri, const QString &targetNamespace)
110 QByteArray localData(data);
112 QBuffer buffer(&localData);
113 buffer.open(QIODevice::ReadOnly);
115 load(&buffer, documentUri, targetNamespace);
118 void QXmlSchemaPrivate::load(QIODevice *source, const QUrl &documentUri, const QString &targetNamespace)
120 m_schemaParserContext = QPatternist::XsdSchemaParserContext::Ptr(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext));
121 m_schemaIsValid = false;
124 qWarning("A null QIODevice pointer cannot be passed.");
128 if (!source->isReadable()) {
129 qWarning("The device must be readable.");
133 m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(documentUri);
134 m_schemaContext->setMessageHandler(messageHandler());
135 m_schemaContext->setUriResolver(uriResolver());
136 m_schemaContext->setNetworkAccessManager(networkAccessManager());
138 QPatternist::XsdSchemaParser parser(m_schemaContext, m_schemaParserContext, source);
139 parser.setDocumentURI(documentUri);
140 parser.setTargetNamespace(targetNamespace);
144 m_schemaParserContext->resolver()->resolve();
146 m_schemaIsValid = true;
147 } catch (QPatternist::Exception) {
148 m_schemaIsValid = false;
152 bool QXmlSchemaPrivate::isValid() const
154 return m_schemaIsValid;
157 QXmlNamePool QXmlSchemaPrivate::namePool() const
162 QUrl QXmlSchemaPrivate::documentUri() const
164 return m_documentUri;
167 void QXmlSchemaPrivate::setMessageHandler(QAbstractMessageHandler *handler)
169 m_userMessageHandler = handler;
172 QAbstractMessageHandler *QXmlSchemaPrivate::messageHandler() const
174 if (m_userMessageHandler)
175 return m_userMessageHandler;
177 return m_messageHandler.data()->value;
180 void QXmlSchemaPrivate::setUriResolver(const QAbstractUriResolver *resolver)
182 m_uriResolver = resolver;
185 const QAbstractUriResolver *QXmlSchemaPrivate::uriResolver() const
187 return m_uriResolver;
190 void QXmlSchemaPrivate::setNetworkAccessManager(QNetworkAccessManager *networkmanager)
192 m_userNetworkAccessManager = networkmanager;
195 QNetworkAccessManager *QXmlSchemaPrivate::networkAccessManager() const
197 if (m_userNetworkAccessManager)
198 return m_userNetworkAccessManager;
200 return m_networkAccessManager.data()->value;