1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the qmake application of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "xmloutput.h"
QT_BEGIN_NAMESPACE
XmlOutput::XmlOutput(QTextStream &file, ConverstionType type)
: xmlFile(file), indent("\t"), currentLevel(0), currentState(Bare), format(NewLine),
conversion(type)
{
tagStack.clear();
}
XmlOutput::~XmlOutput()
{
closeAll();
}
// Settings ------------------------------------------------------------------
void XmlOutput::setIndentString(const QString &indentString)
{
indent = indentString;
}
QString XmlOutput::indentString()
{
return indent;
}
void XmlOutput::setIndentLevel(int level)
{
currentLevel = level;
}
int XmlOutput::indentLevel()
{
return currentLevel;
}
void XmlOutput::setState(XMLState state)
{
currentState = state;
}
void XmlOutput::setFormat(XMLFormat newFormat)
{
format = newFormat;
}
XmlOutput::XMLState XmlOutput::state()
{
return currentState;
}
void XmlOutput::updateIndent()
{
currentIndent.clear();
for (int i = 0; i < currentLevel; ++i)
currentIndent.append(indent);
}
void XmlOutput::increaseIndent()
{
++currentLevel;
updateIndent();
}
void XmlOutput::decreaseIndent()
{
if (currentLevel)
--currentLevel;
updateIndent();
if (!currentLevel)
currentState = Bare;
}
QString XmlOutput::doConversion(const QString &text)
{
if (!text.count())
return QString();
else if (conversion == NoConversion)
return text;
QString output;
if (conversion == XMLConversion) {
// this is a way to escape characters that shouldn't be converted
for (int i=0; i<text.count(); ++i) {
if (text.at(i) == QLatin1Char('&')) {
if ( (i + 7) < text.count() &&
text.at(i + 1) == QLatin1Char('#') &&
text.at(i + 2) == QLatin1Char('x') &&
text.at(i + 7) == QLatin1Char(';') ) {
output += text.at(i);
} else {
output += QLatin1String("&");
}
} else {
QChar c = text.at(i);
if (c.unicode() < 0x20) {
output += QString("&#x%1;").arg(c.unicode(), 2, 16, QLatin1Char('0'));
} else {
output += text.at(i);
}
}
}
} else {
output = text;
}
if (conversion == XMLConversion) {
output.replace('\"', """);
output.replace('\'', "'");
} else if (conversion == EscapeConversion) {
output.replace('\"', "\\\"");
output.replace('\'', "\\\'");
}
return output;
}
// Stream functions ----------------------------------------------------------
XmlOutput& XmlOutput::operator<<(const QString& o)
{
return operator<<(data(o));
}
XmlOutput& XmlOutput::operator<<(const xml_output& o)
{
switch(o.xo_type) {
case tNothing:
break;
case tRaw:
addRaw(o.xo_text);
break;
case tDeclaration:
addDeclaration(o.xo_text, o.xo_value);
break;
case tTag:
newTagOpen(o.xo_text);
break;
case tTagValue:
addRaw(QString("\n%1<%2>").arg(currentIndent).arg(o.xo_text));
addRaw(QString("%1").arg(o.xo_value));
addRaw(QString("</%1>").arg(o.xo_text));
break;
case tValueTag:
addRaw(QString("%1").arg(doConversion(o.xo_text)));
setFormat(NoNewLine);
closeTag();
setFormat(NewLine);
break;
case tImport:
addRaw(QString("\n%1<Import %2=\"%3\" />").arg(currentIndent).arg(o.xo_text).arg(o.xo_value));
break;
case tCloseTag:
if (o.xo_value.count())
closeAll();
else if (o.xo_text.count())
closeTo(o.xo_text);
else
closeTag();
break;
case tAttribute:
addAttribute(o.xo_text, o.xo_value);
break;
case tAttributeTag:
addAttributeTag(o.xo_text, o.xo_value);
break;
case tData:
{
// Special case to be able to close tag in normal
// way ("</tag>", not "/>") without using addRaw()..
if (!o.xo_text.count()) {
closeOpen();
break;
}
QString output = doConversion(o.xo_text);
output.replace('\n', "\n" + currentIndent);
addRaw(QString("\n%1%2").arg(currentIndent).arg(output));
}
break;
case tComment:
{
QString output("<!--%1-->");
addRaw(output.arg(o.xo_text));
}
break;
case tCDATA:
{
QString output("<![CDATA[\n%1\n]]>");
addRaw(output.arg(o.xo_text));
}
break;
}
return *this;
}
// Output functions ----------------------------------------------------------
void XmlOutput::newTag(const QString &tag)
{
Q_ASSERT_X(tag.count(), "XmlOutput", "Cannot open an empty tag");
newTagOpen(tag);
closeOpen();
}
void XmlOutput::newTagOpen(const QString &tag)
{
Q_ASSERT_X(tag.count(), "XmlOutput", "Cannot open an empty tag");
closeOpen();
if (format == NewLine)
xmlFile << endl << currentIndent;
xmlFile << '<' << doConversion(tag);
currentState = Attribute;
tagStack.append(tag);
increaseIndent(); // ---> indent
}
void XmlOutput::closeOpen()
{
switch(currentState) {
case Bare:
case Tag:
return;
case Attribute:
break;
}
xmlFile << '>';
currentState = Tag;
}
void XmlOutput::closeTag()
{
switch(currentState) {
case Bare:
if (tagStack.count())
//warn_msg(WarnLogic, "<Root>: Cannot close tag in Bare state, %d tags on stack", tagStack.count());
qDebug("<Root>: Cannot close tag in Bare state, %d tags on stack", tagStack.count());
else
//warn_msg(WarnLogic, "<Root>: Cannot close tag, no tags on stack");
qDebug("<Root>: Cannot close tag, no tags on stack");
return;
case Tag:
decreaseIndent(); // <--- Pre-decrease indent
if (format == NewLine)
xmlFile << endl << currentIndent;
xmlFile << "</" << doConversion(tagStack.last()) << '>';
tagStack.pop_back();
break;
case Attribute:
xmlFile << " />";
tagStack.pop_back();
currentState = Tag;
decreaseIndent(); // <--- Post-decrease indent
break;
}
}
void XmlOutput::closeTo(const QString &tag)
{
bool cont = true;
if (!tagStack.contains(tag) && !tag.isNull()) {
//warn_msg(WarnLogic, "<%s>: Cannot close to tag <%s>, not on stack", tagStack.last().latin1(), tag.latin1());
qDebug("<%s>: Cannot close to tag <%s>, not on stack", tagStack.last().toLatin1().constData(), tag.toLatin1().constData());
return;
}
int left = tagStack.count();
while (left-- && cont) {
cont = tagStack.last().compare(tag) != 0;
closeTag();
}
}
void XmlOutput::closeAll()
{
if (!tagStack.count())
return;
closeTo(QString());
}
void XmlOutput::addDeclaration(const QString &version, const QString &encoding)
{
switch(currentState) {
case Bare:
break;
case Tag:
case Attribute:
//warn_msg(WarnLogic, "<%s>: Cannot add declaration when not in bare state", tagStack.last().toLatin1().constData());
qDebug("<%s>: Cannot add declaration when not in bare state", tagStack.last().toLatin1().constData());
return;
}
QString outData = QString("<?xml version=\"%1\" encoding=\"%2\"?>")
.arg(doConversion(version))
.arg(doConversion(encoding));
addRaw(outData);
}
void XmlOutput::addRaw(const QString &rawText)
{
closeOpen();
xmlFile << rawText;
}
void XmlOutput::addAttribute(const QString &attribute, const QString &value)
{
switch(currentState) {
case Bare:
case Tag:
//warn_msg(WarnLogic, "<%s>: Cannot add attribute since tags not open", tagStack.last().toLatin1().constData());
qDebug("<%s>: Cannot add attribute (%s) since tag's not open",
(tagStack.count() ? tagStack.last().toLatin1().constData() : "Root"),
attribute.toLatin1().constData());
return;
case Attribute:
break;
}
if (format == NewLine)
xmlFile << endl;
xmlFile << currentIndent << doConversion(attribute) << "=\"" << doConversion(value) << "\"";
}
void XmlOutput::addAttributeTag(const QString &attribute, const QString &value)
{
switch(currentState) {
case Bare:
case Tag:
//warn_msg(WarnLogic, "<%s>: Cannot add attribute since tags not open", tagStack.last().toLatin1().constData());
qDebug("<%s>: Cannot add attribute (%s) since tag's not open",
(tagStack.count() ? tagStack.last().toLatin1().constData() : "Root"),
attribute.toLatin1().constData());
return;
case Attribute:
break;
}
xmlFile << " " << doConversion(attribute) << "=\"" << doConversion(value) << "\"";
}
QT_END_NAMESPACE
|