Qt 4.8
qqnamefns.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 "qanyuri_p.h"
43 #include "qbuiltintypes_p.h"
44 #include "qcommonvalues_p.h"
45 #include "qpatternistlocale_p.h"
47 #include "qqnameconstructor_p.h"
48 #include "qqnamevalue_p.h"
49 #include "qatomicstring_p.h"
50 #include "qxpathhelper_p.h"
51 
52 #include "qqnamefns_p.h"
53 
55 
56 using namespace QPatternist;
57 
59 {
60  const Item paramURI(m_operands.first()->evaluateSingleton(context));
61  const QString paramQName(m_operands.last()->evaluateSingleton(context).stringValue());
62 
63  QString ns;
64  if(paramURI)
65  ns = paramURI.stringValue();
66 
67  if(!XPathHelper::isQName(paramQName))
68  {
69  context->error(QtXmlPatterns::tr("%1 is an invalid %2").arg(formatData(paramQName),
72  return Item();
73  }
74 
75  QString prefix;
76  QString lname;
77  XPathHelper::splitQName(paramQName, prefix, lname);
78  const QXmlName n(context->namePool()->allocateQName(ns, lname, prefix));
79 
80  if(ns.isEmpty())
81  {
82  if(prefix.isEmpty())
83  return toItem(QNameValue::fromValue(context->namePool(), n));
84  else
85  {
86  context->error(QtXmlPatterns::tr(
87  "If the first argument is the empty sequence or "
88  "a zero-length string (no namespace), a prefix "
89  "cannot be specified. Prefix %1 was specified.")
90  .arg(formatKeyword(prefix)),
92  return Item(); /* Silence compiler warning. */
93  }
94  }
95  else
96  return toItem(QNameValue::fromValue(context->namePool(), n));
97 }
98 
100 {
101  const Item itemName(m_operands.first()->evaluateSingleton(context));
102 
103  if(!itemName)
104  return Item();
105 
107  const QString strName(itemName.stringValue());
110  ReportContext::FONS0004>(strName,
111  context,
112  resolver,
113  this);
114 
115  return toItem(QNameValue::fromValue(context->namePool(), name));
116 }
117 
119 {
121  if(!arg)
122  return Item();
123 
124  const QString prefix(context->namePool()->stringForPrefix(arg->qName().prefix()));
125 
126  if(prefix.isEmpty())
127  return Item();
128  else
129  return AtomicString::fromValue(context->namePool()->stringForPrefix(arg->qName().prefix()));
130 }
131 
133 {
135  return arg ? toItem(AtomicString::fromValue(context->namePool()->stringForLocalName(arg->qName().localName()))) : Item();
136 }
137 
139 {
141  return arg ? toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(arg->qName().namespaceURI()))) : Item();
142 }
143 
145 {
146  const Item prefixItem(m_operands.first()->evaluateSingleton(context));
147  QXmlName::PrefixCode prefix;
148 
149  if(prefixItem)
150  prefix = context->namePool()->allocatePrefix(prefixItem.stringValue());
151  else
152  prefix = StandardPrefixes::empty;
153 
154  const Item eleItem(m_operands.last()->evaluateSingleton(context));
155  Q_ASSERT(eleItem);
156 
157  const QXmlName::NamespaceCode ns = eleItem.asNode().namespaceForPrefix(prefix);
158 
160  {
161  /* This is a bit tricky. The default namespace is not considered an in-scope binding
162  * on a node, but the specification for this function do consider it a binding and therefore
163  * the empty string. */
164  if(prefix == StandardPrefixes::empty)
166  else
167  return Item();
168  }
169  else
170  return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(ns)));
171 }
172 
174 {
175  const Item e(m_operands.first()->evaluateSingleton(context));
176 
177  const QVector<QXmlName> nbs(e.asNode().namespaceBindings());
178  const int len = nbs.size();
179  const NamePool::Ptr np(context->namePool());
180 
181  QList<Item> result;
182 
183  for(int i = 0; i < len; ++i)
184  result.append(AtomicString::fromValue(np->stringForPrefix(nbs.at(i).prefix())));
185 
186  return makeListIterator(result);
187 }
188 
const QString & stringForLocalName(const QXmlName::LocalNameCode code) const
Definition: qnamepool_p.h:168
virtual NamePool::Ptr namePool() const =0
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:132
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:144
QString formatKeyword(const QString &keyword)
qint16 NamespaceCode
Definition: qxmlname.h:82
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
Item toItem(const QExplicitlySharedDataPointer< T > atomicValue)
Definition: qitem_p.h:431
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
static QXmlName expandQName(const QString &lexicalQName, const TReportContext &context, const NamespaceResolver::Ptr &nsResolver, const SourceLocationReflection *const r, const bool asForAttribute=false)
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)
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:138
QXmlName::PrefixCode allocatePrefix(const QString &prefix)
Definition: qnamepool_p.h:214
const QString & stringForPrefix(const QXmlName::PrefixCode code) const
Definition: qnamepool_p.h:174
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
static QNameValue::Ptr fromValue(const NamePool::Ptr &np, const QXmlName name)
Definition: qqnamevalue.cpp:59
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
The namespace for the internal API of QtXmlPatterns.
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:173
static QString formatData(const QString &data)
A NamespaceResolver that use a QXmlNodeModelIndex&#39;s in-scope namespace bindings for resolving namespa...
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:99
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:58
The QXmlName class represents the name of an XML node, in an efficient, namespace-aware way...
Definition: qxmlname.h:58
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static const AtomicType::Ptr xsQName
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qqnamefns.cpp:118
Implements the value instance of the xs:QName type.
Definition: qqnamevalue_p.h:73
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
const QString & stringForNamespace(const QXmlName::NamespaceCode code) const
Definition: qnamepool_p.h:180
static const AtomicValue::Ptr EmptyString
QXmlName allocateQName(const QString &uri, const QString &localName, const QString &prefix=QString())
Definition: qnamepool.cpp:251
static void splitQName(const QString &qName, QString &prefix, QString &localName)
Splits qName into localName and prefix.
QExplicitlySharedDataPointer< DynamicContext > Ptr
TCastTarget * as() const
Definition: qitem_p.h:278
static bool isQName(const QString &qName)
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
static AnyURI::Ptr fromValue(const QString &value)
Definition: qanyuri.cpp:56
NamespaceCode PrefixCode
Definition: qxmlname.h:83