Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / functions / qqnamefns.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
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"
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"
51
52 #include "qqnamefns_p.h"
53
54 QT_BEGIN_NAMESPACE
55
56 using namespace QPatternist;
57
58 Item QNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
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),
70                                                      formatType(context->namePool(), BuiltinTypes::xsQName)),
71                        ReportContext::FOCA0002, this);
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)),
91                            ReportContext::FOCA0002, this);
92             return Item(); /* Silence compiler warning. */
93         }
94     }
95     else
96         return toItem(QNameValue::fromValue(context->namePool(), n));
97 }
98
99 Item ResolveQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
100 {
101     const Item itemName(m_operands.first()->evaluateSingleton(context));
102
103     if(!itemName)
104         return Item();
105
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,
111                                                                               context,
112                                                                               resolver,
113                                                                               this);
114
115     return toItem(QNameValue::fromValue(context->namePool(), name));
116 }
117
118 Item PrefixFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
119 {
120     const QNameValue::Ptr arg(m_operands.first()->evaluateSingleton(context).as<QNameValue>());
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
132 Item LocalNameFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
133 {
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();
136 }
137
138 Item NamespaceURIFromQNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
139 {
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();
142 }
143
144 Item NamespaceURIForPrefixFN::evaluateSingleton(const DynamicContext::Ptr &context) const
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
159     if(ns == NamespaceResolver::NoBinding)
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)
165             return CommonValues::EmptyString;
166         else
167             return Item();
168     }
169     else
170         return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(ns)));
171 }
172
173 Item::Iterator::Ptr InScopePrefixesFN::evaluateSequence(const DynamicContext::Ptr &context) const
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
189 QT_END_NAMESPACE