1 /****************************************************************************
3 ** Copyright (C) 2012 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 "qanyuri_p.h"
43 #include "qbuiltintypes_p.h"
44 #include "qcommonvalues_p.h"
45 #include "qpatternistlocale_p.h"
46 #include "qnodenamespaceresolver_p.h"
47 #include "qqnameconstructor_p.h"
48 #include "qqnamevalue_p.h"
49 #include "qatomicstring_p.h"
50 #include "qxpathhelper_p.h"
52 #include "qqnamefns_p.h"
56 using namespace QPatternist;
58 Item QNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
60 const Item paramURI(m_operands.first()->evaluateSingleton(context));
61 const QString paramQName(m_operands.last()->evaluateSingleton(context).stringValue());
65 ns = paramURI.stringValue();
67 if(!XPathHelper::isQName(paramQName))
69 context->error(QtXmlPatterns::tr("%1 is an invalid %2").arg(formatData(paramQName),
70 formatType(context->namePool(), BuiltinTypes::xsQName)),
71 ReportContext::FOCA0002, this);
77 XPathHelper::splitQName(paramQName, prefix, lname);
78 const QXmlName n(context->namePool()->allocateQName(ns, lname, prefix));
83 return toItem(QNameValue::fromValue(context->namePool(), n));
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)),
91 ReportContext::FOCA0002, this);
92 return Item(); /* Silence compiler warning. */
96 return toItem(QNameValue::fromValue(context->namePool(), n));
99 Item ResolveQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
101 const Item itemName(m_operands.first()->evaluateSingleton(context));
106 const NamespaceResolver::Ptr resolver(new NodeNamespaceResolver(m_operands.last()->evaluateSingleton(context)));
107 const QString strName(itemName.stringValue());
108 const QXmlName name = QNameConstructor::expandQName<DynamicContext::Ptr,
109 ReportContext::FOCA0002,
110 ReportContext::FONS0004>(strName,
115 return toItem(QNameValue::fromValue(context->namePool(), name));
118 Item PrefixFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
120 const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>());
124 const QString prefix(context->namePool()->stringForPrefix(arg->qName().prefix()));
129 return AtomicString::fromValue(context->namePool()->stringForPrefix(arg->qName().prefix()));
132 Item LocalNameFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
134 const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>());
135 return arg ? toItem(AtomicString::fromValue(context->namePool()->stringForLocalName(arg->qName().localName()))) : Item();
138 Item NamespaceURIFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
140 const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>());
141 return arg ? toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(arg->qName().namespaceURI()))) : Item();
144 Item NamespaceURIForPrefixFN::evaluateSingleton(const DynamicContext::Ptr &context) const
146 const Item prefixItem(m_operands.first()->evaluateSingleton(context));
147 QXmlName::PrefixCode prefix;
150 prefix = context->namePool()->allocatePrefix(prefixItem.stringValue());
152 prefix = StandardPrefixes::empty;
154 const Item eleItem(m_operands.last()->evaluateSingleton(context));
157 const QXmlName::NamespaceCode ns = eleItem.asNode().namespaceForPrefix(prefix);
159 if(ns == NamespaceResolver::NoBinding)
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)
165 return CommonValues::EmptyString;
170 return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(ns)));
173 Item::Iterator::Ptr InScopePrefixesFN::evaluateSequence(const DynamicContext::Ptr &context) const
175 const Item e(m_operands.first()->evaluateSingleton(context));
177 const QVector<QXmlName> nbs(e.asNode().namespaceBindings());
178 const int len = nbs.size();
179 const NamePool::Ptr np(context->namePool());
183 for(int i = 0; i < len; ++i)
184 result.append(AtomicString::fromValue(np->stringForPrefix(nbs.at(i).prefix())));
186 return makeListIterator(result);