| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_AbstractDuration_H |
| 51 | #define Patternist_AbstractDuration_H |
| 52 | |
| 53 | #include <QRegExp> |
| 54 | |
| 55 | #include <private/qitem_p.h> |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | namespace QPatternist |
| 60 | { |
| 61 | /** |
| 62 | * @short Base class for classes implementing durations. |
| 63 | * |
| 64 | * @see <a href="http://www.w3.org/TR/xmlschema-2/#duration">XML Schema Part |
| 65 | * 2: Datatypes Second Edition, 3.2.6 duration</a> |
| 66 | * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dates-and-times">XQuery |
| 67 | * 1.0 and XPath 2.0 Data Model (XDM), 3.3.2 Dates and Times</a> |
| 68 | * @author Frans Englich <frans.englich@nokia.com> |
| 69 | * @ingroup Patternist_xdm |
| 70 | * @todo Documentation is missing/incomplete |
| 71 | */ |
| 72 | class AbstractDuration : public AtomicValue |
| 73 | { |
| 74 | public: |
| 75 | typedef QExplicitlySharedDataPointer<AbstractDuration> Ptr; |
| 76 | |
| 77 | /** |
| 78 | * @short The amount in milli seconds. |
| 79 | */ |
| 80 | typedef qint64 Value; |
| 81 | |
| 82 | /** |
| 83 | * @short Acts as a mapping table for AbstractDuration::create() |
| 84 | * and describes where certain fields in a QRegExp pattern can be found |
| 85 | * for a particular W3C XML Schema duration type. |
| 86 | * |
| 87 | * @author Frans Englich <frans.englich@nokia.com> |
| 88 | * @ingroup Patternist_xdm |
| 89 | */ |
| 90 | class CaptureTable |
| 91 | { |
| 92 | public: |
| 93 | CaptureTable(const QRegExp &exp, |
| 94 | const qint8 yearP, |
| 95 | const qint8 monthP, |
| 96 | const qint8 dayP = -1, |
| 97 | const qint8 tDelimiterP = -1, |
| 98 | const qint8 hourP = -1, |
| 99 | const qint8 minutesP = -1, |
| 100 | const qint8 secondsP = -1, |
| 101 | const qint8 msecondsP = -1) : regExp(exp), |
| 102 | year(yearP), |
| 103 | month(monthP), |
| 104 | day(dayP), |
| 105 | tDelimiter(tDelimiterP), |
| 106 | hour(hourP), |
| 107 | minutes(minutesP), |
| 108 | seconds(secondsP), |
| 109 | mseconds(msecondsP) |
| 110 | { |
| 111 | Q_ASSERT(exp.isValid()); |
| 112 | Q_ASSERT(yearP == -1 || yearP == 2); |
| 113 | } |
| 114 | |
| 115 | QRegExp regExp; |
| 116 | const qint8 year; |
| 117 | const qint8 month; |
| 118 | const qint8 day; |
| 119 | const qint8 tDelimiter; |
| 120 | const qint8 hour; |
| 121 | const qint8 minutes; |
| 122 | const qint8 seconds; |
| 123 | const qint8 mseconds; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * Determines whether this Duration is equal to @p other. |
| 128 | * |
| 129 | * @note Do not re-implement this function. It uses getters such as years() and |
| 130 | * mseconds() for determining its truth value. |
| 131 | */ |
| 132 | bool operator==(const AbstractDuration &other) const; |
| 133 | |
| 134 | virtual YearProperty years() const = 0; |
| 135 | virtual MonthProperty months() const = 0; |
| 136 | virtual DayCountProperty days() const = 0; |
| 137 | virtual HourProperty hours() const = 0; |
| 138 | virtual MinuteProperty minutes() const = 0; |
| 139 | virtual SecondProperty seconds() const = 0; |
| 140 | virtual MSecondProperty mseconds() const = 0; |
| 141 | |
| 142 | /** |
| 143 | * @returns the value of this AbstractDuration. For example, |
| 144 | * in the case of xs:yearMonthDuration, that is YearMonthDuration, |
| 145 | * years times twelve plus the months is returned. |
| 146 | */ |
| 147 | virtual Value value() const = 0; |
| 148 | |
| 149 | /** |
| 150 | * A polymorphic factory function that returns instances of the |
| 151 | * sub-class with the value @p val. |
| 152 | */ |
| 153 | virtual Item fromValue(const Value val) const = 0; |
| 154 | |
| 155 | /** |
| 156 | * Determines whether this AbstractDuration is positive. For example, |
| 157 | * "P10H" is positive, while "-P10H" is not. |
| 158 | * |
| 159 | * @note Do not re-implement this function. Use the constructor, AbstractDuration(), |
| 160 | * for changing the value. |
| 161 | * @returns @c true if this AbstractDuration is positive, otherwise @c false. |
| 162 | */ |
| 163 | bool isPositive() const; |
| 164 | |
| 165 | protected: |
| 166 | |
| 167 | AbstractDuration(const bool isPos); |
| 168 | |
| 169 | static QString serializeMSeconds(const MSecondProperty mseconds); |
| 170 | static AtomicValue::Ptr create(const CaptureTable &captTable, |
| 171 | const QString &lexical, |
| 172 | bool *isPositive, |
| 173 | YearProperty *years, |
| 174 | MonthProperty *months, |
| 175 | DayCountProperty *days, |
| 176 | HourProperty *hours, |
| 177 | MinuteProperty *minutes, |
| 178 | SecondProperty *seconds, |
| 179 | MSecondProperty *mseconds); |
| 180 | const bool m_isPositive; |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | QT_END_NAMESPACE |
| 185 | |
| 186 | #endif |
| 187 | |