1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
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