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 test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29QT_FORWARD_DECLARE_CLASS(QIODevice)
30QT_FORWARD_DECLARE_CLASS(QString)
31
32#include <QtCore/QFlags>
33
34class QC14N
35{
36public:
37 static bool isEqual(QIODevice *const firstDocument,
38 QIODevice *const secondDocument,
39 QString *const message = 0);
40
41private:
42 static bool isDifferent(const QXmlStreamReader &r1,
43 const QXmlStreamReader &r2,
44 QString *const message);
45 static bool isAttributesEqual(const QXmlStreamReader &r1,
46 const QXmlStreamReader &r2,
47 QString *const message);
48};
49
50#include <QXmlStreamReader>
51
52/*! \internal
53
54 \a firstDocument and \a secondDocument must be pointers to opened devices.
55 */
56bool QC14N::isEqual(QIODevice *const firstDocument,
57 QIODevice *const secondDocument,
58 QString *const message)
59{
60 qDebug() << Q_FUNC_INFO;
61 if (!firstDocument)
62 qFatal(msg: "%s: A valid firstDocument QIODevice pointer must be supplied", Q_FUNC_INFO);
63 if (!secondDocument)
64 qFatal(msg: "%s: A valid secondDocument QIODevice pointer must be supplied", Q_FUNC_INFO);
65 if (!firstDocument->isReadable())
66 qFatal(msg: "%s: The firstDocument device must be readable.", Q_FUNC_INFO);
67 if (!secondDocument->isReadable())
68 qFatal(msg: "%s: The secondDocument device must be readable.", Q_FUNC_INFO);
69
70 QXmlStreamReader r1(firstDocument);
71 QXmlStreamReader r2(secondDocument);
72
73 while(!r1.atEnd())
74 {
75 if(r1.error())
76 {
77 if(message)
78 *message = r1.errorString();
79
80 return false;
81 }
82 else if(r2.error())
83 {
84 if(message)
85 *message = r1.errorString();
86
87 return false;
88 }
89 else
90 {
91 if(isDifferent(r1, r2, message))
92 return true;
93 }
94
95 r1.readNext();
96 r2.readNext();
97 }
98
99 if(!r2.atEnd())
100 {
101 if(message)
102 *message = QLatin1String("Reached the end of the first document, while there was still content left in the second");
103
104 return false;
105 }
106
107 /* And they lived happily ever after. */
108 return true;
109}
110
111/*! \internal
112 */
113bool QC14N::isAttributesEqual(const QXmlStreamReader &r1,
114 const QXmlStreamReader &r2,
115 QString *const message)
116{
117 Q_UNUSED(message);
118
119 const QXmlStreamAttributes &attrs1 = r1.attributes();
120 const QXmlStreamAttributes &attrs2 = r2.attributes();
121 const int len = attrs1.size();
122
123 if(len != attrs2.size())
124 return false;
125
126 for(int i = 0; i < len; ++i)
127 {
128 if(!attrs2.contains(t: attrs1.at(i)))
129 return false;
130 }
131
132 return true;
133}
134
135bool QC14N::isDifferent(const QXmlStreamReader &r1,
136 const QXmlStreamReader &r2,
137 QString *const message)
138{
139 // TODO error reporting can be a lot better here.
140 if(r1.tokenType() != r2.tokenType())
141 return false;
142
143 switch(r1.tokenType())
144 {
145 case QXmlStreamReader::NoToken:
146 /* Fallthrough. */
147 case QXmlStreamReader::StartDocument:
148 /* Fallthrough. */
149 case QXmlStreamReader::EndDocument:
150 /* Fallthrough. */
151 case QXmlStreamReader::DTD:
152 return true;
153 case QXmlStreamReader::Invalid:
154 return false;
155 case QXmlStreamReader::StartElement:
156 {
157 return r1.qualifiedName() == r2.qualifiedName()
158 /* Yes, the namespace test below should be redundant, but with it we
159 * trap namespace bugs in QXmlStreamReader, if any. */
160 && r1.namespaceUri() == r2.namespaceUri()
161 && isAttributesEqual(r1, r2, message);
162
163 }
164 case QXmlStreamReader::EndElement:
165 {
166 return r1.qualifiedName() == r2.qualifiedName()
167 && r1.namespaceUri() == r2.namespaceUri()
168 && r1.name() == r2.name();
169 }
170 case QXmlStreamReader::Characters:
171 /* Fallthrough. */
172 case QXmlStreamReader::Comment:
173 return r1.text() == r2.text();
174 case QXmlStreamReader::EntityReference:
175 case QXmlStreamReader::ProcessingInstruction:
176 {
177 return r1.processingInstructionTarget() == r2.processingInstructionTarget() &&
178 r2.processingInstructionData() == r2.processingInstructionData();
179
180 }
181 default:
182 qFatal(msg: "%s: Unknown tokenType: %d", Q_FUNC_INFO, static_cast<int>(r1.tokenType()));
183 return false;
184 }
185}
186
187

source code of qtbase/tests/auto/corelib/serialization/qxmlstream/qc14n.h