1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the qmake application of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef XMLOUTPUT_H
|
---|
43 | #define XMLOUTPUT_H
|
---|
44 |
|
---|
45 | #include <qtextstream.h>
|
---|
46 | #include <qstack.h>
|
---|
47 |
|
---|
48 | QT_BEGIN_NAMESPACE
|
---|
49 |
|
---|
50 | class XmlOutput
|
---|
51 | {
|
---|
52 | public:
|
---|
53 | enum ConverstionType {
|
---|
54 | NoConversion, // No change
|
---|
55 | EscapeConversion, // Use '\"'
|
---|
56 | XMLConversion // Use "
|
---|
57 | };
|
---|
58 | enum XMLFormat {
|
---|
59 | NoNewLine, // No new lines, unless added manually
|
---|
60 | NewLine // All properties & tags indented on new lines
|
---|
61 | };
|
---|
62 | enum XMLState {
|
---|
63 | Bare, // Not in tag or attribute
|
---|
64 | Tag, // <tagname attribute1="value"
|
---|
65 | Attribute // attribute2="value">
|
---|
66 | };
|
---|
67 | enum XMLType {
|
---|
68 | tNothing, // No XML output, and not state change
|
---|
69 | tRaw, // Raw text (no formating)
|
---|
70 | tDeclaration, // <?xml version="x.x" encoding="xxx"?>
|
---|
71 | tTag, // <tagname attribute1="value"
|
---|
72 | tTagValue, // <tagname>value</tagname>
|
---|
73 | tValueTag, // value</tagname>
|
---|
74 | tCloseTag, // Closes an open tag
|
---|
75 | tAttribute, // attribute2="value">
|
---|
76 | tAttributeTag, // attribute on the same line as a tag
|
---|
77 | tData, // Tag data (formating done)
|
---|
78 | tImport, // <import "type"="path" />
|
---|
79 | tComment, // <!-- Comment -->
|
---|
80 | tCDATA // <![CDATA[ ... ]]>
|
---|
81 | };
|
---|
82 |
|
---|
83 | XmlOutput(QTextStream &file, ConverstionType type = XMLConversion);
|
---|
84 | ~XmlOutput();
|
---|
85 |
|
---|
86 | // Settings
|
---|
87 | void setIndentString(const QString &indentString);
|
---|
88 | QString indentString();
|
---|
89 | void setIndentLevel(int level);
|
---|
90 | int indentLevel();
|
---|
91 | void setState(XMLState state);
|
---|
92 | void setFormat(XMLFormat newFormat);
|
---|
93 | XMLState state();
|
---|
94 |
|
---|
95 |
|
---|
96 | struct xml_output {
|
---|
97 | XMLType xo_type; // Type of struct instance
|
---|
98 | QString xo_text; // Tag/Attribute name/xml version
|
---|
99 | QString xo_value; // Value of attributes/xml encoding
|
---|
100 |
|
---|
101 | xml_output(XMLType type, const QString &text, const QString &value)
|
---|
102 | : xo_type(type), xo_text(text), xo_value(value) {}
|
---|
103 | xml_output(const xml_output &xo)
|
---|
104 | : xo_type(xo.xo_type), xo_text(xo.xo_text), xo_value(xo.xo_value) {}
|
---|
105 | };
|
---|
106 |
|
---|
107 | // Streams
|
---|
108 | XmlOutput& operator<<(const QString& o);
|
---|
109 | XmlOutput& operator<<(const xml_output& o);
|
---|
110 |
|
---|
111 | private:
|
---|
112 | void increaseIndent();
|
---|
113 | void decreaseIndent();
|
---|
114 | void updateIndent();
|
---|
115 |
|
---|
116 | QString doConversion(const QString &text);
|
---|
117 |
|
---|
118 | // Output functions
|
---|
119 | void newTag(const QString &tag);
|
---|
120 | void newTagOpen(const QString &tag);
|
---|
121 | void closeOpen();
|
---|
122 | void closeTag();
|
---|
123 | void closeTo(const QString &tag);
|
---|
124 | void closeAll();
|
---|
125 |
|
---|
126 | void addDeclaration(const QString &version, const QString &encoding);
|
---|
127 | void addRaw(const QString &rawText);
|
---|
128 | void addAttribute(const QString &attribute, const QString &value);
|
---|
129 | void addAttributeTag(const QString &attribute, const QString &value);
|
---|
130 | void addData(const QString &data);
|
---|
131 |
|
---|
132 | // Data
|
---|
133 | QTextStream &xmlFile;
|
---|
134 | QString indent;
|
---|
135 |
|
---|
136 | QString currentIndent;
|
---|
137 | int currentLevel;
|
---|
138 | XMLState currentState;
|
---|
139 |
|
---|
140 | XMLFormat format;
|
---|
141 | ConverstionType conversion;
|
---|
142 | QStack<QString> tagStack;
|
---|
143 | };
|
---|
144 |
|
---|
145 | inline XmlOutput::xml_output noxml()
|
---|
146 | {
|
---|
147 | return XmlOutput::xml_output(XmlOutput::tNothing, QString(), QString());
|
---|
148 | }
|
---|
149 |
|
---|
150 | inline XmlOutput::xml_output raw(const QString &rawText)
|
---|
151 | {
|
---|
152 | return XmlOutput::xml_output(XmlOutput::tRaw, rawText, QString());
|
---|
153 | }
|
---|
154 |
|
---|
155 | inline XmlOutput::xml_output declaration(const QString &version = QString("1.0"),
|
---|
156 | const QString &encoding = QString())
|
---|
157 | {
|
---|
158 | return XmlOutput::xml_output(XmlOutput::tDeclaration, version, encoding);
|
---|
159 | }
|
---|
160 |
|
---|
161 | inline XmlOutput::xml_output decl(const QString &version = QString("1.0"),
|
---|
162 | const QString &encoding = QString())
|
---|
163 | {
|
---|
164 | return declaration(version, encoding);
|
---|
165 | }
|
---|
166 |
|
---|
167 | inline XmlOutput::xml_output tag(const QString &name)
|
---|
168 | {
|
---|
169 | return XmlOutput::xml_output(XmlOutput::tTag, name, QString());
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | inline XmlOutput::xml_output valueTag(const QString &value)
|
---|
174 | {
|
---|
175 | return XmlOutput::xml_output(XmlOutput::tValueTag, value, QString());
|
---|
176 | }
|
---|
177 |
|
---|
178 | inline XmlOutput::xml_output tagValue(const QString &tagName, const QString &value)
|
---|
179 | {
|
---|
180 | return XmlOutput::xml_output(XmlOutput::tTagValue, tagName, value);
|
---|
181 | }
|
---|
182 |
|
---|
183 | inline XmlOutput::xml_output import(const QString &tagName, const QString &value)
|
---|
184 | {
|
---|
185 | return XmlOutput::xml_output(XmlOutput::tImport, tagName, value);
|
---|
186 | }
|
---|
187 |
|
---|
188 | inline XmlOutput::xml_output closetag()
|
---|
189 | {
|
---|
190 | return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString());
|
---|
191 | }
|
---|
192 |
|
---|
193 | inline XmlOutput::xml_output closetag(const QString &toTag)
|
---|
194 | {
|
---|
195 | return XmlOutput::xml_output(XmlOutput::tCloseTag, toTag, QString());
|
---|
196 | }
|
---|
197 |
|
---|
198 | inline XmlOutput::xml_output closeall()
|
---|
199 | {
|
---|
200 | return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString("all"));
|
---|
201 | }
|
---|
202 |
|
---|
203 | inline XmlOutput::xml_output attribute(const QString &name,
|
---|
204 | const QString &value)
|
---|
205 | {
|
---|
206 | return XmlOutput::xml_output(XmlOutput::tAttribute, name, value);
|
---|
207 | }
|
---|
208 |
|
---|
209 | inline XmlOutput::xml_output attributeTag(const QString &name,
|
---|
210 | const QString &value)
|
---|
211 | {
|
---|
212 | return XmlOutput::xml_output(XmlOutput::tAttributeTag, name, value);
|
---|
213 | }
|
---|
214 |
|
---|
215 | inline XmlOutput::xml_output attr(const QString &name,
|
---|
216 | const QString &value)
|
---|
217 | {
|
---|
218 | return attribute(name, value);
|
---|
219 | }
|
---|
220 |
|
---|
221 | inline XmlOutput::xml_output attrTag(const QString &name,
|
---|
222 | const QString &value)
|
---|
223 | {
|
---|
224 | return attributeTag(name, value);
|
---|
225 | }
|
---|
226 |
|
---|
227 | inline XmlOutput::xml_output data(const QString &text = QString())
|
---|
228 | {
|
---|
229 | return XmlOutput::xml_output(XmlOutput::tData, text, QString());
|
---|
230 | }
|
---|
231 |
|
---|
232 | inline XmlOutput::xml_output comment(const QString &text)
|
---|
233 | {
|
---|
234 | return XmlOutput::xml_output(XmlOutput::tComment, text, QString());
|
---|
235 | }
|
---|
236 |
|
---|
237 | inline XmlOutput::xml_output cdata(const QString &text)
|
---|
238 | {
|
---|
239 | return XmlOutput::xml_output(XmlOutput::tCDATA, text, QString());
|
---|
240 | }
|
---|
241 |
|
---|
242 | QT_END_NAMESPACE
|
---|
243 |
|
---|
244 | #endif // XMLOUTPUT_H
|
---|