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 ** 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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QStringList>
44 #include "qbuiltintypes_p.h"
45 #include "qpatternistlocale_p.h"
46 #include "qvalidationerror_p.h"
48 #include "qabstractduration_p.h"
52 using namespace QPatternist;
54 AbstractDuration::AbstractDuration(const bool isPos) : m_isPositive(isPos)
58 #define error(msg) return ValidationError::createError(msg);
59 #define getCapt(sym) ((captTable.sym == -1) ? QString() : capts.at(captTable.sym))
61 AtomicValue::Ptr AbstractDuration::create(const CaptureTable &captTable,
62 const QString &lexical,
65 MonthProperty *months,
66 DayCountProperty *days,
68 MinuteProperty *minutes,
69 SecondProperty *seconds,
70 MSecondProperty *mseconds)
72 /* We don't directly write into the arguments(eg @p years) but uses these
73 * because the arguments are intended for normalized values, and therefore
74 * can cause overflows. */
75 MonthCountProperty monthCount = 0;
76 MinuteCountProperty minCount = 0;
77 HourCountProperty hourCount = 0;
78 SecondCountProperty secCount = 0;
81 QRegExp myExp(captTable.regExp); /* Copy, in order to stay thread safe. */
83 if(!myExp.exactMatch(lexical))
88 const QStringList capts(myExp.capturedTexts());
93 if(getCapt(tDelimiter).isEmpty())
95 if((years && getCapt(year).isEmpty() && getCapt(month).isEmpty() && getCapt(day).isEmpty())
97 (!years && getCapt(day).isEmpty()))
99 error(QtXmlPatterns::tr("At least one component must be present."));
102 else if(getCapt(hour).isEmpty() &&
103 getCapt(minutes).isEmpty() &&
104 getCapt(seconds).isEmpty() &&
105 getCapt(mseconds).isEmpty())
107 error(QtXmlPatterns::tr("At least one time component must appear "
108 "after the %1-delimiter.")
109 .arg(formatKeyword("T")));
112 else if(getCapt(year).isEmpty() && getCapt(month).isEmpty()) /* This checks xs:yearMonthDuration. */
114 error(QtXmlPatterns::tr("At least one component must be present."));
117 /* If we got no '-', we are positive. */
118 *isPositive = capts.at(1).isEmpty();
127 *days = getCapt(day).toInt();
128 hourCount = getCapt(hour).toInt();
129 minCount = getCapt(minutes).toInt();
130 secCount = getCapt(seconds).toInt();
132 const QString msecondsStr(getCapt(mseconds));
133 if(!msecondsStr.isEmpty())
134 *mseconds = msecondsStr.leftJustified(3, QLatin1Char('0')).toInt();
136 *mseconds = msecondsStr.toInt();
140 minCount += secCount / 60;
141 *seconds = secCount % 60;
148 hourCount += minCount / 60;
149 *minutes = minCount % 60;
156 *days += hourCount / 24;
157 *hours = hourCount % 24;
164 return AtomicValue::Ptr();
166 /* We're supposed to handle years/months. */
169 *years = getCapt(year).toInt();
170 monthCount = getCapt(month).toInt();
174 *years += monthCount / 12;
175 *months = monthCount % 12;
178 *months = monthCount;
180 return AtomicValue::Ptr();
185 bool AbstractDuration::operator==(const AbstractDuration &other) const
187 if(years() == other.years()
188 && months() == other.months()
189 && days() == other.days()
190 && hours() == other.hours()
191 && minutes() == other.minutes()
192 && seconds() == other.seconds()
193 && mseconds() == other.mseconds())
195 if(isPositive() == other.isPositive())
205 return true; /* Signedness doesn't matter if all are zero. */
212 QString AbstractDuration::serializeMSeconds(const MSecondProperty mseconds)
215 retval.append(QLatin1Char('.'));
217 MSecondProperty msecs = mseconds;
222 retval.append(QLatin1Char(d + '0'));
230 bool AbstractDuration::isPositive() const