| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QCoreApplication> |
| 30 | #include <QFile> |
| 31 | #include <QtDebug> |
| 32 | #include <QXmlResultItems> |
| 33 | #include <QXmlNamePool> |
| 34 | |
| 35 | #include "Global.h" |
| 36 | |
| 37 | #include <private/qcommonsequencetypes_p.h> |
| 38 | #include <private/qxmldebug_p.h> |
| 39 | #include <private/qatomicstring_p.h> |
| 40 | |
| 41 | #include "ExternalSourceLoader.h" |
| 42 | |
| 43 | using namespace QPatternistSDK; |
| 44 | using namespace QPatternist; |
| 45 | |
| 46 | ExternalSourceLoader::ExternalSourceLoader(const VariableMap &varMap, |
| 47 | const QPatternist::ResourceLoader::Ptr &r) : m_variableMap(varMap) |
| 48 | , m_resourceLoader(r) |
| 49 | , m_query(Global::namePoolAsPublic()) |
| 50 | { |
| 51 | Q_ASSERT(m_resourceLoader); |
| 52 | } |
| 53 | |
| 54 | QPatternist::SequenceType::Ptr |
| 55 | ExternalSourceLoader::announceExternalVariable(const QXmlName name, |
| 56 | const QPatternist::SequenceType::Ptr &declaredType) |
| 57 | { |
| 58 | pDebug() << "ExternalSourceLoader::announceExternalVariable()" ; |
| 59 | Q_ASSERT(!name.isNull()); |
| 60 | Q_ASSERT(declaredType); |
| 61 | Q_UNUSED(declaredType); /* Needed when bulding in release mode. */ |
| 62 | |
| 63 | if(name.namespaceURI() == QPatternist::StandardNamespaces::empty) |
| 64 | { |
| 65 | const VariableValue variable(m_variableMap.value(akey: Global::namePool()->stringForLocalName(code: name.localName()))); |
| 66 | |
| 67 | if(variable.first.isEmpty()) |
| 68 | return QPatternist::SequenceType::Ptr(); |
| 69 | else |
| 70 | { |
| 71 | /* If announceDocument() can't load a document for uriForVar, it will return |
| 72 | * null, which we will too, which is fine, since we can't supply a value for |
| 73 | * this variable then. */ |
| 74 | if(variable.second == Document) |
| 75 | return m_resourceLoader->announceDocument(uri: variable.first, usageHint: QPatternist::ResourceLoader::WillUse); |
| 76 | else if(variable.second == URI) |
| 77 | { |
| 78 | return QPatternist::CommonSequenceTypes::ExactlyOneString; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | /* The type is Query, and we don't pre-load |
| 83 | * them. No particular reason, just not worth it. */ |
| 84 | return QPatternist::CommonSequenceTypes::ZeroOrMoreItems; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | return QPatternist::SequenceType::Ptr(); |
| 90 | } |
| 91 | |
| 92 | QPatternist::Item |
| 93 | ExternalSourceLoader::evaluateSingleton(const QXmlName name, |
| 94 | const QPatternist::DynamicContext::Ptr &context) |
| 95 | { |
| 96 | Q_ASSERT(!name.isNull()); |
| 97 | const VariableValue variable(m_variableMap.value(akey: Global::namePool()->stringForLocalName(code: name.localName()))); |
| 98 | |
| 99 | if(variable.second == Document) |
| 100 | { |
| 101 | Q_ASSERT_X(QFile::exists(variable.first.toLocalFile()), Q_FUNC_INFO, |
| 102 | qPrintable(QString::fromLatin1("The file %1 doesn't exist" ).arg(variable.first.toLocalFile()))); |
| 103 | Q_ASSERT_X(m_resourceLoader->openDocument(variable.first, context), Q_FUNC_INFO, |
| 104 | "We're supposed to have the value. If not, an error should have been issued at query compile time." ); |
| 105 | |
| 106 | return m_resourceLoader->openDocument(uri: variable.first, context); |
| 107 | } |
| 108 | else if(variable.second == Query) |
| 109 | { |
| 110 | /* Well, here we open the file and execute it. */ |
| 111 | m_query.setQuery(queryURI: QUrl::fromLocalFile(localfile: variable.first.toLocalFile())); |
| 112 | Q_ASSERT(m_query.isValid()); |
| 113 | |
| 114 | QXmlResultItems result; |
| 115 | m_query.evaluateTo(result: &result); |
| 116 | |
| 117 | return QPatternist::Item::fromPublic(i: result.next()); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | Q_ASSERT(variable.second == URI); |
| 122 | return QPatternist::AtomicString::fromValue(value: variable.first.toString()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // vim: et:ts=4:sw=4:sts=4 |
| 127 | |
| 128 | |