Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / api / qxmlschema_p.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
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) {
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;
170 }
171
172 QAbstractMessageHandler *QXmlSchemaPrivate::messageHandler() const
173 {
174     if (m_userMessageHandler)
175         return m_userMessageHandler;
176
177     return m_messageHandler.data()->value;
178 }
179
180 void QXmlSchemaPrivate::setUriResolver(const QAbstractUriResolver *resolver)
181 {
182     m_uriResolver = resolver;
183 }
184
185 const QAbstractUriResolver *QXmlSchemaPrivate::uriResolver() const
186 {
187     return m_uriResolver;
188 }
189
190 void QXmlSchemaPrivate::setNetworkAccessManager(QNetworkAccessManager *networkmanager)
191 {
192     m_userNetworkAccessManager = networkmanager;
193 }
194
195 QNetworkAccessManager *QXmlSchemaPrivate::networkAccessManager() const
196 {
197     if (m_userNetworkAccessManager)
198         return m_userNetworkAccessManager;
199
200     return m_networkAccessManager.data()->value;
201 }
202
203 QT_END_NAMESPACE