|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
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 const 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 const 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.numCaptures(), 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.toAscii()) |
|
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 const 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 |