1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the qmake application of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department 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 | tCloseTag, // Closes an open tag
|
---|
73 | tAttribute, // attribute2="value">
|
---|
74 | tData, // Tag data (formating done)
|
---|
75 | tComment, // <!-- Comment -->
|
---|
76 | tCDATA // <![CDATA[ ... ]]>
|
---|
77 | };
|
---|
78 |
|
---|
79 | XmlOutput(QTextStream &file, ConverstionType type = XMLConversion);
|
---|
80 | ~XmlOutput();
|
---|
81 |
|
---|
82 | // Settings
|
---|
83 | void setIndentString(const QString &indentString);
|
---|
84 | QString indentString();
|
---|
85 | void setIndentLevel(int level);
|
---|
86 | int indentLevel();
|
---|
87 | void setState(XMLState state);
|
---|
88 | XMLState state();
|
---|
89 |
|
---|
90 |
|
---|
91 | struct xml_output {
|
---|
92 | XMLType xo_type; // Type of struct instance
|
---|
93 | QString xo_text; // Tag/Attribute name/xml version
|
---|
94 | QString xo_value; // Value of attributes/xml encoding
|
---|
95 |
|
---|
96 | xml_output(XMLType type, const QString &text, const QString &value)
|
---|
97 | : xo_type(type), xo_text(text), xo_value(value) {}
|
---|
98 | xml_output(const xml_output &xo)
|
---|
99 | : xo_type(xo.xo_type), xo_text(xo.xo_text), xo_value(xo.xo_value) {}
|
---|
100 | };
|
---|
101 |
|
---|
102 | // Streams
|
---|
103 | XmlOutput& operator<<(const QString& o);
|
---|
104 | XmlOutput& operator<<(const xml_output& o);
|
---|
105 |
|
---|
106 | private:
|
---|
107 | void increaseIndent();
|
---|
108 | void decreaseIndent();
|
---|
109 | void updateIndent();
|
---|
110 |
|
---|
111 | QString doConversion(const QString &text);
|
---|
112 |
|
---|
113 | // Output functions
|
---|
114 | void newTag(const QString &tag);
|
---|
115 | void newTagOpen(const QString &tag);
|
---|
116 | void closeOpen();
|
---|
117 | void closeTag();
|
---|
118 | void closeTo(const QString &tag);
|
---|
119 | void closeAll();
|
---|
120 |
|
---|
121 | void addDeclaration(const QString &version, const QString &encoding);
|
---|
122 | void addRaw(const QString &rawText);
|
---|
123 | void addAttribute(const QString &attribute, const QString &value);
|
---|
124 | void addData(const QString &data);
|
---|
125 |
|
---|
126 | // Data
|
---|
127 | QTextStream &xmlFile;
|
---|
128 | QString indent;
|
---|
129 |
|
---|
130 | QString currentIndent;
|
---|
131 | int currentLevel;
|
---|
132 | XMLState currentState;
|
---|
133 |
|
---|
134 | XMLFormat format;
|
---|
135 | ConverstionType conversion;
|
---|
136 | QStack<QString> tagStack;
|
---|
137 | };
|
---|
138 |
|
---|
139 | inline XmlOutput::xml_output noxml()
|
---|
140 | {
|
---|
141 | return XmlOutput::xml_output(XmlOutput::tNothing, QString(), QString());
|
---|
142 | }
|
---|
143 |
|
---|
144 | inline XmlOutput::xml_output raw(const QString &rawText)
|
---|
145 | {
|
---|
146 | return XmlOutput::xml_output(XmlOutput::tRaw, rawText, QString());
|
---|
147 | }
|
---|
148 |
|
---|
149 | inline XmlOutput::xml_output declaration(const QString &version = QString("1.0"),
|
---|
150 | const QString &encoding = QString())
|
---|
151 | {
|
---|
152 | return XmlOutput::xml_output(XmlOutput::tDeclaration, version, encoding);
|
---|
153 | }
|
---|
154 |
|
---|
155 | inline XmlOutput::xml_output decl(const QString &version = QString("1.0"),
|
---|
156 | const QString &encoding = QString())
|
---|
157 | {
|
---|
158 | return declaration(version, encoding);
|
---|
159 | }
|
---|
160 |
|
---|
161 | inline XmlOutput::xml_output tag(const QString &name)
|
---|
162 | {
|
---|
163 | return XmlOutput::xml_output(XmlOutput::tTag, name, QString());
|
---|
164 | }
|
---|
165 |
|
---|
166 | inline XmlOutput::xml_output closetag()
|
---|
167 | {
|
---|
168 | return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString());
|
---|
169 | }
|
---|
170 |
|
---|
171 | inline XmlOutput::xml_output closetag(const QString &toTag)
|
---|
172 | {
|
---|
173 | return XmlOutput::xml_output(XmlOutput::tCloseTag, toTag, QString());
|
---|
174 | }
|
---|
175 |
|
---|
176 | inline XmlOutput::xml_output closeall()
|
---|
177 | {
|
---|
178 | return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString("all"));
|
---|
179 | }
|
---|
180 |
|
---|
181 | inline XmlOutput::xml_output attribute(const QString &name,
|
---|
182 | const QString &value)
|
---|
183 | {
|
---|
184 | return XmlOutput::xml_output(XmlOutput::tAttribute, name, value);
|
---|
185 | }
|
---|
186 |
|
---|
187 | inline XmlOutput::xml_output attr(const QString &name,
|
---|
188 | const QString &value)
|
---|
189 | {
|
---|
190 | return attribute(name, value);
|
---|
191 | }
|
---|
192 |
|
---|
193 | inline XmlOutput::xml_output data(const QString &text = QString())
|
---|
194 | {
|
---|
195 | return XmlOutput::xml_output(XmlOutput::tData, text, QString());
|
---|
196 | }
|
---|
197 |
|
---|
198 | inline XmlOutput::xml_output comment(const QString &text)
|
---|
199 | {
|
---|
200 | return XmlOutput::xml_output(XmlOutput::tComment, text, QString());
|
---|
201 | }
|
---|
202 |
|
---|
203 | inline XmlOutput::xml_output cdata(const QString &text)
|
---|
204 | {
|
---|
205 | return XmlOutput::xml_output(XmlOutput::tCDATA, text, QString());
|
---|
206 | }
|
---|
207 |
|
---|
208 | QT_END_NAMESPACE
|
---|
209 |
|
---|
210 | #endif // XMLOUTPUT_H
|
---|