1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qscxmlerror.h"
5
6QT_BEGIN_NAMESPACE
7
8class QScxmlError::ScxmlErrorPrivate
9{
10public:
11 ScxmlErrorPrivate()
12 : line(-1)
13 , column(-1)
14 {}
15
16 QString fileName;
17 int line;
18 int column;
19 QString description;
20};
21
22/*!
23 * \class QScxmlError
24 * \brief The QScxmlError class describes the errors returned by the Qt SCXML
25 * state machine when parsing an SCXML file.
26 * \since 5.7
27 * \inmodule QtScxml
28 *
29 * \sa QScxmlStateMachine QScxmlCompiler
30 */
31
32/*!
33 \property QScxmlError::column
34 \brief The column number in which the SCXML error occurred.
35*/
36
37/*!
38 \property QScxmlError::description
39 \brief A description of the SCXML error.
40*/
41
42/*!
43 \property QScxmlError::fileName
44 \brief The name of the file in which the SCXML error occurred.
45*/
46
47/*!
48 \property QScxmlError::line
49 \brief The line number on which the SCXML error occurred.
50*/
51
52/*!
53 \property QScxmlError::valid
54 \brief Whether the SCXML error is valid.
55*/
56
57/*!
58 * Creates a new invalid SCXML error.
59 */
60QScxmlError::QScxmlError()
61 : d(nullptr)
62{}
63
64/*!
65 * Creates a new valid SCXML error that contains the error message,
66 * \a description, as well as the \a fileName, \a line, and \a column where the
67 * error occurred.
68 */
69QScxmlError::QScxmlError(const QString &fileName, int line, int column, const QString &description)
70 : d(new ScxmlErrorPrivate)
71{
72 d->fileName = fileName;
73 d->line = line;
74 d->column = column;
75 d->description = description;
76}
77
78/*!
79 * Constructs a copy of \a other.
80 */
81QScxmlError::QScxmlError(const QScxmlError &other)
82 : d(nullptr)
83{
84 *this = other;
85}
86
87/*!
88 * Assigns \a other to this SCXML error and returns a reference to this SCXML
89 * error.
90 */
91QScxmlError &QScxmlError::operator=(const QScxmlError &other)
92{
93 if (other.d) {
94 if (!d)
95 d = new ScxmlErrorPrivate;
96 d->fileName = other.d->fileName;
97 d->line = other.d->line;
98 d->column = other.d->column;
99 d->description = other.d->description;
100 } else {
101 delete d;
102 d = nullptr;
103 }
104 return *this;
105}
106
107/*!
108 * Destroys the SCXML error.
109 */
110QScxmlError::~QScxmlError()
111{
112 delete d;
113 d = nullptr;
114}
115
116/*!
117 * Returns \c true if the error is valid, \c false otherwise. An invalid error
118 * can only be created by calling the default constructor or by assigning an
119 * invalid error.
120 */
121bool QScxmlError::isValid() const
122{
123 return d != nullptr;
124}
125
126/*!
127 * Returns the name of the file in which the error occurred.
128 */
129QString QScxmlError::fileName() const
130{
131 return isValid() ? d->fileName : QString();
132}
133
134/*!
135 * Returns the line on which the error occurred.
136 */
137int QScxmlError::line() const
138{
139 return isValid() ? d->line : -1;
140}
141
142/*!
143 * Returns the column in which the error occurred.
144 */
145int QScxmlError::column() const
146{
147 return isValid() ? d->column : -1;
148}
149
150/*!
151 * Returns the error message.
152 */
153QString QScxmlError::description() const
154{
155 return isValid() ? d->description : QString();
156}
157
158/*!
159 * This convenience method converts an error to a string.
160 * Returns the error message formatted as:
161 * \e {"filename:line:column: error: description"}
162 */
163QString QScxmlError::toString() const
164{
165 QString str;
166 if (!isValid())
167 return str;
168
169 if (d->fileName.isEmpty())
170 str = QStringLiteral("<Unknown File>");
171 else
172 str = d->fileName;
173 if (d->line != -1) {
174 str += QStringLiteral(":%1").arg(a: d->line);
175 if (d->column != -1)
176 str += QStringLiteral(":%1").arg(a: d->column);
177 }
178 str += QStringLiteral(": error: ") + d->description;
179
180 return str;
181}
182
183QT_END_NAMESPACE
184

source code of qtscxml/src/scxml/qscxmlerror.cpp