source: trunk/src/xmlpatterns/acceltree/qacceltreeresourceloader.cpp@ 317

Last change on this file since 317 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtCore/QFile>
43#include <QtCore/QTextCodec>
44#include <QtCore/QTimer>
45#include <QtCore/QXmlStreamReader>
46
47#include <QtNetwork/QNetworkRequest>
48
49#include "qacceltreebuilder_p.h"
50#include "qatomicstring_p.h"
51#include "qautoptr_p.h"
52#include "qcommonsequencetypes_p.h"
53
54#include "qacceltreeresourceloader_p.h"
55
56QT_BEGIN_NAMESPACE
57
58using namespace QPatternist;
59
60static inline uint qHash(const QUrl &uri)
61{
62 return qHash(uri.toString());
63}
64
65AccelTreeResourceLoader::AccelTreeResourceLoader(const NamePool::Ptr &np,
66 const NetworkAccessDelegator::Ptr &manager) : m_namePool(np)
67 , m_networkAccessDelegator(manager)
68{
69 Q_ASSERT(m_namePool);
70 Q_ASSERT(m_networkAccessDelegator);
71}
72
73bool AccelTreeResourceLoader::retrieveDocument(const QUrl &uri,
74 const ReportContext::Ptr &context)
75{
76 Q_ASSERT(uri.isValid());
77 AccelTreeBuilder<true> builder(uri, uri, m_namePool, context.data());
78
79 const AutoPtr<QNetworkReply> reply(load(uri, m_networkAccessDelegator, context));
80
81 if(!reply)
82 return false;
83
84 bool success = false;
85 success = streamToReceiver(reply.data(), &builder, m_namePool, context, uri);
86
87 m_loadedDocuments.insert(uri, builder.builtDocument());
88 return success;
89}
90
91QNetworkReply *AccelTreeResourceLoader::load(const QUrl &uri,
92 const NetworkAccessDelegator::Ptr &networkDelegator,
93 const ReportContext::Ptr &context)
94{
95 return load(uri,
96 networkDelegator->managerFor(uri),
97 context);
98}
99
100QNetworkReply *AccelTreeResourceLoader::load(const QUrl &uri,
101 QNetworkAccessManager *const networkManager,
102 const ReportContext::Ptr &context)
103{
104 Q_ASSERT(networkManager);
105 Q_ASSERT(uri.isValid());
106
107 NetworkLoop networkLoop;
108
109 QNetworkRequest request(uri);
110 QNetworkReply *const reply = networkManager->get(request);
111 networkLoop.connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(error(QNetworkReply::NetworkError)));
112 networkLoop.connect(reply, SIGNAL(finished()), SLOT(finished()));
113
114 if(networkLoop.exec())
115 {
116 const QString errorMessage(escape(reply->errorString()));
117
118 /* Note, we delete reply before we exit this function with error(). */
119 delete reply;
120
121 const QSourceLocation location(uri);
122
123 if(context)
124 context->error(errorMessage, ReportContext::FODC0002, location);
125
126 return 0;
127 }
128 else
129 return reply;
130}
131
132bool AccelTreeResourceLoader::streamToReceiver(QIODevice *const dev,
133 QAbstractXmlReceiver *const receiver,
134 const NamePool::Ptr &np,
135 const ReportContext::Ptr &context,
136 const QUrl &uri)
137{
138 Q_ASSERT(dev);
139 Q_ASSERT(receiver);
140 Q_ASSERT(np);
141
142 QXmlStreamReader reader(dev);
143
144 /* Optimize: change NamePool to take QStringRef such that we don't have to call toString() below. That
145 * will save us a gazillion of temporary QStrings. */
146
147 while(!reader.atEnd())
148 {
149 reader.readNext();
150
151 switch(reader.tokenType())
152 {
153 case QXmlStreamReader::StartElement:
154 {
155 /* Send the name. */
156 receiver->startElement(np->allocateQName(reader.namespaceUri().toString(), reader.name().toString(),
157 reader.prefix().toString()));
158
159 /* Send namespace declarations. */
160 const QXmlStreamNamespaceDeclarations &nss = reader.namespaceDeclarations();
161
162 /* The far most common case, is for it to be empty. */
163 if(!nss.isEmpty())
164 {
165 const int len = nss.size();
166
167 for(int i = 0; i < len; ++i)
168 {
169 const QXmlStreamNamespaceDeclaration &ns = nss.at(i);
170 receiver->namespaceBinding(np->allocateBinding(ns.prefix().toString(), ns.namespaceUri().toString()));
171 }
172 }
173
174 /* Send attributes. */
175 const QXmlStreamAttributes &attrs = reader.attributes();
176 const int len = attrs.size();
177
178 for(int i = 0; i < len; ++i)
179 {
180 const QXmlStreamAttribute &attr = attrs.at(i);
181
182 receiver->attribute(np->allocateQName(attr.namespaceUri().toString(), attr.name().toString(),
183 attr.prefix().toString()),
184 attr.value());