1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qabstractfloat_p.h"
43 #include "qanyuri_p.h"
44 #include "qboolean_p.h"
45 #include "qbuiltintypes_p.h"
46 #include "qcommonnamespaces_p.h"
47 #include "qcommonvalues_p.h"
48 #include "qliteral_p.h"
49 #include "qatomicstring_p.h"
51 #include "qnodefns_p.h"
55 using namespace QPatternist;
57 Item NameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
59 const Item node(m_operands.first()->evaluateSingleton(context));
63 const QXmlName name(node.asNode().name());
66 return CommonValues::EmptyString;
68 return AtomicString::fromValue(context->namePool()->toLexical(name));
71 return CommonValues::EmptyString;
74 Item LocalNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
76 const Item node(m_operands.first()->evaluateSingleton(context));
80 const QXmlName name(node.asNode().name());
83 return CommonValues::EmptyString;
85 return AtomicString::fromValue(context->namePool()->stringForLocalName(name.localName()));
88 return CommonValues::EmptyString;
91 Item NamespaceURIFN::evaluateSingleton(const DynamicContext::Ptr &context) const
93 const Item node(m_operands.first()->evaluateSingleton(context));
97 const QXmlName name(node.asNode().name());
100 return CommonValues::EmptyAnyURI;
102 return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(name.namespaceURI())));
105 return CommonValues::EmptyAnyURI;
108 Item NumberFN::evaluateSingleton(const DynamicContext::Ptr &context) const
110 const Item item(m_operands.first()->evaluateSingleton(context));
113 return CommonValues::DoubleNaN;
115 const Item val(cast(item, context));
118 if(val.as<AtomicValue>()->hasError())
119 return CommonValues::DoubleNaN;
124 Expression::Ptr NumberFN::typeCheck(const StaticContext::Ptr &context,
125 const SequenceType::Ptr &reqType)
127 const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
128 const ItemType::Ptr sourceType(m_operands.first()->staticType()->itemType());
130 if(BuiltinTypes::xsDouble->xdtTypeMatches(sourceType))
132 /* The operand is already xs:double, no need for fn:number(). */
133 return m_operands.first()->typeCheck(context, reqType);
135 else if(prepareCasting(context, sourceType))
139 /* Casting to xs:double will never succeed and we would always return NaN.*/
140 return wrapLiteral(CommonValues::DoubleNaN, context, this)->typeCheck(context, reqType);
144 bool LangFN::isLangMatch(const QString &candidate, const QString &toMatch)
146 if(QString::compare(candidate, toMatch, Qt::CaseInsensitive) == 0)
149 return candidate.startsWith(toMatch, Qt::CaseInsensitive)
150 && candidate.length() > toMatch.length()
151 && candidate.at(toMatch.length()) == QLatin1Char('-');
154 Item LangFN::evaluateSingleton(const DynamicContext::Ptr &context) const
156 const Item langArg(m_operands.first()->evaluateSingleton(context));
157 const QString lang(langArg ? langArg.stringValue() : QString());
159 const QXmlName xmlLang(StandardNamespaces::xml, StandardLocalNames::lang, StandardPrefixes::xml);
160 const QXmlNodeModelIndex langNode(m_operands.at(1)->evaluateSingleton(context).asNode());
162 const QXmlNodeModelIndex::Iterator::Ptr ancestors(langNode.iterate(QXmlNodeModelIndex::AxisAncestorOrSelf));
163 QXmlNodeModelIndex ancestor(ancestors->next());
165 while(!ancestor.isNull())
167 const QXmlNodeModelIndex::Iterator::Ptr attributes(ancestor.iterate(QXmlNodeModelIndex::AxisAttribute));
168 QXmlNodeModelIndex attribute(attributes->next());
170 while(!attribute.isNull())
172 Q_ASSERT(attribute.kind() == QXmlNodeModelIndex::Attribute);
174 if(attribute.name() == xmlLang)
176 if(isLangMatch(attribute.stringValue(), lang))
177 return CommonValues::BooleanTrue;
179 return CommonValues::BooleanFalse;
182 attribute = attributes->next();
185 ancestor = ancestors->next();
188 return CommonValues::BooleanFalse;
191 Item RootFN::evaluateSingleton(const DynamicContext::Ptr &context) const
193 const Item arg(m_operands.first()->evaluateSingleton(context));
196 return arg.asNode().root();
201 SequenceType::Ptr RootFN::staticType() const
203 if(m_operands.isEmpty())
204 return makeGenericSequenceType(BuiltinTypes::node, Cardinality::exactlyOne());
206 return makeGenericSequenceType(BuiltinTypes::node, m_operands.first()->staticType()->cardinality().toWithoutMany());