Qt 4.8
qnodefns.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
54 
55 using namespace QPatternist;
56 
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())
67  else
68  return AtomicString::fromValue(context->namePool()->toLexical(name));
69  }
70  else
72 }
73 
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())
84  else
85  return AtomicString::fromValue(context->namePool()->stringForLocalName(name.localName()));
86  }
87  else
89 }
90 
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())
101  else
102  return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(name.namespaceURI())));
103  }
104  else
106 }
107 
109 {
110  const Item item(m_operands.first()->evaluateSingleton(context));
111 
112  if(!item)
114 
115  const Item val(cast(item, context));
116  Q_ASSERT(val);
117 
118  if(val.as<AtomicValue>()->hasError())
120  else
121  return val;
122 }
123 
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 
155 {
156  const Item langArg(m_operands.first()->evaluateSingleton(context));
157  const QString lang(langArg ? langArg.stringValue() : QString());
158 
160  const QXmlNodeModelIndex langNode(m_operands.at(1)->evaluateSingleton(context).asNode());
161 
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))
178  else
180  }
181 
182  attribute = attributes->next();
183  }
184 
185  ancestor = ancestors->next();
186  }
187 
189 }
190 
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 
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 
const QString & stringForLocalName(const QXmlName::LocalNameCode code) const
Definition: qnamepool_p.h:168
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
virtual NamePool::Ptr namePool() const =0
The QXmlNodeModelIndex class identifies a node in an XML node model subclassed from QAbstractXmlNodeM...
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:191
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
static const AtomicValue::Ptr BooleanTrue
virtual bool hasError() const
virtual SequenceType::Ptr staticType() const
Definition: qnodefns.cpp:201
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static const AtomicValue::Ptr EmptyAnyURI
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
Item toItem(const QExplicitlySharedDataPointer< T > atomicValue)
Definition: qitem_p.h:431
static const AtomicValue::Ptr DoubleNaN
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qnodefns.cpp:124
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static AtomicString::Ptr fromValue(const QString &value)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:74
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:154
Base class for all classes representing atomic values.
Definition: qitem_p.h:118
const char * name
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:57
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:108
The namespace for the internal API of QtXmlPatterns.
static const AtomicType::Ptr xsDouble
virtual Cardinality cardinality() const =0
static const AnyNodeType::Ptr node
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qnodefns.cpp:91
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
virtual SequenceType::Ptr staticType() const =0
The QXmlName class represents the name of an XML node, in an efficient, namespace-aware way...
Definition: qxmlname.h:58
int compare(const QString &s) const
Definition: qstring.cpp:5037
const QXmlNodeModelIndex & asNode() const
Definition: qitem_p.h:379
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
QXmlNodeModelIndex root() const
Definition: qitem_p.h:451
Cardinality toWithoutMany() const
const QString & stringForNamespace(const QXmlName::NamespaceCode code) const
Definition: qnamepool_p.h:180
static const AtomicValue::Ptr EmptyString
static Cardinality exactlyOne()
virtual ItemType::Ptr itemType() const =0
TCastTarget * as() const
Definition: qitem_p.h:278
static const AtomicValue::Ptr BooleanFalse
QString toLexical(const QXmlName qName) const
Definition: qnamepool_p.h:188
static bool isLangMatch(const QString &candidate, const QString &toMatch)
Definition: qnodefns.cpp:144
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
static AnyURI::Ptr fromValue(const QString &value)
Definition: qanyuri.cpp:56