| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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 |
|
|---|
| 56 | QT_BEGIN_NAMESPACE
|
|---|
| 57 |
|
|---|
| 58 | using namespace QPatternist;
|
|---|
| 59 |
|
|---|
| 60 | static inline uint qHash(const QUrl &uri)
|
|---|
| 61 | {
|
|---|
| 62 | return qHash(uri.toString());
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | AccelTreeResourceLoader::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 |
|
|---|
| 73 | bool 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 |
|
|---|
| 91 | QNetworkReply *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 |
|
|---|
| 100 | QNetworkReply *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 |
|
|---|
| 132 | bool 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());
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | continue;
|
|---|
| 188 | }
|
|---|
| 189 | case QXmlStreamReader::EndElement:
|
|---|
| 190 | {
|
|---|
| 191 | receiver->endElement();
|
|---|
| 192 | continue;
|
|---|
| 193 | }
|
|---|
| 194 | case QXmlStreamReader::Characters:
|
|---|
| 195 | {
|
|---|
| 196 | if(reader.isWhitespace())
|
|---|
| 197 | receiver->whitespaceOnly(reader.text());
|
|---|
| 198 | else
|
|---|
| 199 | receiver->characters(reader.text());
|
|---|
| 200 |
|
|---|
| 201 | continue;
|
|---|
| 202 | }
|
|---|
| 203 | case QXmlStreamReader::Comment:
|
|---|
| 204 | {
|
|---|
| 205 | receiver->comment(reader.text().toString());
|
|---|
| 206 | continue;
|
|---|
| 207 | }
|
|---|
| 208 | case QXmlStreamReader::ProcessingInstruction:
|
|---|
| 209 | {
|
|---|
| 210 | receiver->processingInstruction(np->allocateQName(QString(), reader.processingInstructionTarget().toString()),
|
|---|
| 211 | reader.processingInstructionData().toString());
|
|---|
| 212 | continue;
|
|---|
| 213 | }
|
|---|
| 214 | case QXmlStreamReader::StartDocument:
|
|---|
| 215 | {
|
|---|
| 216 | receiver->startDocument();
|
|---|
| 217 | continue;
|
|---|
| 218 | }
|
|---|
| 219 | case QXmlStreamReader::EndDocument:
|
|---|
| 220 | {
|
|---|
| 221 | receiver->endDocument();
|
|---|
| 222 | continue;
|
|---|
| 223 | }
|
|---|
| 224 | case QXmlStreamReader::EntityReference:
|
|---|
| 225 | /* Fallthrough. */
|
|---|
| 226 | case QXmlStreamReader::DTD:
|
|---|
| 227 | {
|
|---|
| 228 | /* We just ignore any DTD and entity references. */
|
|---|
| 229 | continue;
|
|---|
| 230 | }
|
|---|
| 231 | case QXmlStreamReader::Invalid:
|
|---|
| 232 | {
|
|---|
| 233 | if(context)
|
|---|
| 234 | context->error(escape(reader.errorString()), ReportContext::FODC0002, QSourceLocation(uri, reader.lineNumber(), reader.columnNumber()));
|
|---|
| 235 |
|
|---|
| 236 | return false;
|
|---|
| 237 | }
|
|---|
| 238 | case QXmlStreamReader::NoToken:
|
|---|
| 239 | {
|
|---|
| 240 | Q_ASSERT_X(false, Q_FUNC_INFO,
|
|---|
| 241 | "This token is never expected to be received.");
|
|---|
| 242 | return false;
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | return true;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | Item AccelTreeResourceLoader::openDocument(const QUrl &uri,
|
|---|
| 251 | const ReportContext::Ptr &context)
|
|---|
| 252 | {
|
|---|
| 253 | const AccelTree::Ptr doc(m_loadedDocuments.value(uri));
|
|---|
| 254 |
|
|---|
| 255 | if(doc)
|
|---|
| 256 | return doc->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
|
|---|
| 257 | else
|
|---|
| 258 | {
|
|---|
| 259 | if(retrieveDocument(uri, context))
|
|---|
| 260 | return m_loadedDocuments.value(uri)->root(QXmlNodeModelIndex()); /* Pass in dummy object. We know AccelTree doesn't use it. */
|
|---|
| 261 | else
|
|---|
| 262 | return Item();
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | SequenceType::Ptr AccelTreeResourceLoader::announceDocument(const QUrl &uri, const Usage)
|
|---|
| 267 | {
|
|---|
| 268 | // TODO deal with the usage thingy
|
|---|
| 269 | Q_ASSERT(uri.isValid());
|
|---|
| 270 | Q_ASSERT(!uri.isRelative());
|
|---|
| 271 | Q_UNUSED(uri); /* Needed when compiling in release mode. */
|
|---|
| 272 |
|
|---|
| 273 | return CommonSequenceTypes::ZeroOrOneDocumentNode;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | bool AccelTreeResourceLoader::isDocumentAvailable(const QUrl &uri)
|
|---|
| 277 | {
|
|---|
| 278 | return retrieveDocument(uri, ReportContext::Ptr());
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | static inline uint qHash(const QPair<QUrl, QString> &desc)
|
|---|
| 282 | {
|
|---|
| 283 | /* Probably a lousy hash. */
|
|---|
| 284 | return qHash(desc.first) + qHash(desc.second);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | bool AccelTreeResourceLoader::retrieveUnparsedText(const QUrl &uri,
|
|---|
| 288 | const QString &encoding,
|
|---|
| 289 | const ReportContext::Ptr &context,
|
|---|
| 290 | const SourceLocationReflection *const where)
|
|---|
| 291 | {
|
|---|
| 292 | const AutoPtr<QNetworkReply> reply(load(uri, m_networkAccessDelegator, context));
|
|---|
| 293 |
|
|---|
| 294 | if(!reply)
|
|---|
| 295 | return false;
|
|---|
| 296 |
|
|---|
| 297 | const QTextCodec * codec;
|
|---|
| 298 | if(encoding.isEmpty())
|
|---|
| 299 | {
|
|---|
| 300 | /* XSL Transformations (XSLT) Version 2.0 16.2 Reading Text Files:
|
|---|
| 301 | *
|
|---|
| 302 | * "if the media type of the resource is text/xml or application/xml
|
|---|
| 303 | * (see [RFC2376]), or if it matches the conventions text/\*+xml or
|
|---|
| 304 | * application/\*+xml (see [RFC3023] and/or its successors), then the
|
|---|
| 305 | * encoding is recognized as specified in [XML 1.0]"
|
|---|
| 306 | */
|
|---|
| 307 | codec = QTextCodec::codecForMib(106);
|
|---|
| 308 | }
|
|---|
| 309 | else
|
|---|
| 310 | {
|
|---|
| 311 | codec = QTextCodec::codecForName(encoding.toLatin1());
|
|---|
| 312 | if(codec && context)
|
|---|
| 313 | {
|
|---|
| 314 | context->error(QtXmlPatterns::tr("%1 is an unsupported encoding.").arg(formatURI(encoding)),
|
|---|
| 315 | ReportContext::XTDE1190,
|
|---|
| 316 | where);
|
|---|
| 317 | }
|
|---|
| 318 | else
|
|---|
| 319 | return false;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | QTextCodec::ConverterState converterState;
|
|---|
| 323 | const QByteArray inData(reply->readAll());
|
|---|
| 324 | const QString result(codec->toUnicode(inData.constData(), inData.length(), &converterState));
|
|---|
| 325 |
|
|---|
| 326 | if(converterState.invalidChars)
|
|---|
| 327 | {
|
|---|
| 328 | if(context)
|
|---|
| 329 | {
|
|---|
| 330 | context->error(QtXmlPatterns::tr("%1 contains octets which are disallowed in "
|
|---|
| 331 | "the requested encoding %2.").arg(formatURI(uri),
|
|---|
| 332 | formatURI(encoding)),
|
|---|
| 333 | ReportContext::XTDE1190,
|
|---|
| 334 | where);
|
|---|
| 335 | }
|
|---|
| 336 | else
|
|---|
| 337 | return false;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | const int len = result.length();
|
|---|
| 341 | /* This code is a candidate for threading. Divide and conqueror. */
|
|---|
| 342 | for(int i = 0; i < len; ++i)
|
|---|
| 343 | {
|
|---|
| 344 | if(!QXmlUtils::isChar(result.at(i)))
|
|---|
| 345 | {
|
|---|
| 346 | if(context)
|
|---|
| 347 | {
|
|---|
| 348 | context->error(QtXmlPatterns::tr("The codepoint %1, occurring in %2 using encoding %3, "
|
|---|
| 349 | "is an invalid XML character.").arg(formatData(result.at(i)),
|
|---|
| 350 | formatURI(uri),
|
|---|
| 351 | formatURI(encoding)),
|
|---|
| 352 | ReportContext::XTDE1190,
|
|---|
| 353 | where);
|
|---|
| 354 | }
|
|---|
| 355 | else
|
|---|
| 356 | return false;
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | m_unparsedTexts.insert(qMakePair(uri, encoding), result);
|
|---|
| 361 | return true;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | bool AccelTreeResourceLoader::isUnparsedTextAvailable(const QUrl &uri,
|
|---|
| 365 | const QString &encoding)
|
|---|
| 366 | {
|
|---|
| 367 | return retrieveUnparsedText(uri, encoding, ReportContext::Ptr(), 0);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | Item AccelTreeResourceLoader::openUnparsedText(const QUrl &uri,
|
|---|
| 371 | const QString &encoding,
|
|---|
| 372 | const ReportContext::Ptr &context,
|
|---|
| 373 | const SourceLocationReflection *const where)
|
|---|
| 374 | {
|
|---|
| 375 | const QString &text = m_unparsedTexts.value(qMakePair(uri, encoding));
|
|---|
| 376 |
|
|---|
| 377 | if(text.isNull())
|
|---|
| 378 | {
|
|---|
| 379 | if(retrieveUnparsedText(uri, encoding, context, where))
|
|---|
| 380 | return openUnparsedText(uri, encoding, context, where);
|
|---|
| 381 | else
|
|---|
| 382 | return Item();
|
|---|
| 383 | }
|
|---|
| 384 | else
|
|---|
| 385 | return AtomicString::fromValue(text);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | QSet<QUrl> AccelTreeResourceLoader::deviceURIs() const
|
|---|
| 389 | {
|
|---|
| 390 | QHash<QUrl, AccelTree::Ptr>::const_iterator it(m_loadedDocuments.constBegin());
|
|---|
| 391 | const QHash<QUrl, AccelTree::Ptr>::const_iterator end(m_loadedDocuments.constEnd());
|
|---|
| 392 | QSet<QUrl> retval;
|
|---|
| 393 |
|
|---|
| 394 | while (it != end)
|
|---|
| 395 | {
|
|---|
| 396 | if(it.key().toString().startsWith(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:")))
|
|---|
| 397 | retval.insert(it.key());
|
|---|
| 398 |
|
|---|
| 399 | ++it;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | return retval;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | void AccelTreeResourceLoader::clear(const QUrl &uri)
|
|---|
| 406 | {
|
|---|
| 407 | m_loadedDocuments.remove(uri);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | QT_END_NAMESPACE
|
|---|
| 411 |
|
|---|