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 "qatomiccomparators_p.h"
41#include "qatomicstring_p.h"
42#include "qcomparisonplatform_p.h"
43#include "qvaluefactory_p.h"
44
45#include "qcomparisonfactory_p.h"
46
47QT_BEGIN_NAMESPACE
48
49using namespace QPatternist;
50
51/**
52 * @short Helper class for ComparisonFactory::fromLexical() which exposes
53 * CastingPlatform appropriately.
54 *
55 * @relates ComparisonFactory
56 */
57class PerformComparison : public ComparisonPlatform<PerformComparison, true>
58 , public SourceLocationReflection
59{
60public:
61 PerformComparison(const SourceLocationReflection *const sourceLocationReflection,
62 const AtomicComparator::Operator op) : m_sourceReflection(sourceLocationReflection)
63 , m_operator(op)
64 {
65 Q_ASSERT(m_sourceReflection);
66 }
67
68 bool operator()(const AtomicValue::Ptr &operand1,
69 const AtomicValue::Ptr &operand2,
70 const SchemaType::Ptr &type,
71 const ReportContext::Ptr &context)
72 {
73 const ItemType::Ptr asItemType((AtomicType::Ptr(type)));
74
75 /* One area where the Query Transform world differs from the Schema
76 * world is that @c xs:duration is not considedered comparable, because
77 * it's according to Schema is partially comparable. This means
78 * ComparisonPlatform::fetchComparator() flags it as impossible, and
79 * hence we need to override that.
80 *
81 * SchemaType::wxsTypeMatches() will return true for sub-types of @c
82 * xs:duration as well, but that's ok since AbstractDurationComparator
83 * works for them too. */
84 if(BuiltinTypes::xsDuration->wxsTypeMatches(other: type))
85 prepareComparison(c: AtomicComparator::Ptr(new AbstractDurationComparator()));
86 else if (BuiltinTypes::xsGYear->wxsTypeMatches(other: type) ||
87 BuiltinTypes::xsGYearMonth->wxsTypeMatches(other: type) ||
88 BuiltinTypes::xsGMonth->wxsTypeMatches(other: type) ||
89 BuiltinTypes::xsGMonthDay->wxsTypeMatches(other: type) ||
90 BuiltinTypes::xsGDay->wxsTypeMatches(other: type))
91 prepareComparison(c: AtomicComparator::Ptr(new AbstractDateTimeComparator()));
92 else
93 prepareComparison(c: fetchComparator(t1: asItemType, t2: asItemType, context));
94
95 return flexibleCompare(it1: operand1, it2: operand2, context);
96 }
97
98 const SourceLocationReflection *actualReflection() const
99 {
100 return m_sourceReflection;
101 }
102
103 AtomicComparator::Operator operatorID() const
104 {
105 return m_operator;
106 }
107
108private:
109 const SourceLocationReflection *const m_sourceReflection;
110 const AtomicComparator::Operator m_operator;
111};
112
113bool ComparisonFactory::compare(const AtomicValue::Ptr &operand1,
114 const AtomicComparator::Operator op,
115 const AtomicValue::Ptr &operand2,
116 const SchemaType::Ptr &type,
117 const ReportContext::Ptr &context,
118 const SourceLocationReflection *const sourceLocationReflection)
119{
120 Q_ASSERT(operand1);
121 Q_ASSERT(operand2);
122 Q_ASSERT(context);
123 Q_ASSERT(sourceLocationReflection);
124 Q_ASSERT(type);
125 Q_ASSERT_X(type->category() == SchemaType::SimpleTypeAtomic, Q_FUNC_INFO,
126 "We can only compare atomic values.");
127
128 return PerformComparison(sourceLocationReflection, op)(operand1, operand2, type, context);
129}
130
131bool ComparisonFactory::constructAndCompare(const DerivedString<TypeString>::Ptr &operand1,
132 const AtomicComparator::Operator op,
133 const DerivedString<TypeString>::Ptr &operand2,
134 const SchemaType::Ptr &type,
135 const ReportContext::Ptr &context,
136 const SourceLocationReflection *const sourceLocationReflection)
137{
138 Q_ASSERT(operand1);
139 Q_ASSERT(operand2);
140 Q_ASSERT(context);
141 Q_ASSERT(sourceLocationReflection);
142 Q_ASSERT(type);
143 Q_ASSERT_X(type->category() == SchemaType::SimpleTypeAtomic, Q_FUNC_INFO,
144 "We can only compare atomic values.");
145
146 const AtomicValue::Ptr value1 = ValueFactory::fromLexical(lexicalValue: operand1->stringValue(), type, context, sourceLocationReflection);
147 const AtomicValue::Ptr value2 = ValueFactory::fromLexical(lexicalValue: operand2->stringValue(), type, context, sourceLocationReflection);
148
149 return compare(operand1: value1, op, operand2: value2, type, context, sourceLocationReflection);
150}
151
152QT_END_NAMESPACE
153

source code of qtxmlpatterns/src/xmlpatterns/data/qcomparisonfactory.cpp