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 <QStringList>
41
42#include "qboolean_p.h"
43#include "qcommonvalues_p.h"
44#include "qitemmappingiterator_p.h"
45#include "qpatternistlocale_p.h"
46#include "qatomicstring_p.h"
47
48#include "qpatternmatchingfns_p.h"
49
50QT_BEGIN_NAMESPACE
51
52using namespace QPatternist;
53
54MatchesFN::MatchesFN() : PatternPlatform(2)
55{
56}
57
58Item MatchesFN::evaluateSingleton(const DynamicContext::Ptr &context) const
59{
60 QRegExp regexp(pattern(context));
61 QString input;
62
63 const Item arg(m_operands.first()->evaluateSingleton(context));
64 if(arg)
65 input = arg.stringValue();
66
67 return Boolean::fromValue(value: input.contains(rx&: regexp));
68}
69
70ReplaceFN::ReplaceFN() : PatternPlatform(3)
71{
72}
73
74Item ReplaceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
75{
76 QRegExp regexp(pattern(context));
77 QString input;
78
79 const Item arg(m_operands.first()->evaluateSingleton(context));
80 if(arg)
81 input = arg.stringValue();
82
83 const QString replacement(m_replacementString.isNull() ? parseReplacement(captureCount: regexp.captureCount(), context)
84 : m_replacementString);
85
86
87 return AtomicString::fromValue(value: input.replace(rx: regexp, after: replacement));
88}
89
90QString ReplaceFN::errorAtEnd(const char ch)
91{
92 return QtXmlPatterns::tr(sourceText: "%1 must be followed by %2 or %3, not at "
93 "the end of the replacement string.")
94 .arg(a: formatKeyword(keyword: QLatin1Char(ch)))
95 .arg(a: formatKeyword(keyword: QLatin1Char('\\')))
96 .arg(a: formatKeyword(keyword: QLatin1Char('$')));
97}
98
99QString ReplaceFN::parseReplacement(const int,
100 const DynamicContext::Ptr &context) const
101{
102 // TODO what if there is no groups, can one rewrite to the replacement then?
103 const QString input(m_operands.at(i: 2)->evaluateSingleton(context).stringValue());
104
105 QString retval;
106 retval.reserve(asize: input.size());
107 const int len = input.length();
108
109 for(int i = 0; i < len; ++i)
110 {
111 const QChar ch(input.at(i));
112 switch(ch.toLatin1())
113 {
114 case '$':
115 {
116 /* QRegExp uses '\' as opposed to '$' for marking sub groups. */
117 retval.append(c: QLatin1Char('\\'));
118
119 ++i;
120 if(i == len)
121 {
122 context->error(message: errorAtEnd(ch: '$'), errorCode: ReportContext::FORX0004, reflection: this);
123 return QString();
124 }
125
126 const QChar nextCh(input.at(i));
127 if(nextCh.isDigit())
128 retval.append(c: nextCh);
129 else
130 {
131 context->error(message: QtXmlPatterns::tr(sourceText: "In the replacement string, %1 must be "
132 "followed by at least one digit when not escaped.")
133 .arg(a: formatKeyword(keyword: QLatin1Char('$'))),
134 errorCode: ReportContext::FORX0004, reflection: this);
135 return QString();
136 }
137
138 break;
139 }
140 case '\\':
141 {
142 ++i;
143 if(i == len)
144 {
145 /* error, we've reached the end. */;
146 context->error(message: errorAtEnd(ch: '\\'), errorCode: ReportContext::FORX0004, reflection: this);
147 }
148
149 const QChar nextCh(input.at(i));
150 if(nextCh == QLatin1Char('\\') || nextCh == QLatin1Char('$'))
151 {
152 retval.append(c: ch);
153 break;
154 }
155 else
156 {
157 context->error(message: QtXmlPatterns::tr(sourceText: "In the replacement string, %1 can only be used to "
158 "escape itself or %2, not %3")
159 .arg(a: formatKeyword(keyword: QLatin1Char('\\')))
160 .arg(a: formatKeyword(keyword: QLatin1Char('$')))
161 .arg(a: formatKeyword(keyword: nextCh)),
162 errorCode: ReportContext::FORX0004, reflection: this);
163 return QString();
164 }
165 }
166 default:
167 retval.append(c: ch);
168 }
169 }
170
171 return retval;
172}
173
174Expression::Ptr ReplaceFN::compress(const StaticContext::Ptr &context)
175{
176 const Expression::Ptr me(PatternPlatform::compress(context));
177
178 if(me != this)
179 return me;
180
181 if(m_operands.at(i: 2)->is(i: IDStringValue))
182 {
183 const int capt = captureCount();
184 if(capt == -1)
185 return me;
186 else
187 m_replacementString = parseReplacement(captureCount(), context: context->dynamicContext());
188 }
189
190 return me;
191}
192
193TokenizeFN::TokenizeFN() : PatternPlatform(2)
194{
195}
196
197/**
198 * Used by QAbstractXmlForwardIterator.
199 */
200static inline bool qIsForwardIteratorEnd(const QString &item)
201{
202 return item.isNull();
203}
204
205Item TokenizeFN::mapToItem(const QString &subject, const DynamicContext::Ptr &) const
206{
207 return AtomicString::fromValue(value: subject);
208}
209
210Item::Iterator::Ptr TokenizeFN::evaluateSequence(const DynamicContext::Ptr &context) const
211{
212 const Item arg(m_operands.first()->evaluateSingleton(context));
213 if(!arg)
214 return CommonValues::emptyIterator;
215
216 const QString input(arg.stringValue());
217 if(input.isEmpty())
218 return CommonValues::emptyIterator;
219
220 QRegExp regExp(pattern(context));
221 const QStringList result(input.split(sep: regExp, behavior: Qt::KeepEmptyParts));
222
223 return makeItemMappingIterator<Item>(mapper: ConstPtr(this),
224 source: makeListIterator(list: result),
225 context: DynamicContext::Ptr());
226}
227
228QT_END_NAMESPACE
229

source code of qtxmlpatterns/src/xmlpatterns/functions/qpatternmatchingfns.cpp