1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qanyuri_p.h"
41#include "qbuiltintypes_p.h"
42#include "qcommonsequencetypes_p.h"
43#include "qpatternistlocale_p.h"
44#include "private/qxmlutils_p.h"
45
46#include "qcomputednamespaceconstructor_p.h"
47
48QT_BEGIN_NAMESPACE
49
50using namespace QPatternist;
51
52ComputedNamespaceConstructor::ComputedNamespaceConstructor(const Expression::Ptr &prefix,
53 const Expression::Ptr &namespaceURI) : PairContainer(prefix, namespaceURI)
54{
55}
56
57void ComputedNamespaceConstructor::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
58{
59 const Item prefixItem(m_operand1->evaluateSingleton(context));
60 const QString prefix(prefixItem ? prefixItem.stringValue() : QString());
61
62 const Item namespaceItem(m_operand2->evaluateSingleton(context));
63 const QString namespaceURI(namespaceItem ? namespaceItem.stringValue() : QString());
64
65 if(namespaceURI.isEmpty())
66 {
67 context->error(message: QtXmlPatterns::tr(sourceText: "In a namespace constructor, the value for a namespace cannot be an empty string."),
68 errorCode: ReportContext::XTDE0930,
69 reflection: this);
70 }
71
72 /* One optimization could be to store a pointer to
73 * the name pool as a member in order to avoid the virtual call(s). */
74 const NamePool::Ptr np(context->namePool());
75
76 if(!prefix.isEmpty() && !QXmlUtils::isNCName(ncName: prefix))
77 {
78 context->error(message: QtXmlPatterns::tr(sourceText: "The prefix must be a valid %1, which %2 is not.")
79 .arg(args: formatType(np, type: BuiltinTypes::xsNCName),
80 args: formatKeyword(keyword: prefix)),
81 errorCode: ReportContext::XTDE0920,
82 reflection: this);
83 }
84 const QXmlName binding(np->allocateBinding(prefix, uri: namespaceURI));
85
86 AnyURI::toQUrl<ReportContext::XTDE0905, DynamicContext::Ptr>(value: namespaceURI,
87 context,
88 r: this);
89
90 if(binding.prefix() == StandardPrefixes::xmlns)
91 {
92 context->error(message: QtXmlPatterns::tr(sourceText: "The prefix %1 cannot be bound.")
93 .arg(a: formatKeyword(keyword: prefix)),
94 errorCode: ReportContext::XTDE0920,
95 reflection: this);
96 }
97
98 if((binding.prefix() == StandardPrefixes::xml && binding.namespaceURI() != StandardNamespaces::xml)
99 ||
100 (binding.prefix() != StandardPrefixes::xml && binding.namespaceURI() == StandardNamespaces::xml))
101 {
102 context->error(message: QtXmlPatterns::tr(sourceText: "Only the prefix %1 can be bound to %2 and vice versa.")
103 .arg(args: formatKeyword(keyword: prefix), args: formatKeyword(keyword: namespaceURI)),
104 errorCode: ReportContext::XTDE0925,
105 reflection: this);
106 }
107
108 context->outputReceiver()->namespaceBinding(name: binding);
109}
110
111SequenceType::Ptr ComputedNamespaceConstructor::staticType() const
112{
113 return CommonSequenceTypes::ExactlyOneAttribute;
114}
115
116SequenceType::List ComputedNamespaceConstructor::expectedOperandTypes() const
117{
118 SequenceType::List result;
119 result.append(t: CommonSequenceTypes::ZeroOrOneString);
120 result.append(t: CommonSequenceTypes::ZeroOrOneString);
121 return result;
122}
123
124Expression::Properties ComputedNamespaceConstructor::properties() const
125{
126 return DisableElimination | IsNodeConstructor;
127}
128
129ExpressionVisitorResult::Ptr ComputedNamespaceConstructor::accept(const ExpressionVisitor::Ptr &visitor) const
130{
131 return visitor->visit(this);
132}
133
134QT_END_NAMESPACE
135

source code of qtxmlpatterns/src/xmlpatterns/expr/qcomputednamespaceconstructor.cpp