1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtXmlPatterns module 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 | #include "qacceltreeresourceloader_p.h"
|
---|
43 | #include "qxmlschema.h"
|
---|
44 | #include "qxmlschema_p.h"
|
---|
45 |
|
---|
46 | #include <QtCore/QBuffer>
|
---|
47 | #include <QtCore/QIODevice>
|
---|
48 | #include <QtCore/QUrl>
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlNamePool &namePool)
|
---|
53 | : m_namePool(namePool)
|
---|
54 | , m_userMessageHandler(0)
|
---|
55 | , m_uriResolver(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)
|
---|
60 | {
|
---|
61 | m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
|
---|
62 | m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
|
---|
63 | }
|
---|
64 |
|
---|
65 | QXmlSchemaPrivate::QXmlSchemaPrivate(const QPatternist::XsdSchemaContext::Ptr &schemaContext)
|
---|
66 | : m_namePool(QXmlNamePool(schemaContext->namePool().data()))
|
---|
67 | , m_userMessageHandler(0)
|
---|
68 | , m_uriResolver(0)
|
---|
69 | , m_userNetworkAccessManager(0)
|
---|
70 | , m_schemaContext(schemaContext)
|
---|
71 | , m_schemaParserContext(new QPatternist::XsdSchemaParserContext(m_namePool.d, m_schemaContext))
|
---|
72 | , m_schemaIsValid(false)
|
---|
73 | {
|
---|
74 | m_networkAccessManager = new QPatternist::ReferenceCountedValue<QNetworkAccessManager>(new QNetworkAccessManager());
|
---|
75 | m_messageHandler = new QPatternist::ReferenceCountedValue<QAbstractMessageHandler>(new QPatternist::ColoringMessageHandler());
|
---|
76 | }
|
---|
77 |
|
---|
78 | QXmlSchemaPrivate::QXmlSchemaPrivate(const QXmlSchemaPrivate &other)
|
---|
79 | : QSharedData(other)
|
---|
80 | {
|
---|
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;
|
---|
87 |
|
---|
88 | m_schemaContext = other.m_schemaContext;
|
---|
89 | m_schemaParserContext = other.m_schemaParserContext;
|
---|
90 | m_schemaIsValid = other.m_schemaIsValid;
|
---|
91 | m_documentUri = other.m_documentUri;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void QXmlSchemaPrivate::load(const QUrl &source, const QString &targetNamespace)
|
---|
95 | {
|
---|
96 | m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(source);
|
---|
97 |
|
---|
98 | m_schemaContext->setMessageHandler(messageHandler());
|
---|
99 | m_schemaContext->setUriResolver(uriResolver());
|
---|
100 | m_schemaContext->setNetworkAccessManager(networkAccessManager());
|
---|
101 |
|
---|
102 | const QPatternist::AutoPtr<QNetworkReply> reply(QPatternist::AccelTreeResourceLoader::load(source, m_schemaContext->networkAccessManager(),
|
---|
103 | m_schemaContext, QPatternist::AccelTreeResourceLoader::ContinueOnError));
|
---|
104 | if (reply)
|
---|
105 | load(reply.data(), source, targetNamespace);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QXmlSchemaPrivate::load(const QByteArray &data, const QUrl &documentUri, const QString &targetNamespace)
|
---|
109 | {
|
---|
110 | QByteArray localData(data);
|
---|
111 |
|
---|
112 | QBuffer buffer(&localData);
|
---|
113 | buffer.open(QIODevice::ReadOnly);
|
---|
114 |
|
---|
115 | load(&buffer, documentUri, targetNamespace);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void 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("A null QIODevice pointer cannot be passed.");
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (!source->isReadable()) {
|
---|
129 | qWarning("The device must be readable.");
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | m_documentUri = QPatternist::XPathHelper::normalizeQueryURI(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 exception) {
|
---|
148 | m_schemaIsValid = false;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool QXmlSchemaPrivate::isValid() const
|
---|
153 | {
|
---|
154 | return m_schemaIsValid;
|
---|
155 | }
|
---|
156 |
|
---|
157 | QXmlNamePool QXmlSchemaPrivate::namePool() const
|
---|
158 | {
|
---|
159 | return m_namePool;
|
---|
160 | }
|
---|
161 |
|
---|
162 | QUrl QXmlSchemaPrivate::documentUri() const
|
---|
163 | {
|
---|
164 | return m_documentUri;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void QXmlSchemaPrivate::setMessageHandler(QAbstractMessageHandler *handler)
|
---|
168 | {
|
---|
169 | m_userMessageHandler = handler;
|
---|
|
---|