1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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