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 "qabstractfloat_p.h" |
41 | #include "qanyuri_p.h" |
42 | #include "qboolean_p.h" |
43 | #include "qbuiltintypes_p.h" |
44 | #include "qcommonnamespaces_p.h" |
45 | #include "qcommonvalues_p.h" |
46 | #include "qliteral_p.h" |
47 | #include "qatomicstring_p.h" |
48 | |
49 | #include "qnodefns_p.h" |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | using namespace QPatternist; |
54 | |
55 | Item NameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
56 | { |
57 | const Item node(m_operands.first()->evaluateSingleton(context)); |
58 | |
59 | if(node) |
60 | { |
61 | const QXmlName name(node.asNode().name()); |
62 | |
63 | if(name.isNull()) |
64 | return CommonValues::EmptyString; |
65 | else |
66 | return AtomicString::fromValue(value: context->namePool()->toLexical(qName: name)); |
67 | } |
68 | else |
69 | return CommonValues::EmptyString; |
70 | } |
71 | |
72 | Item LocalNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
73 | { |
74 | const Item node(m_operands.first()->evaluateSingleton(context)); |
75 | |
76 | if(node) |
77 | { |
78 | const QXmlName name(node.asNode().name()); |
79 | |
80 | if(name.isNull()) |
81 | return CommonValues::EmptyString; |
82 | else |
83 | return AtomicString::fromValue(value: context->namePool()->stringForLocalName(code: name.localName())); |
84 | } |
85 | else |
86 | return CommonValues::EmptyString; |
87 | } |
88 | |
89 | Item NamespaceURIFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
90 | { |
91 | const Item node(m_operands.first()->evaluateSingleton(context)); |
92 | |
93 | if(node) |
94 | { |
95 | const QXmlName name(node.asNode().name()); |
96 | |
97 | if(name.isNull()) |
98 | return CommonValues::EmptyAnyURI; |
99 | else |
100 | return toItem(atomicValue: AnyURI::fromValue(value: context->namePool()->stringForNamespace(code: name.namespaceURI()))); |
101 | } |
102 | else |
103 | return CommonValues::EmptyAnyURI; |
104 | } |
105 | |
106 | Item NumberFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
107 | { |
108 | const Item item(m_operands.first()->evaluateSingleton(context)); |
109 | |
110 | if(!item) |
111 | return CommonValues::DoubleNaN; |
112 | |
113 | const Item val(cast(sourceValue: item, context)); |
114 | Q_ASSERT(val); |
115 | |
116 | if(val.as<AtomicValue>()->hasError()) |
117 | return CommonValues::DoubleNaN; |
118 | else |
119 | return val; |
120 | } |
121 | |
122 | Expression::Ptr NumberFN::typeCheck(const StaticContext::Ptr &context, |
123 | const SequenceType::Ptr &reqType) |
124 | { |
125 | const Expression::Ptr me(FunctionCall::typeCheck(context, reqType)); |
126 | const ItemType::Ptr sourceType(m_operands.first()->staticType()->itemType()); |
127 | |
128 | if(BuiltinTypes::xsDouble->xdtTypeMatches(other: sourceType)) |
129 | { |
130 | /* The operand is already xs:double, no need for fn:number(). */ |
131 | return m_operands.first()->typeCheck(context, reqType); |
132 | } |
133 | else if(prepareCasting(context, sourceType)) |
134 | return me; |
135 | else |
136 | { |
137 | /* Casting to xs:double will never succeed and we would always return NaN.*/ |
138 | return wrapLiteral(item: CommonValues::DoubleNaN, context, r: this)->typeCheck(context, reqType); |
139 | } |
140 | } |
141 | |
142 | bool LangFN::isLangMatch(const QString &candidate, const QString &toMatch) |
143 | { |
144 | if(QString::compare(s1: candidate, s2: toMatch, cs: Qt::CaseInsensitive) == 0) |
145 | return true; |
146 | |
147 | return candidate.startsWith(s: toMatch, cs: Qt::CaseInsensitive) |
148 | && candidate.length() > toMatch.length() |
149 | && candidate.at(i: toMatch.length()) == QLatin1Char('-'); |
150 | } |
151 | |
152 | Item LangFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
153 | { |
154 | const Item langArg(m_operands.first()->evaluateSingleton(context)); |
155 | const QString lang(langArg ? langArg.stringValue() : QString()); |
156 | |
157 | const QXmlName xmlLang(StandardNamespaces::xml, StandardLocalNames::lang, StandardPrefixes::xml); |
158 | const QXmlNodeModelIndex langNode(m_operands.at(i: 1)->evaluateSingleton(context).asNode()); |
159 | |
160 | const QXmlNodeModelIndex::Iterator::Ptr ancestors(langNode.iterate(axis: QXmlNodeModelIndex::AxisAncestorOrSelf)); |
161 | QXmlNodeModelIndex ancestor(ancestors->next()); |
162 | |
163 | while(!ancestor.isNull()) |
164 | { |
165 | const QXmlNodeModelIndex::Iterator::Ptr attributes(ancestor.iterate(axis: QXmlNodeModelIndex::AxisAttribute)); |
166 | QXmlNodeModelIndex attribute(attributes->next()); |
167 | |
168 | while(!attribute.isNull()) |
169 | { |
170 | Q_ASSERT(attribute.kind() == QXmlNodeModelIndex::Attribute); |
171 | |
172 | if(attribute.name() == xmlLang) |
173 | { |
174 | if(isLangMatch(candidate: attribute.stringValue(), toMatch: lang)) |
175 | return CommonValues::BooleanTrue; |
176 | else |
177 | return CommonValues::BooleanFalse; |
178 | } |
179 | |
180 | attribute = attributes->next(); |
181 | } |
182 | |
183 | ancestor = ancestors->next(); |
184 | } |
185 | |
186 | return CommonValues::BooleanFalse; |
187 | } |
188 | |
189 | Item RootFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
190 | { |
191 | const Item arg(m_operands.first()->evaluateSingleton(context)); |
192 | |
193 | if(arg) |
194 | return arg.asNode().root(); |
195 | else |
196 | return Item(); |
197 | } |
198 | |
199 | SequenceType::Ptr RootFN::staticType() const |
200 | { |
201 | if(m_operands.isEmpty()) |
202 | return makeGenericSequenceType(itemType: BuiltinTypes::node, cardinality: Cardinality::exactlyOne()); |
203 | else |
204 | return makeGenericSequenceType(itemType: BuiltinTypes::node, cardinality: m_operands.first()->staticType()->cardinality().toWithoutMany()); |
205 | } |
206 | |
207 | QT_END_NAMESPACE |
208 | |