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 "qbuiltintypes_p.h" |
41 | #include "qcommonsequencetypes_p.h" |
42 | #include "qitemmappingiterator_p.h" |
43 | |
44 | #include "qcopyof_p.h" |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | using namespace QPatternist; |
49 | |
50 | CopyOf::CopyOf(const Expression::Ptr &operand, |
51 | const bool inheritNSS, |
52 | const bool preserveNSS) : SingleContainer(operand) |
53 | , m_inheritNamespaces(inheritNSS) |
54 | , m_preserveNamespaces(preserveNSS) |
55 | , m_settings((m_inheritNamespaces ? QAbstractXmlNodeModel::InheritNamespaces : QAbstractXmlNodeModel::NodeCopySettings()) | |
56 | (m_preserveNamespaces ? QAbstractXmlNodeModel::PreserveNamespaces : QAbstractXmlNodeModel::NodeCopySettings())) |
57 | { |
58 | } |
59 | |
60 | Expression::Ptr CopyOf::compress(const StaticContext::Ptr &context) |
61 | { |
62 | /* We have zero effect if we have these settings. */ |
63 | if(m_inheritNamespaces && m_preserveNamespaces) |
64 | return m_operand->compress(context); |
65 | else |
66 | { |
67 | const ItemType::Ptr t(m_operand->staticType()->itemType()); |
68 | /* We have no effect on the empty sequence or atomic values. */ |
69 | if(BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(other: t) |
70 | || *t == *CommonSequenceTypes::Empty) |
71 | return m_operand->compress(context); |
72 | else |
73 | return SingleContainer::compress(context); |
74 | } |
75 | } |
76 | |
77 | void CopyOf::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const |
78 | { |
79 | /* Optimization: this completely breaks streaming. We get a call to |
80 | * evaluateToSequenceReceiver() but we require heap allocations by calling |
81 | * evaluateSequence(). */ |
82 | |
83 | const Item::Iterator::Ptr it(m_operand->evaluateSequence(context)); |
84 | QAbstractXmlReceiver *const receiver = context->outputReceiver(); |
85 | Item next(it->next()); |
86 | |
87 | while(next) |
88 | { |
89 | if(next.isNode()) |
90 | { |
91 | const QXmlNodeModelIndex &asNode = next.asNode(); |
92 | asNode.model()->copyNodeTo(node: asNode, receiver, m_settings); |
93 | } |
94 | else |
95 | receiver->item(item: next); |
96 | |
97 | next = it->next(); |
98 | } |
99 | } |
100 | |
101 | ExpressionVisitorResult::Ptr CopyOf::accept(const ExpressionVisitor::Ptr &visitor) const |
102 | { |
103 | return visitor->visit(this); |
104 | } |
105 | |
106 | SequenceType::Ptr CopyOf::staticType() const |
107 | { |
108 | return m_operand->staticType(); |
109 | } |
110 | |
111 | SequenceType::List CopyOf::expectedOperandTypes() const |
112 | { |
113 | SequenceType::List result; |
114 | result.append(t: CommonSequenceTypes::ZeroOrMoreItems); |
115 | return result; |
116 | } |
117 | |
118 | Expression::Properties CopyOf::properties() const |
119 | { |
120 | /* We have the content of node constructors as children, but even though |
121 | * createCopyOf() typically avoids creating us, we can still end up with an operand |
122 | * that allows compression. We must always avoid that, because we don't have |
123 | * implementation of evaluateSequence(), and so on. */ |
124 | return (m_operand->properties() & ~CreatesFocusForLast) | DisableElimination; |
125 | } |
126 | |
127 | ItemType::Ptr CopyOf::expectedContextItemType() const |
128 | { |
129 | return m_operand->expectedContextItemType(); |
130 | } |
131 | |
132 | QT_END_NAMESPACE |
133 | |