| 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 | #include "qanyuri_p.h" |
| 41 | #include "qbuiltintypes_p.h" |
| 42 | #include "qcommonvalues_p.h" |
| 43 | #include "qpatternistlocale_p.h" |
| 44 | #include "qnodenamespaceresolver_p.h" |
| 45 | #include "qqnameconstructor_p.h" |
| 46 | #include "qqnamevalue_p.h" |
| 47 | #include "qatomicstring_p.h" |
| 48 | #include "qxpathhelper_p.h" |
| 49 | |
| 50 | #include "qqnamefns_p.h" |
| 51 | |
| 52 | QT_BEGIN_NAMESPACE |
| 53 | |
| 54 | using namespace QPatternist; |
| 55 | |
| 56 | Item QNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 57 | { |
| 58 | const Item paramURI(m_operands.first()->evaluateSingleton(context)); |
| 59 | const QString paramQName(m_operands.last()->evaluateSingleton(context).stringValue()); |
| 60 | |
| 61 | QString ns; |
| 62 | if(paramURI) |
| 63 | ns = paramURI.stringValue(); |
| 64 | |
| 65 | if(!XPathHelper::isQName(qName: paramQName)) |
| 66 | { |
| 67 | context->error(message: QtXmlPatterns::tr(sourceText: "%1 is an invalid %2" ).arg(args: formatData(data: paramQName), |
| 68 | args: formatType(np: context->namePool(), type: BuiltinTypes::xsQName)), |
| 69 | errorCode: ReportContext::FOCA0002, reflection: this); |
| 70 | return Item(); |
| 71 | } |
| 72 | |
| 73 | QString prefix; |
| 74 | QString lname; |
| 75 | XPathHelper::splitQName(qName: paramQName, prefix, localName&: lname); |
| 76 | const QXmlName n(context->namePool()->allocateQName(uri: ns, localName: lname, prefix)); |
| 77 | |
| 78 | if(ns.isEmpty()) |
| 79 | { |
| 80 | if(prefix.isEmpty()) |
| 81 | return toItem(atomicValue: QNameValue::fromValue(np: context->namePool(), name: n)); |
| 82 | else |
| 83 | { |
| 84 | context->error(message: QtXmlPatterns::tr( |
| 85 | sourceText: "If the first argument is the empty sequence or " |
| 86 | "a zero-length string (no namespace), a prefix " |
| 87 | "cannot be specified. Prefix %1 was specified." ) |
| 88 | .arg(a: formatKeyword(keyword: prefix)), |
| 89 | errorCode: ReportContext::FOCA0002, reflection: this); |
| 90 | return Item(); /* Silence compiler warning. */ |
| 91 | } |
| 92 | } |
| 93 | else |
| 94 | return toItem(atomicValue: QNameValue::fromValue(np: context->namePool(), name: n)); |
| 95 | } |
| 96 | |
| 97 | Item ResolveQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 98 | { |
| 99 | const Item itemName(m_operands.first()->evaluateSingleton(context)); |
| 100 | |
| 101 | if(!itemName) |
| 102 | return Item(); |
| 103 | |
| 104 | const NamespaceResolver::Ptr resolver(new NodeNamespaceResolver(m_operands.last()->evaluateSingleton(context))); |
| 105 | const QString strName(itemName.stringValue()); |
| 106 | const QXmlName name = QNameConstructor::expandQName<DynamicContext::Ptr, |
| 107 | ReportContext::FOCA0002, |
| 108 | ReportContext::FONS0004>(lexicalQName: strName, |
| 109 | context, |
| 110 | nsResolver: resolver, |
| 111 | r: this); |
| 112 | |
| 113 | return toItem(atomicValue: QNameValue::fromValue(np: context->namePool(), name)); |
| 114 | } |
| 115 | |
| 116 | Item PrefixFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 117 | { |
| 118 | const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>()); |
| 119 | if(!arg) |
| 120 | return Item(); |
| 121 | |
| 122 | const QString prefix(context->namePool()->stringForPrefix(code: arg->qName().prefix())); |
| 123 | |
| 124 | if(prefix.isEmpty()) |
| 125 | return Item(); |
| 126 | else |
| 127 | return AtomicString::fromValue(value: context->namePool()->stringForPrefix(code: arg->qName().prefix())); |
| 128 | } |
| 129 | |
| 130 | Item LocalNameFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 131 | { |
| 132 | const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>()); |
| 133 | return arg ? toItem(atomicValue: AtomicString::fromValue(value: context->namePool()->stringForLocalName(code: arg->qName().localName()))) : Item(); |
| 134 | } |
| 135 | |
| 136 | Item NamespaceURIFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 137 | { |
| 138 | const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>()); |
| 139 | return arg ? toItem(atomicValue: AnyURI::fromValue(value: context->namePool()->stringForNamespace(code: arg->qName().namespaceURI()))) : Item(); |
| 140 | } |
| 141 | |
| 142 | Item NamespaceURIForPrefixFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 143 | { |
| 144 | const Item prefixItem(m_operands.first()->evaluateSingleton(context)); |
| 145 | QXmlName::PrefixCode prefix; |
| 146 | |
| 147 | if(prefixItem) |
| 148 | prefix = context->namePool()->allocatePrefix(prefix: prefixItem.stringValue()); |
| 149 | else |
| 150 | prefix = StandardPrefixes::empty; |
| 151 | |
| 152 | const Item eleItem(m_operands.last()->evaluateSingleton(context)); |
| 153 | Q_ASSERT(eleItem); |
| 154 | |
| 155 | const QXmlName::NamespaceCode ns = eleItem.asNode().namespaceForPrefix(prefix); |
| 156 | |
| 157 | if(ns == NamespaceResolver::NoBinding) |
| 158 | { |
| 159 | /* This is a bit tricky. The default namespace is not considered an in-scope binding |
| 160 | * on a node, but the specification for this function do consider it a binding and therefore |
| 161 | * the empty string. */ |
| 162 | if(prefix == StandardPrefixes::empty) |
| 163 | return CommonValues::EmptyString; |
| 164 | else |
| 165 | return Item(); |
| 166 | } |
| 167 | else |
| 168 | return toItem(atomicValue: AnyURI::fromValue(value: context->namePool()->stringForNamespace(code: ns))); |
| 169 | } |
| 170 | |
| 171 | Item::Iterator::Ptr InScopePrefixesFN::evaluateSequence(const DynamicContext::Ptr &context) const |
| 172 | { |
| 173 | const Item e(m_operands.first()->evaluateSingleton(context)); |
| 174 | |
| 175 | const QVector<QXmlName> nbs(e.asNode().namespaceBindings()); |
| 176 | const int len = nbs.size(); |
| 177 | const NamePool::Ptr np(context->namePool()); |
| 178 | |
| 179 | QList<Item> result; |
| 180 | |
| 181 | for(int i = 0; i < len; ++i) |
| 182 | result.append(t: AtomicString::fromValue(value: np->stringForPrefix(code: nbs.at(i).prefix()))); |
| 183 | |
| 184 | return makeListIterator(list: result); |
| 185 | } |
| 186 | |
| 187 | QT_END_NAMESPACE |
| 188 | |