1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QStringList>
|
---|
43 |
|
---|
44 | #include "qbuiltintypes_p.h"
|
---|
45 | #include "qpatternistlocale_p.h"
|
---|
46 | #include "qvalidationerror_p.h"
|
---|
47 |
|
---|
48 | #include "qabstractduration_p.h"
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | using namespace QPatternist;
|
---|
53 |
|
---|
54 | AbstractDuration::AbstractDuration(const bool isPos) : m_isPositive(isPos)
|
---|
55 | {
|
---|
56 | }
|
---|
57 |
|
---|
58 | #define error(msg) return ValidationError::createError(msg);
|
---|
59 | #define getCapt(sym) ((captTable.sym == -1) ? QString() : capts.at(captTable.sym))
|
---|
60 |
|
---|
61 | AtomicValue::Ptr AbstractDuration::create(const CaptureTable &captTable,
|
---|
62 | const QString &lexical,
|
---|
63 | bool *isPositive,
|
---|
64 | YearProperty *years,
|
---|
65 | MonthProperty *months,
|
---|
66 | DayCountProperty *days,
|
---|
67 | HourProperty *hours,
|
---|
68 | MinuteProperty *minutes,
|
---|
69 | SecondProperty *seconds,
|
---|
70 | MSecondProperty *mseconds)
|
---|
71 | {
|
---|
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;
|
---|
79 |
|
---|
80 | Q_ASSERT(isPositive);
|
---|
81 | QRegExp myExp(captTable.regExp); /* Copy, in order to stay thread safe. */
|
---|
82 |
|
---|
83 | if(!myExp.exactMatch(lexical))
|
---|
84 | {
|
---|
85 | error(QString());
|
---|
86 | }
|
---|
87 |
|
---|
88 | const QStringList capts(myExp.capturedTexts());
|
---|
89 |
|
---|
90 |
|
---|
91 | if(days)
|
---|
92 | {
|
---|
93 | if(getCapt(tDelimiter).isEmpty())
|
---|
94 | {
|
---|
95 | if((years && getCapt(year).isEmpty() && getCapt(month).isEmpty() && getCapt(day).isEmpty())
|
---|
96 | ||
|
---|
97 | (!years && getCapt(day).isEmpty()))
|
---|
98 | {
|
---|
99 | error(QtXmlPatterns::tr("At least one component must be present."));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | else if(getCapt(hour).isEmpty() &&
|
---|
103 | getCapt(minutes).isEmpty() &&
|
---|
104 | getCapt(seconds).isEmpty() &&
|
---|
105 | getCapt(mseconds).isEmpty())
|
---|
106 | {
|
---|
107 | error(QtXmlPatterns::tr("At least one time component must appear "
|
---|
108 | "after the %1-delimiter.")
|
---|
109 | .arg(formatKeyword("T")));
|
---|
110 | }
|
---|
111 | }
|
---|
112 | else if(getCapt(year).isEmpty() && getCapt(month).isEmpty()) /* This checks xs:yearMonthDuration. */
|
---|
113 | {
|
---|
114 | error(QtXmlPatterns::tr("At least one component must be present."));
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* If we got no '-', we are positive. */
|
---|
118 | *isPositive = capts.at(1).isEmpty();
|
---|
119 |
|
---|
120 | if(days)
|
---|
121 | {
|
---|
|
---|