1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtCore/QFile>
43 #include <QtCore/QTextCodec>
44 #include <QtCore/QTimer>
45 #include <QtCore/QXmlStreamReader>
47 #include <QtNetwork/QNetworkRequest>
49 #include "qatomicstring_p.h"
50 #include "qautoptr_p.h"
51 #include "qcommonsequencetypes_p.h"
53 #include "qacceltreeresourceloader_p.h"
57 using namespace QPatternist;
59 AccelTreeResourceLoader::AccelTreeResourceLoader(const NamePool::Ptr &np,
60 const NetworkAccessDelegator::Ptr &manager,
61 AccelTreeBuilder<true>::Features features)
63 , m_networkAccessDelegator(manager)
64 , m_features(features)
67 Q_ASSERT(m_networkAccessDelegator);
70 bool AccelTreeResourceLoader::retrieveDocument(const QUrl &uri,
71 const ReportContext::Ptr &context)
73 Q_ASSERT(uri.isValid());
74 AccelTreeBuilder<true> builder(uri, uri, m_namePool, context.data(), m_features);
76 const AutoPtr<QNetworkReply> reply(load(uri, m_networkAccessDelegator, context));
82 success = streamToReceiver(reply.data(), &builder, m_namePool, context, uri);
84 m_loadedDocuments.insert(uri, builder.builtDocument());
88 bool AccelTreeResourceLoader::retrieveDocument(QIODevice *source, const QUrl &documentUri, const ReportContext::Ptr &context)
91 Q_ASSERT(source->isReadable());
92 Q_ASSERT(documentUri.isValid());
94 AccelTreeBuilder<true> builder(documentUri, documentUri, m_namePool, context.data(), m_features);
97 success = streamToReceiver(source, &builder, m_namePool, context, documentUri);
99 m_loadedDocuments.insert(documentUri, builder.builtDocument());
104 QNetworkReply *AccelTreeResourceLoader::load(const QUrl &uri,
105 const NetworkAccessDelegator::Ptr &networkDelegator,
106 const ReportContext::Ptr &context, ErrorHandling errorHandling)
109 networkDelegator->managerFor(uri),
110 context, errorHandling);
113 QNetworkReply *AccelTreeResourceLoader::load(const QUrl &uri,
114 QNetworkAccessManager *const networkManager,
115 const ReportContext::Ptr &context, ErrorHandling errorHandling)
118 Q_ASSERT(networkManager);
119 Q_ASSERT(uri.isValid());
121 NetworkLoop networkLoop;
123 QNetworkRequest request(uri);
124 QNetworkReply *const reply = networkManager->get(request);
125 networkLoop.connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(error(QNetworkReply::NetworkError)));
126 networkLoop.connect(reply, SIGNAL(finished()), SLOT(finished()));
128 if(networkLoop.exec(QEventLoop::ExcludeUserInputEvents))
130 const QString errorMessage(escape(reply->errorString()));
132 /* Note, we delete reply before we exit this function with error(). */
135 const QSourceLocation location(uri);
137 if(context && (errorHandling == FailOnError))
138 context->error(errorMessage, ReportContext::FODC0002, location);
146 bool AccelTreeResourceLoader::streamToReceiver(QIODevice *const dev,
147 AccelTreeBuilder<true> *const receiver,
148 const NamePool::Ptr &np,
149 const ReportContext::Ptr &context,
156 QXmlStreamReader reader(dev);
158 /* Optimize: change NamePool to take QStringRef such that we don't have to call toString() below. That
159 * will save us a gazillion of temporary QStrings. */
161 while(!reader.atEnd())
165 switch(reader.tokenType())
167 case QXmlStreamReader::StartElement:
170 receiver->startElement(np->allocateQName(reader.namespaceUri().toString(), reader.name().toString(),
171 reader.prefix().toString()), reader.lineNumber(), reader.columnNumber());
173 /* Send namespace declarations. */
174 const QXmlStreamNamespaceDeclarations &nss = reader.namespaceDeclarations();
176 /* The far most common case, is for it to be empty. */
179 const int len = nss.size();
181 for(int i = 0; i < len; ++i)
183 const QXmlStreamNamespaceDeclaration &ns = nss.at(i);
184 receiver->namespaceBinding(np->allocateBinding(ns.prefix().toString(), ns.namespaceUri().toString()));
188 /* Send attributes. */
189 const QXmlStreamAttributes &attrs = reader.attributes();
190 const int len = attrs.size();
192 for(int i = 0; i < len; ++i)
194 const QXmlStreamAttribute &attr = attrs.at(i);
196 receiver->attribute(np->allocateQName(attr.namespaceUri().toString(), attr.name().toString(),
197 attr.prefix().toString()),
203 case QXmlStreamReader::EndElement:
205 receiver->endElement();
208 case QXmlStreamReader::Characters:
210 if(reader.isWhitespace())
211 receiver->whitespaceOnly(reader.text());
213 receiver->characters(reader.text());
217 case QXmlStreamReader::Comment:
219 receiver->comment(reader.text().toString());
222 case QXmlStreamReader::ProcessingInstruction:
224 receiver->processingInstruction(np->allocateQName(QString(), reader.processingInstructionTarget().toString()),
225 reader.processingInstructionData().toString());
228 case QXmlStreamReader::StartDocument:
230 receiver->startDocument();
233 case QXmlStreamReader::EndDocument:
235 receiver->endDocument();
238 case QXmlStreamReader::EntityReference:
240 case QXmlStreamReader::DTD:
242 /* We just ignore any DTD and entity references. */
245 case QXmlStreamReader::Invalid:
248 context->error(escape(reader.errorString()), ReportContext::FODC0002, QSourceLocation(uri, reader.lineNumber(), reader.columnNumber()));
252 case QXmlStreamReader::NoToken:
254 Q_ASSERT_X(false, Q_FUNC_INFO,
255 "This token is never expected to be received.");
264 Item AccelTreeResourceLoader::openDocument(const QUrl &uri,
265 const ReportContext::Ptr &context)
267 const AccelTree::Ptr doc(m_loadedDocuments.value(uri));
270 return doc->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
273 if(retrieveDocument(uri, context))
274 return m_loadedDocuments.value(uri)->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
280 Item AccelTreeResourceLoader::openDocument(QIODevice *source, const QUrl &documentUri,
281 const ReportContext::Ptr &context)
283 const AccelTree::Ptr doc(m_loadedDocuments.value(documentUri));
286 return doc->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
289 if(retrieveDocument(source, documentUri, context))
290 return m_loadedDocuments.value(documentUri)->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
296 SequenceType::Ptr AccelTreeResourceLoader::announceDocument(const QUrl &uri, const Usage)
298 // TODO deal with the usage thingy
299 Q_ASSERT(uri.isValid());
300 Q_ASSERT(!uri.isRelative());
301 Q_UNUSED(uri); /* Needed when compiling in release mode. */
303 return CommonSequenceTypes::ZeroOrOneDocumentNode;
306 bool AccelTreeResourceLoader::isDocumentAvailable(const QUrl &uri)
308 return retrieveDocument(uri, ReportContext::Ptr());
311 static inline uint qHash(const QPair<QUrl, QString> &desc)
313 /* Probably a lousy hash. */
314 return qHash(desc.first) + qHash(desc.second);
317 bool AccelTreeResourceLoader::retrieveUnparsedText(const QUrl &uri,
318 const QString &encoding,
319 const ReportContext::Ptr &context,
320 const SourceLocationReflection *const where)
322 const AutoPtr<QNetworkReply> reply(load(uri, m_networkAccessDelegator, context));
327 const QTextCodec * codec;
328 if(encoding.isEmpty())
330 /* XSL Transformations (XSLT) Version 2.0 16.2 Reading Text Files:
332 * "if the media type of the resource is text/xml or application/xml
333 * (see [RFC2376]), or if it matches the conventions text/\*+xml or
334 * application/\*+xml (see [RFC3023] and/or its successors), then the
335 * encoding is recognized as specified in [XML 1.0]"
337 codec = QTextCodec::codecForMib(106);
341 codec = QTextCodec::codecForName(encoding.toLatin1());
344 context->error(QtXmlPatterns::tr("%1 is an unsupported encoding.").arg(formatURI(encoding)),
345 ReportContext::XTDE1190,
352 QTextCodec::ConverterState converterState;
353 const QByteArray inData(reply->readAll());
354 const QString result(codec->toUnicode(inData.constData(), inData.length(), &converterState));
356 if(converterState.invalidChars)
360 context->error(QtXmlPatterns::tr("%1 contains octets which are disallowed in "
361 "the requested encoding %2.").arg(formatURI(uri),
362 formatURI(encoding)),
363 ReportContext::XTDE1190,
370 const int len = result.length();
371 /* This code is a candidate for threading. Divide and conqueror. */
372 for(int i = 0; i < len; ++i)
374 if(!QXmlUtils::isChar(result.at(i)))
378 context->error(QtXmlPatterns::tr("The codepoint %1, occurring in %2 using encoding %3, "
379 "is an invalid XML character.").arg(formatData(result.at(i)),
381 formatURI(encoding)),
382 ReportContext::XTDE1190,
390 m_unparsedTexts.insert(qMakePair(uri, encoding), result);
394 bool AccelTreeResourceLoader::isUnparsedTextAvailable(const QUrl &uri,
395 const QString &encoding)
397 return retrieveUnparsedText(uri, encoding, ReportContext::Ptr(), 0);
400 Item AccelTreeResourceLoader::openUnparsedText(const QUrl &uri,
401 const QString &encoding,
402 const ReportContext::Ptr &context,
403 const SourceLocationReflection *const where)
405 const QString &text = m_unparsedTexts.value(qMakePair(uri, encoding));
409 if(retrieveUnparsedText(uri, encoding, context, where))
410 return openUnparsedText(uri, encoding, context, where);
415 return AtomicString::fromValue(text);
418 QSet<QUrl> AccelTreeResourceLoader::deviceURIs() const
420 QHash<QUrl, AccelTree::Ptr>::const_iterator it(m_loadedDocuments.constBegin());
421 const QHash<QUrl, AccelTree::Ptr>::const_iterator end(m_loadedDocuments.constEnd());
426 if(it.key().toString().startsWith(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:")))
427 retval.insert(it.key());
435 void AccelTreeResourceLoader::clear(const QUrl &uri)
437 m_loadedDocuments.remove(uri);