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