Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / functions / qnodefns.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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"
50
51 #include "qnodefns_p.h"
52
53 QT_BEGIN_NAMESPACE
54
55 using namespace QPatternist;
56
57 Item NameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
58 {
59     const Item node(m_operands.first()->evaluateSingleton(context));
60
61     if(node)
62     {
63         const QXmlName name(node.asNode().name());
64
65         if(name.isNull())
66             return CommonValues::EmptyString;
67         else
68             return AtomicString::fromValue(context->namePool()->toLexical(name));
69     }
70     else
71         return CommonValues::EmptyString;
72 }
73
74 Item LocalNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
75 {
76     const Item node(m_operands.first()->evaluateSingleton(context));
77
78     if(node)
79     {
80         const QXmlName name(node.asNode().name());
81
82         if(name.isNull())
83             return CommonValues::EmptyString;
84         else
85             return AtomicString::fromValue(context->namePool()->stringForLocalName(name.localName()));
86     }
87     else
88         return CommonValues::EmptyString;
89 }
90
91 Item NamespaceURIFN::evaluateSingleton(const DynamicContext::Ptr &context) const
92 {
93     const Item node(m_operands.first()->evaluateSingleton(context));
94
95     if(node)
96     {
97         const QXmlName name(node.asNode().name());
98
99         if(name.isNull())
100             return CommonValues::EmptyAnyURI;
101         else
102             return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(name.namespaceURI())));
103     }
104     else
105         return CommonValues::EmptyAnyURI;
106 }
107
108 Item NumberFN::evaluateSingleton(const DynamicContext::Ptr &context) const
109 {
110     const Item item(m_operands.first()->evaluateSingleton(context));
111
112     if(!item)
113         return CommonValues::DoubleNaN;
114
115     const Item val(cast(item, context));
116     Q_ASSERT(val);
117
118     if(val.as<AtomicValue>()->hasError())
119         return CommonValues::DoubleNaN;
120     else
121         return val;
122 }
123
124 Expression::Ptr NumberFN::typeCheck(const StaticContext::Ptr &context,
125                                     const SequenceType::Ptr &reqType)
126 {
127     const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
128     const ItemType::Ptr sourceType(m_operands.first()->staticType()->itemType());
129
130     if(BuiltinTypes::xsDouble->xdtTypeMatches(sourceType))
131     {
132         /* The operand is already xs:double, no need for fn:number(). */
133         return m_operands.first()->typeCheck(context, reqType);
134     }
135     else if(prepareCasting(context, sourceType))
136         return me;
137     else
138     {
139         /* Casting to xs:double will never succeed and we would always return NaN.*/
140         return wrapLiteral(CommonValues::DoubleNaN, context, this)->typeCheck(context, reqType);
141     }
142 }
143
144 bool LangFN::isLangMatch(const QString &candidate, const QString &toMatch)
145 {
146     if(QString::compare(candidate, toMatch, Qt::CaseInsensitive) == 0)
147         return true;
148
149     return candidate.startsWith(toMatch, Qt::CaseInsensitive)
150            && candidate.length() > toMatch.length()
151            && candidate.at(toMatch.length()) == QLatin1Char('-');
152 }
153
154 Item LangFN::evaluateSingleton(const DynamicContext::Ptr &context) const
155 {
156     const Item langArg(m_operands.first()->evaluateSingleton(context));
157     const QString lang(langArg ? langArg.stringValue() : QString());
158
159     const QXmlName xmlLang(StandardNamespaces::xml, StandardLocalNames::lang, StandardPrefixes::xml);
160     const QXmlNodeModelIndex langNode(m_operands.at(1)->evaluateSingleton(context).asNode());
161
162     const QXmlNodeModelIndex::Iterator::Ptr ancestors(langNode.iterate(QXmlNodeModelIndex::AxisAncestorOrSelf));
163     QXmlNodeModelIndex ancestor(ancestors->next());
164
165     while(!ancestor.isNull())
166     {
167         const QXmlNodeModelIndex::Iterator::Ptr attributes(ancestor.iterate(QXmlNodeModelIndex::AxisAttribute));
168         QXmlNodeModelIndex attribute(attributes->next());
169
170         while(!attribute.isNull())
171         {
172             Q_ASSERT(attribute.kind() == QXmlNodeModelIndex::Attribute);
173
174             if(attribute.name() == xmlLang)
175             {
176                 if(isLangMatch(attribute.stringValue(), lang))
177                     return CommonValues::BooleanTrue;
178                 else
179                     return CommonValues::BooleanFalse;
180             }
181
182             attribute = attributes->next();
183         }
184
185         ancestor = ancestors->next();
186     }
187
188     return CommonValues::BooleanFalse;
189 }
190
191 Item RootFN::evaluateSingleton(const DynamicContext::Ptr &context) const
192 {
193     const Item arg(m_operands.first()->evaluateSingleton(context));
194
195     if(arg)
196         return arg.asNode().root();
197     else
198         return Item();
199 }
200
201 SequenceType::Ptr RootFN::staticType() const
202 {
203     if(m_operands.isEmpty())
204         return makeGenericSequenceType(BuiltinTypes::node, Cardinality::exactlyOne());
205     else
206         return makeGenericSequenceType(BuiltinTypes::node, m_operands.first()->staticType()->cardinality().toWithoutMany());
207 }
208
209 QT_END_NAMESPACE