4e5848dc5a8f2960a35800b7d50eec806e7b0743
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / functions / qpatternmatchingfns.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QStringList>
43
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"
49
50 #include "qpatternmatchingfns_p.h"
51
52 QT_BEGIN_NAMESPACE
53
54 using namespace QPatternist;
55
56 MatchesFN::MatchesFN() : PatternPlatform(2)
57 {
58 }
59
60 Item MatchesFN::evaluateSingleton(const DynamicContext::Ptr &context) const
61 {
62     QRegExp regexp(pattern(context));
63     QString input;
64
65     const Item arg(m_operands.first()->evaluateSingleton(context));
66     if(arg)
67         input = arg.stringValue();
68
69     return Boolean::fromValue(input.contains(regexp));
70 }
71
72 ReplaceFN::ReplaceFN() : PatternPlatform(3)
73 {
74 }
75
76 Item ReplaceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
77 {
78     QRegExp regexp(pattern(context));
79     QString input;
80
81     const Item arg(m_operands.first()->evaluateSingleton(context));
82     if(arg)
83         input = arg.stringValue();
84
85     const QString replacement(m_replacementString.isNull() ? parseReplacement(regexp.captureCount(), context)
86                                                            : m_replacementString);
87
88
89     return AtomicString::fromValue(input.replace(regexp, replacement));
90 }
91
92 QString ReplaceFN::errorAtEnd(const char ch)
93 {
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('$')));
99 }
100
101 QString ReplaceFN::parseReplacement(const int,
102                                     const DynamicContext::Ptr &context) const
103 {
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());
106
107     QString retval;
108     retval.reserve(input.size());
109     const int len = input.length();
110
111     for(int i = 0; i < len; ++i)
112     {
113         const QChar ch(input.at(i));
114         switch(ch.toLatin1())
115         {
116             case '$':
117             {
118                 /* QRegExp uses '\' as opposed to '$' for marking sub groups. */
119                 retval.append(QLatin1Char('\\'));
120
121                 ++i;
122                 if(i == len)
123                 {
124                     context->error(errorAtEnd('$'), ReportContext::FORX0004, this);
125                     return QString();
126                 }
127
128                 const QChar nextCh(input.at(i));
129                 if(nextCh.isDigit())
130                     retval.append(nextCh);
131                 else
132                 {
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);
137                     return QString();
138                 }
139
140                 break;
141             }
142             case '\\':
143             {
144                 ++i;
145                 if(i == len)
146                 {
147                     /* error, we've reached the end. */;
148                     context->error(errorAtEnd('\\'), ReportContext::FORX0004, this);
149                 }
150
151                 const QChar nextCh(input.at(i));
152                 if(nextCh == QLatin1Char('\\') || nextCh == QLatin1Char('$'))
153                 {
154                     retval.append(ch);
155                     break;
156                 }
157                 else
158                 {
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);
165                     return QString();
166                 }
167             }
168             default:
169                 retval.append(ch);
170         }
171     }
172
173     return retval;
174 }
175
176 Expression::Ptr ReplaceFN::compress(const StaticContext::Ptr &context)
177 {
178     const Expression::Ptr me(PatternPlatform::compress(context));
179
180     if(me != this)
181         return me;
182
183     if(m_operands.at(2)->is(IDStringValue))
184     {
185         const int capt = captureCount();
186         if(capt == -1)
187             return me;
188         else
189             m_replacementString = parseReplacement(captureCount(), context->dynamicContext());
190     }
191
192     return me;
193 }
194
195 TokenizeFN::TokenizeFN() : PatternPlatform(2)
196 {
197 }
198
199 /**
200  * Used by QAbstractXmlForwardIterator.
201  */
202 static inline bool qIsForwardIteratorEnd(const QString &item)
203 {
204     return item.isNull();
205 }
206
207 Item TokenizeFN::mapToItem(const QString &subject, const DynamicContext::Ptr &) const
208 {
209     return AtomicString::fromValue(subject);
210 }
211
212 Item::Iterator::Ptr TokenizeFN::evaluateSequence(const DynamicContext::Ptr &context) const
213 {
214     const Item arg(m_operands.first()->evaluateSingleton(context));
215     if(!arg)
216         return CommonValues::emptyIterator;
217
218     const QString input(arg.stringValue());
219     if(input.isEmpty())
220         return CommonValues::emptyIterator;
221
222     QRegExp regExp(pattern(context));
223     const QStringList result(input.split(regExp, QString::KeepEmptyParts));
224
225     return makeItemMappingIterator<Item>(ConstPtr(this),
226                                          makeListIterator(result),
227                                          DynamicContext::Ptr());
228 }
229
230 QT_END_NAMESPACE