1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 "qabstractfloat_p.h"
43 #include "qatomicstring_p.h"
44 #include "qcommonsequencetypes_p.h"
45 #include "qcommonvalues_p.h"
46 #include "qinteger_p.h"
47 #include "qliteral_p.h"
48 #include "qpatternistlocale_p.h"
49 #include "qschemanumeric_p.h"
51 #include "qstringvaluefns_p.h"
55 using namespace QPatternist;
57 Item ConcatFN::evaluateSingleton(const DynamicContext::Ptr &context) const
59 const Expression::List::const_iterator end(m_operands.constEnd());
60 Expression::List::const_iterator it(m_operands.constBegin());
63 for(; it != end; ++it)
65 Item item((*it)->evaluateSingleton(context));
68 result += item.stringValue();
71 return AtomicString::fromValue(result);
74 Item StringJoinFN::evaluateSingleton(const DynamicContext::Ptr &context) const
76 Item::Iterator::Ptr it(m_operands.first()->evaluateSequence(context));
78 Item current(it->next());
80 if(!current) /* Exit early, don't evaluate the separator. */
81 return CommonValues::EmptyString;
85 const Item isep(m_operands.at(1)->evaluateSingleton(context));
88 separator = isep.stringValue();
92 result += current.stringValue();
101 return result.isEmpty()
102 ? toItem(CommonValues::EmptyString)
103 : toItem(AtomicString::fromValue(result));
106 Expression::Ptr StringJoinFN::compress(const StaticContext::Ptr &context)
108 if(m_operands.first()->staticType()->cardinality().allowsMany())
109 return FunctionCall::compress(context);
112 if(m_operands.first()->is(IDEmptySequence))
113 return wrapLiteral(CommonValues::EmptyString, context, this);
115 return m_operands.first()->compress(context);
119 Item SubstringFN::evaluateSingleton(const DynamicContext::Ptr &context) const
121 Item item(m_operands.first()->evaluateSingleton(context));
124 return CommonValues::EmptyString;
126 const QString str(item.stringValue());
128 const xsDouble dblStart = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()
129 ->round()->toDouble();
131 return CommonValues::EmptyString;
133 /* XPath starts from 1, but C++ starts from 0. */
134 xsInteger startingLoc = Double::fromValue(dblStart)->round()->toInteger() - 1;
136 xsInteger length = 0;
137 if(m_operands.count() == 2)
138 length = str.length() - startingLoc;
141 const xsDouble dblLen = m_operands.at(2)->evaluateSingleton(context).as<Numeric>()
142 ->round()->toDouble();
145 return CommonValues::EmptyString;
147 length = Double::fromValue(dblLen)->round()->toInteger();
148 if(startingLoc > startingLoc + length)
149 return CommonValues::EmptyString;
154 length = length + startingLoc;
158 return AtomicString::fromValue(str.mid(startingLoc, length));
161 Item StringLengthFN::evaluateSingleton(const DynamicContext::Ptr &context) const
163 const Item item(m_operands.first()->evaluateSingleton(context));
165 /* fn:string() is re-implemented "inline" here. */
167 return Integer::fromValue(item.stringValue().length());
169 return CommonValues::IntegerZero;
172 NormalizeUnicodeFN::NormalizeUnicodeFN() : m_normForm(QString::NormalizationForm_C)
176 Item NormalizeSpaceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
178 const Item arg(m_operands.first()->evaluateSingleton(context));
181 return CommonValues::EmptyString;
183 return toItem(AtomicString::fromValue(arg.stringValue().simplified()));
186 Item NormalizeUnicodeFN::evaluateSingleton(const DynamicContext::Ptr &context) const
188 const Item arg(m_operands.first()->evaluateSingleton(context));
191 return CommonValues::EmptyString;
195 /* The second argument has been removed, if we've already determined the form. */
196 if(m_operands.count() == 1)
197 normForm = m_normForm;
200 normForm = determineNormalizationForm(context);
202 return toItem(AtomicString::fromValue(arg.stringValue()));
205 return AtomicString::fromValue(arg.stringValue().normalized(
206 static_cast<QString::NormalizationForm>(normForm)));
209 Expression::Ptr NormalizeUnicodeFN::compress(const StaticContext::Ptr &context)
211 const Expression::Ptr me(FunctionCall::compress(context));
215 Q_ASSERT(m_operands.count() == 1 || m_operands.count() == 2);
217 if(m_operands.count() == 1)
218 m_normForm = QString::NormalizationForm_C;
219 else if(m_operands.last()->is(IDStringValue))
221 m_normForm = static_cast<QString::NormalizationForm>(
222 determineNormalizationForm(context->dynamicContext()));
225 return m_operands.first();
227 /* Remove the operand since we don't need it anymore. */
228 m_operands.removeLast();
234 int NormalizeUnicodeFN::determineNormalizationForm(const DynamicContext::Ptr &context) const
236 const QString strRepr(m_operands.last()->evaluateSingleton(context).stringValue().trimmed().toUpper());
238 /* TODO. Put these values in a QHash for faster lookup. Keep thread safety in mind. */
239 if(strRepr.isEmpty())
241 else if(strRepr == QLatin1String("NFC"))
242 return QString::NormalizationForm_C;
243 else if(strRepr == QLatin1String("NFD"))
244 return QString::NormalizationForm_D;
245 else if(strRepr == QLatin1String("NFKC"))
246 return QString::NormalizationForm_KC;
247 else if(strRepr == QLatin1String("NFKD"))
248 return QString::NormalizationForm_KD;
251 /* What form is FULLY_NORMALIZED? Is a code path available for that somewhere? */
252 context->error(QtXmlPatterns::tr("The normalization form %1 is "
253 "unsupported. The supported forms are "
254 "%2, %3, %4, and %5, and none, i.e. "
255 "the empty string (no normalization).")
256 .arg(formatKeyword(strRepr))
257 .arg(formatKeyword("NFC"))
258 .arg(formatKeyword("NFD"))
259 .arg(formatKeyword("NFKC"))
260 .arg(formatKeyword("NFKD")),
261 ReportContext::FOCH0003,
263 return QString::NormalizationForm_C; /* Silence compiler warning. */
267 Item UpperCaseFN::evaluateSingleton(const DynamicContext::Ptr &context) const
269 const Item item(m_operands.first()->evaluateSingleton(context));
272 return CommonValues::EmptyString;
274 return AtomicString::fromValue(item.stringValue().toUpper());
277 Item LowerCaseFN::evaluateSingleton(const DynamicContext::Ptr &context) const
279 const Item item(m_operands.first()->evaluateSingleton(context));
282 return CommonValues::EmptyString;
284 return AtomicString::fromValue(item.stringValue().toLower());
287 Item TranslateFN::evaluateSingleton(const DynamicContext::Ptr &context) const
289 const Item item(m_operands.first()->evaluateSingleton(context));
292 return CommonValues::EmptyString;
294 const QString mapString(m_operands.at(1)->evaluateSingleton(context).stringValue());
295 const QString arg(item.stringValue());
297 if(mapString.isEmpty())
298 return AtomicString::fromValue(arg);
300 const QString transString(m_operands.at(2)->evaluateSingleton(context).stringValue());
301 const int transLen = transString.length();
302 const int argLen = arg.length();
305 result.reserve(argLen);
308 for(int i = 0; i < argLen; ++i)
310 const QChar argCh(arg.at(i));
311 const int mapPos = mapString.indexOf(argCh);
315 result[outI] = argCh;
319 else if(mapPos >= transLen)
322 const QChar transCh(transString.at(mapPos));
327 result[outI] = transCh;
331 result.truncate(outI);
332 return AtomicString::fromValue(result);
335 EncodeString::EncodeString(const QByteArray &excludeChars,
336 const QByteArray &includeChars) : m_excludeChars(excludeChars),
337 m_includeChars(includeChars)
341 Item EncodeString::evaluateSingleton(const DynamicContext::Ptr &context) const
343 const Item item(m_operands.first()->evaluateSingleton(context));
346 return CommonValues::EmptyString;
348 return AtomicString::fromValue(QString::fromAscii(QUrl::toPercentEncoding(item.stringValue(),
350 m_includeChars).constData()));
353 const char *const EncodeForURIFN::include = "#!*'()";
355 EncodeForURIFN::EncodeForURIFN() : EncodeString(QByteArray(), QByteArray::fromRawData(include, 6))
359 const char *const IriToURIFN::exclude = "#-_!~*'();?@&=+$,[]/:%";
361 IriToURIFN::IriToURIFN() : EncodeString(QByteArray::fromRawData(exclude, 22), QByteArray())
365 const char *const EscapeHtmlURIFN::include = "?&[]%";
366 const char *const EscapeHtmlURIFN::exclude = " :;=@!./+*()-,#$'";
368 EscapeHtmlURIFN::EscapeHtmlURIFN() : EncodeString(QByteArray::fromRawData(exclude, 17),
369 QByteArray::fromRawData(include, 6))