| 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 QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #include "qnamespacesupport_p.h" |
| 51 | |
| 52 | QT_BEGIN_NAMESPACE |
| 53 | |
| 54 | using namespace QPatternist; |
| 55 | |
| 56 | NamespaceSupport::NamespaceSupport() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | NamespaceSupport::NamespaceSupport(const NamePool::Ptr &namePool) |
| 61 | : m_namePool(namePool) |
| 62 | { |
| 63 | // the XML namespace |
| 64 | m_ns.insert(akey: StandardPrefixes::xml, avalue: StandardNamespaces::xml); |
| 65 | } |
| 66 | |
| 67 | void NamespaceSupport::setPrefix(const QXmlName::PrefixCode prefixCode, const QXmlName::NamespaceCode namespaceCode) |
| 68 | { |
| 69 | m_ns.insert(akey: prefixCode, avalue: namespaceCode); |
| 70 | } |
| 71 | |
| 72 | void NamespaceSupport::setPrefixes(const QXmlStreamNamespaceDeclarations &declarations) |
| 73 | { |
| 74 | for (int i = 0; i < declarations.count(); i++) { |
| 75 | const QXmlStreamNamespaceDeclaration declaration = declarations.at(i); |
| 76 | |
| 77 | const QXmlName::PrefixCode prefixCode = m_namePool->allocatePrefix(prefix: declaration.prefix().toString()); |
| 78 | const QXmlName::NamespaceCode namespaceCode = m_namePool->allocateNamespace(uri: declaration.namespaceUri().toString()); |
| 79 | m_ns.insert(akey: prefixCode, avalue: namespaceCode); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void NamespaceSupport::setTargetNamespace(const QXmlName::NamespaceCode namespaceCode) |
| 84 | { |
| 85 | m_ns.insert(akey: 0, avalue: namespaceCode); |
| 86 | } |
| 87 | |
| 88 | QXmlName::PrefixCode NamespaceSupport::prefix(const QXmlName::NamespaceCode namespaceCode) const |
| 89 | { |
| 90 | NamespaceHash::const_iterator itc, it = m_ns.constBegin(); |
| 91 | while ((itc=it) != m_ns.constEnd()) { |
| 92 | ++it; |
| 93 | if (*itc == namespaceCode) |
| 94 | return itc.key(); |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | QXmlName::NamespaceCode NamespaceSupport::uri(const QXmlName::PrefixCode prefixCode) const |
| 100 | { |
| 101 | return m_ns.value(akey: prefixCode); |
| 102 | } |
| 103 | |
| 104 | bool NamespaceSupport::processName(const QString& qname, NameType type, QXmlName &name) const |
| 105 | { |
| 106 | int len = qname.size(); |
| 107 | const QChar *data = qname.constData(); |
| 108 | for (int pos = 0; pos < len; ++pos) { |
| 109 | if (data[pos] == QLatin1Char(':')) { |
| 110 | const QXmlName::PrefixCode prefixCode = m_namePool->allocatePrefix(prefix: qname.left(n: pos)); |
| 111 | if (!m_ns.contains(akey: prefixCode)) |
| 112 | return false; |
| 113 | const QXmlName::NamespaceCode namespaceCode = uri(prefixCode); |
| 114 | const QXmlName::LocalNameCode localNameCode = m_namePool->allocateLocalName(ln: qname.mid(position: pos + 1)); |
| 115 | name = QXmlName(namespaceCode, localNameCode, prefixCode); |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // there was no ':' |
| 121 | QXmlName::NamespaceCode namespaceCode = 0; |
| 122 | // attributes don't take default namespace |
| 123 | if (type == ElementName && !m_ns.isEmpty()) { |
| 124 | namespaceCode = m_ns.value(akey: 0); // get default namespace |
| 125 | } |
| 126 | |
| 127 | const QXmlName::LocalNameCode localNameCode = m_namePool->allocateLocalName(ln: qname); |
| 128 | name = QXmlName(namespaceCode, localNameCode, 0); |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | void NamespaceSupport::pushContext() |
| 134 | { |
| 135 | m_nsStack.push(t: m_ns); |
| 136 | } |
| 137 | |
| 138 | void NamespaceSupport::popContext() |
| 139 | { |
| 140 | m_ns.clear(); |
| 141 | if(!m_nsStack.isEmpty()) |
| 142 | m_ns = m_nsStack.pop(); |
| 143 | } |
| 144 | |
| 145 | QList<QXmlName> NamespaceSupport::namespaceBindings() const |
| 146 | { |
| 147 | QList<QXmlName> bindings; |
| 148 | |
| 149 | for (auto it = m_ns.cbegin(), end = m_ns.cend(); it != end; ++it) |
| 150 | bindings.append(t: QXmlName(it.value(), StandardLocalNames::empty, it.key())); |
| 151 | |
| 152 | return bindings; |
| 153 | } |
| 154 | |
| 155 | QT_END_NAMESPACE |
| 156 | |