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 | #include "qatomictype_p.h" |
41 | #include "qbuiltintypes_p.h" |
42 | #include "qcommonvalues_p.h" |
43 | #include "qdynamiccontext_p.h" |
44 | #include "qpatternistlocale_p.h" |
45 | #include "qvalidationerror_p.h" |
46 | |
47 | #include "qboolean_p.h" |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | using namespace QPatternist; |
52 | |
53 | bool Boolean::evaluateEBV(const Item::Iterator::Ptr &it, |
54 | const QExplicitlySharedDataPointer<DynamicContext> &context) |
55 | { |
56 | return evaluateEBV(first: it->next(), e: it, context); |
57 | } |
58 | |
59 | bool Boolean::evaluateEBV(const Item &first, |
60 | const Item::Iterator::Ptr &it, |
61 | const QExplicitlySharedDataPointer<DynamicContext> &context) |
62 | { |
63 | Q_ASSERT(it); |
64 | Q_ASSERT(context); |
65 | |
66 | if(!first) |
67 | return false; |
68 | else if(first.isNode()) |
69 | return true; |
70 | |
71 | const Item second(it->next()); |
72 | |
73 | if(second) |
74 | { |
75 | Q_ASSERT(context); |
76 | context->error(message: QtXmlPatterns::tr(sourceText: "Effective Boolean Value cannot be calculated for a sequence " |
77 | "containing two or more atomic values." ), |
78 | errorCode: ReportContext::FORG0006, |
79 | sourceLocation: QSourceLocation()); |
80 | return false; |
81 | } |
82 | else |
83 | return first.as<AtomicValue>()->evaluateEBV(context); |
84 | } |
85 | |
86 | bool Boolean::evaluateEBV(const Item &item, |
87 | const QExplicitlySharedDataPointer<DynamicContext> &context) |
88 | { |
89 | if(!item) |
90 | return false; |
91 | else if(item.isNode()) |
92 | return true; |
93 | else |
94 | return item.as<AtomicValue>()->evaluateEBV(context); |
95 | } |
96 | |
97 | Boolean::Boolean(const bool value) : m_value(value) |
98 | { |
99 | } |
100 | |
101 | QString Boolean::stringValue() const |
102 | { |
103 | return m_value |
104 | ? CommonValues::TrueString->stringValue() |
105 | : CommonValues::FalseString->stringValue(); |
106 | } |
107 | |
108 | bool Boolean::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const |
109 | { |
110 | return m_value; |
111 | } |
112 | |
113 | Boolean::Ptr Boolean::fromValue(const bool value) |
114 | { |
115 | return value ? CommonValues::BooleanTrue : CommonValues::BooleanFalse; |
116 | } |
117 | |
118 | AtomicValue::Ptr Boolean::fromLexical(const QString &lexical) |
119 | { |
120 | const QString val(lexical.trimmed()); /* Apply the whitespace facet. */ |
121 | |
122 | if(val == QLatin1String("true" ) || val == QChar(QLatin1Char('1'))) |
123 | return CommonValues::BooleanTrue; |
124 | else if(val == QLatin1String("false" ) || val == QChar(QLatin1Char('0'))) |
125 | return CommonValues::BooleanFalse; |
126 | else |
127 | return ValidationError::createError(); |
128 | } |
129 | |
130 | ItemType::Ptr Boolean::type() const |
131 | { |
132 | return BuiltinTypes::xsBoolean; |
133 | } |
134 | |
135 | QT_END_NAMESPACE |
136 | |