author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the qmake application of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
72 |
tTagValue, // <tagname>value</tagname> |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
73 |
tValueTag, // value</tagname> |
0 | 74 |
tCloseTag, // Closes an open tag |
75 |
tAttribute, // attribute2="value"> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
76 |
tAttributeTag, // attribute on the same line as a tag |
0 | 77 |
tData, // Tag data (formating done) |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
78 |
tImport, // <import "type"="path" /> |
0 | 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); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
92 |
void setFormat(XMLFormat newFormat); |
0 | 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); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
129 |
void addAttributeTag(const QString &attribute, const QString &value); |
0 | 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 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
172 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
173 |
inline XmlOutput::xml_output valueTag(const QString &value) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
174 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
175 |
return XmlOutput::xml_output(XmlOutput::tValueTag, value, QString()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
176 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
177 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
178 |
inline XmlOutput::xml_output tagValue(const QString &tagName, const QString &value) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
179 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
180 |
return XmlOutput::xml_output(XmlOutput::tTagValue, tagName, value); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
181 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
182 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
183 |
inline XmlOutput::xml_output import(const QString &tagName, const QString &value) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
184 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
185 |
return XmlOutput::xml_output(XmlOutput::tImport, tagName, value); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
186 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
187 |
|
0 | 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 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
209 |
inline XmlOutput::xml_output attributeTag(const QString &name, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
210 |
const QString &value) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
211 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
212 |
return XmlOutput::xml_output(XmlOutput::tAttributeTag, name, value); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
213 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
214 |
|
0 | 215 |
inline XmlOutput::xml_output attr(const QString &name, |
216 |
const QString &value) |
|
217 |
{ |
|
218 |
return attribute(name, value); |
|
219 |
} |
|
220 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
221 |
inline XmlOutput::xml_output attrTag(const QString &name, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
222 |
const QString &value) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
223 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
224 |
return attributeTag(name, value); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
225 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
226 |
|
0 | 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 |