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 QtScxml 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 "qscxmlerror.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | class QScxmlError::ScxmlErrorPrivate |
45 | { |
46 | public: |
47 | ScxmlErrorPrivate() |
48 | : line(-1) |
49 | , column(-1) |
50 | {} |
51 | |
52 | QString fileName; |
53 | int line; |
54 | int column; |
55 | QString description; |
56 | }; |
57 | |
58 | /*! |
59 | * \class QScxmlError |
60 | * \brief The QScxmlError class describes the errors returned by the Qt SCXML |
61 | * state machine when parsing an SCXML file. |
62 | * \since 5.7 |
63 | * \inmodule QtScxml |
64 | * |
65 | * \sa QScxmlStateMachine QScxmlCompiler |
66 | */ |
67 | |
68 | /*! |
69 | \property QScxmlError::column |
70 | \brief The column number in which the SCXML error occurred. |
71 | */ |
72 | |
73 | /*! |
74 | \property QScxmlError::description |
75 | \brief A description of the SCXML error. |
76 | */ |
77 | |
78 | /*! |
79 | \property QScxmlError::fileName |
80 | \brief The name of the file in which the SCXML error occurred. |
81 | */ |
82 | |
83 | /*! |
84 | \property QScxmlError::line |
85 | \brief The line number on which the SCXML error occurred. |
86 | */ |
87 | |
88 | /*! |
89 | \property QScxmlError::valid |
90 | \brief Whether the SCXML error is valid. |
91 | */ |
92 | |
93 | /*! |
94 | * Creates a new invalid SCXML error. |
95 | */ |
96 | QScxmlError::QScxmlError() |
97 | : d(nullptr) |
98 | {} |
99 | |
100 | /*! |
101 | * Creates a new valid SCXML error that contains the error message, |
102 | * \a description, as well as the \a fileName, \a line, and \a column where the |
103 | * error occurred. |
104 | */ |
105 | QScxmlError::QScxmlError(const QString &fileName, int line, int column, const QString &description) |
106 | : d(new ScxmlErrorPrivate) |
107 | { |
108 | d->fileName = fileName; |
109 | d->line = line; |
110 | d->column = column; |
111 | d->description = description; |
112 | } |
113 | |
114 | /*! |
115 | * Constructs a copy of \a other. |
116 | */ |
117 | QScxmlError::QScxmlError(const QScxmlError &other) |
118 | : d(nullptr) |
119 | { |
120 | *this = other; |
121 | } |
122 | |
123 | /*! |
124 | * Assigns \a other to this SCXML error and returns a reference to this SCXML |
125 | * error. |
126 | */ |
127 | QScxmlError &QScxmlError::operator=(const QScxmlError &other) |
128 | { |
129 | if (other.d) { |
130 | if (!d) |
131 | d = new ScxmlErrorPrivate; |
132 | d->fileName = other.d->fileName; |
133 | d->line = other.d->line; |
134 | d->column = other.d->column; |
135 | d->description = other.d->description; |
136 | } else { |
137 | delete d; |
138 | d = nullptr; |
139 | } |
140 | return *this; |
141 | } |
142 | |
143 | /*! |
144 | * Destroys the SCXML error. |
145 | */ |
146 | QScxmlError::~QScxmlError() |
147 | { |
148 | delete d; |
149 | d = nullptr; |
150 | } |
151 | |
152 | /*! |
153 | * Returns \c true if the error is valid, \c false otherwise. An invalid error |
154 | * can only be created by calling the default constructor or by assigning an |
155 | * invalid error. |
156 | */ |
157 | bool QScxmlError::isValid() const |
158 | { |
159 | return d != nullptr; |
160 | } |
161 | |
162 | /*! |
163 | * Returns the name of the file in which the error occurred. |
164 | */ |
165 | QString QScxmlError::fileName() const |
166 | { |
167 | return isValid() ? d->fileName : QString(); |
168 | } |
169 | |
170 | /*! |
171 | * Returns the line on which the error occurred. |
172 | */ |
173 | int QScxmlError::line() const |
174 | { |
175 | return isValid() ? d->line : -1; |
176 | } |
177 | |
178 | /*! |
179 | * Returns the column in which the error occurred. |
180 | */ |
181 | int QScxmlError::column() const |
182 | { |
183 | return isValid() ? d->column : -1; |
184 | } |
185 | |
186 | /*! |
187 | * Returns the error message. |
188 | */ |
189 | QString QScxmlError::description() const |
190 | { |
191 | return isValid() ? d->description : QString(); |
192 | } |
193 | |
194 | /*! |
195 | * This convenience method converts an error to a string. |
196 | * Returns the error message formatted as: |
197 | * \e {"filename:line:column: error: description"} |
198 | */ |
199 | QString QScxmlError::toString() const |
200 | { |
201 | QString str; |
202 | if (!isValid()) |
203 | return str; |
204 | |
205 | if (d->fileName.isEmpty()) |
206 | str = QStringLiteral("<Unknown File>" ); |
207 | else |
208 | str = d->fileName; |
209 | if (d->line != -1) { |
210 | str += QStringLiteral(":%1" ).arg(a: d->line); |
211 | if (d->column != -1) |
212 | str += QStringLiteral(":%1" ).arg(a: d->column); |
213 | } |
214 | str += QStringLiteral(": error: " ) + d->description; |
215 | |
216 | return str; |
217 | } |
218 | |
219 | QT_END_NAMESPACE |
220 | |