1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 <QStringList>
44 #include "qboolean_p.h"
45 #include "qcommonvalues_p.h"
46 #include "qitemmappingiterator_p.h"
47 #include "qpatternistlocale_p.h"
48 #include "qatomicstring_p.h"
50 #include "qpatternmatchingfns_p.h"
54 using namespace QPatternist;
56 MatchesFN::MatchesFN() : PatternPlatform(2)
60 Item MatchesFN::evaluateSingleton(const DynamicContext::Ptr &context) const
62 const QRegExp regexp(pattern(context));
65 const Item arg(m_operands.first()->evaluateSingleton(context));
67 input = arg.stringValue();
69 return Boolean::fromValue(input.contains(regexp));
72 ReplaceFN::ReplaceFN() : PatternPlatform(3)
76 Item ReplaceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
78 const QRegExp regexp(pattern(context));
81 const Item arg(m_operands.first()->evaluateSingleton(context));
83 input = arg.stringValue();
85 const QString replacement(m_replacementString.isNull() ? parseReplacement(regexp.captureCount(), context)
86 : m_replacementString);
89 return AtomicString::fromValue(input.replace(regexp, replacement));
92 QString ReplaceFN::errorAtEnd(const char ch)
94 return QtXmlPatterns::tr("%1 must be followed by %2 or %3, not at "
95 "the end of the replacement string.")
96 .arg(formatKeyword(QLatin1Char(ch)))
97 .arg(formatKeyword(QLatin1Char('\\')))
98 .arg(formatKeyword(QLatin1Char('$')));
101 QString ReplaceFN::parseReplacement(const int,
102 const DynamicContext::Ptr &context) const
104 // TODO what if there is no groups, can one rewrite to the replacement then?
105 const QString input(m_operands.at(2)->evaluateSingleton(context).stringValue());
108 retval.reserve(input.size());
109 const int len = input.length();
111 for(int i = 0; i < len; ++i)
113 const QChar ch(input.at(i));
118 /* QRegExp uses '\' as opposed to '$' for marking sub groups. */
119 retval.append(QLatin1Char('\\'));
124 context->error(errorAtEnd('$'), ReportContext::FORX0004, this);
128 const QChar nextCh(input.at(i));
130 retval.append(nextCh);
133 context->error(QtXmlPatterns::tr("In the replacement string, %1 must be "
134 "followed by at least one digit when not escaped.")
135 .arg(formatKeyword(QLatin1Char('$'))),
136 ReportContext::FORX0004, this);
147 /* error, we've reached the end. */;
148 context->error(errorAtEnd('\\'), ReportContext::FORX0004, this);
151 const QChar nextCh(input.at(i));
152 if(nextCh == QLatin1Char('\\') || nextCh == QLatin1Char('$'))
159 context->error(QtXmlPatterns::tr("In the replacement string, %1 can only be used to "
160 "escape itself or %2, not %3")
161 .arg(formatKeyword(QLatin1Char('\\')))
162 .arg(formatKeyword(QLatin1Char('$')))
163 .arg(formatKeyword(nextCh)),
164 ReportContext::FORX0004, this);
176 Expression::Ptr ReplaceFN::compress(const StaticContext::Ptr &context)
178 const Expression::Ptr me(PatternPlatform::compress(context));
183 if(m_operands.at(2)->is(IDStringValue))
185 const int capt = captureCount();
189 m_replacementString = parseReplacement(captureCount(), context->dynamicContext());
195 TokenizeFN::TokenizeFN() : PatternPlatform(2)
200 * Used by QAbstractXmlForwardIterator.
202 static inline bool qIsForwardIteratorEnd(const QString &item)
204 return item.isNull();
207 Item TokenizeFN::mapToItem(const QString &subject, const DynamicContext::Ptr &) const
209 return AtomicString::fromValue(subject);
212 Item::Iterator::Ptr TokenizeFN::evaluateSequence(const DynamicContext::Ptr &context) const
214 const Item arg(m_operands.first()->evaluateSingleton(context));
216 return CommonValues::emptyIterator;
218 const QString input(arg.stringValue());
220 return CommonValues::emptyIterator;
222 const QRegExp regExp(pattern(context));
223 const QStringList result(input.split(regExp, QString::KeepEmptyParts));
225 return makeItemMappingIterator<Item>(ConstPtr(this),
226 makeListIterator(result),
227 DynamicContext::Ptr());