1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef XMLOUTPUT_H
5#define XMLOUTPUT_H
6
7#include <qtextstream.h>
8#include <qstack.h>
9
10QT_BEGIN_NAMESPACE
11
12class XmlOutput
13{
14public:
15 enum ConverstionType {
16 NoConversion, // No change
17 EscapeConversion, // Use '\"'
18 XMLConversion // Use &quot;
19 };
20 enum XMLFormat {
21 NoNewLine, // No new lines, unless added manually
22 NewLine // All properties & tags indented on new lines
23 };
24 enum XMLState {
25 Bare, // Not in tag or attribute
26 Tag, // <tagname attribute1="value"
27 Attribute // attribute2="value">
28 };
29 enum XMLType {
30 tNothing, // No XML output, and not state change
31 tRaw, // Raw text (no formating)
32 tDeclaration, // <?xml version="x.x" encoding="xxx"?>
33 tTag, // <tagname attribute1="value"
34 tTagValue, // <tagname>value</tagname>
35 tValueTag, // value</tagname>
36 tCloseTag, // Closes an open tag
37 tAttribute, // attribute2="value">
38 tAttributeTag, // attribute on the same line as a tag
39 tData, // Tag data (formating done)
40 tImport, // <import "type"="path" />
41 tComment, // <!-- Comment -->
42 tCDATA // <![CDATA[ ... ]]>
43 };
44
45 XmlOutput(QTextStream &file, ConverstionType type = XMLConversion);
46 ~XmlOutput();
47
48 // Settings
49 void setIndentString(const QString &indentString);
50 QString indentString();
51 void setIndentLevel(int level);
52 int indentLevel();
53 void setState(XMLState state);
54 void setFormat(XMLFormat newFormat);
55 XMLState state();
56
57
58 struct xml_output {
59 XMLType xo_type; // Type of struct instance
60 QString xo_text; // Tag/Attribute name/xml version
61 QString xo_value; // Value of attributes/xml encoding
62
63 xml_output(XMLType type, const QString &text, const QString &value)
64 : xo_type(type), xo_text(text), xo_value(value) {}
65 xml_output(const xml_output &xo)
66 : xo_type(xo.xo_type), xo_text(xo.xo_text), xo_value(xo.xo_value) {}
67 };
68
69 // Streams
70 XmlOutput& operator<<(const QString& o);
71 XmlOutput& operator<<(const xml_output& o);
72
73private:
74 void increaseIndent();
75 void decreaseIndent();
76 void updateIndent();
77
78 QString doConversion(const QString &text);
79
80 // Output functions
81 void newTag(const QString &tag);
82 void newTagOpen(const QString &tag);
83 void closeOpen();
84 void closeTag();
85 void closeTo(const QString &tag);
86 void closeAll();
87
88 void addDeclaration(const QString &version, const QString &encoding);
89 void addRaw(const QString &rawText);
90 void addAttribute(const QString &attribute, const QString &value);
91 void addAttributeTag(const QString &attribute, const QString &value);
92 void addData(const QString &data);
93
94 // Data
95 QTextStream &xmlFile;
96 QString indent;
97
98 QString currentIndent;
99 int currentLevel;
100 XMLState currentState;
101
102 XMLFormat format;
103 ConverstionType conversion;
104 QStack<QString> tagStack;
105};
106
107inline XmlOutput::xml_output noxml()
108{
109 return XmlOutput::xml_output(XmlOutput::tNothing, QString(), QString());
110}
111
112inline XmlOutput::xml_output raw(const QString &rawText)
113{
114 return XmlOutput::xml_output(XmlOutput::tRaw, rawText, QString());
115}
116
117inline XmlOutput::xml_output declaration(const QString &version = QString("1.0"),
118 const QString &encoding = QString())
119{
120 return XmlOutput::xml_output(XmlOutput::tDeclaration, version, encoding);
121}
122
123inline XmlOutput::xml_output decl(const QString &version = QString("1.0"),
124 const QString &encoding = QString())
125{
126 return declaration(version, encoding);
127}
128
129inline XmlOutput::xml_output tag(const QString &name)
130{
131 return XmlOutput::xml_output(XmlOutput::tTag, name, QString());
132}
133
134
135inline XmlOutput::xml_output valueTag(const QString &value)
136{
137 return XmlOutput::xml_output(XmlOutput::tValueTag, value, QString());
138}
139
140inline XmlOutput::xml_output tagValue(const QString &tagName, const QString &value)
141{
142 return XmlOutput::xml_output(XmlOutput::tTagValue, tagName, value);
143}
144
145inline XmlOutput::xml_output import(const QString &tagName, const QString &value)
146{
147 return XmlOutput::xml_output(XmlOutput::tImport, tagName, value);
148}
149
150inline XmlOutput::xml_output closetag()
151{
152 return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString());
153}
154
155inline XmlOutput::xml_output closetag(const QString &toTag)
156{
157 return XmlOutput::xml_output(XmlOutput::tCloseTag, toTag, QString());
158}
159
160inline XmlOutput::xml_output closeall()
161{
162 return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString("all"));
163}
164
165inline XmlOutput::xml_output attribute(const QString &name,
166 const QString &value)
167{
168 return XmlOutput::xml_output(XmlOutput::tAttribute, name, value);
169}
170
171inline XmlOutput::xml_output attributeTag(const QString &name,
172 const QString &value)
173{
174 return XmlOutput::xml_output(XmlOutput::tAttributeTag, name, value);
175}
176
177inline XmlOutput::xml_output attr(const QString &name,
178 const QString &value)
179{
180 return attribute(name, value);
181}
182
183inline XmlOutput::xml_output attrTag(const QString &name,
184 const QString &value)
185{
186 return attributeTag(name, value);
187}
188
189inline XmlOutput::xml_output data(const QString &text = QString())
190{
191 return XmlOutput::xml_output(XmlOutput::tData, text, QString());
192}
193
194inline XmlOutput::xml_output comment(const QString &text)
195{
196 return XmlOutput::xml_output(XmlOutput::tComment, text, QString());
197}
198
199inline XmlOutput::xml_output cdata(const QString &text)
200{
201 return XmlOutput::xml_output(XmlOutput::tCDATA, text, QString());
202}
203
204QT_END_NAMESPACE
205
206#endif // XMLOUTPUT_H
207

source code of qtbase/qmake/generators/xmloutput.h