lib

KoXmlWriter.h
1/* This file is part of the KDE project
2 Copyright (C) 2004 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef XMLWRITER_H
21#define XMLWRITER_H
22
23#include <tqstring.h>
24#include <tqvaluestack.h>
25#include <tqmap.h>
26#include <koffice_export.h>
27
28class TQIODevice;
29
36class KOSTORE_EXPORT KoXmlWriter
37{
38public:
43 KoXmlWriter( TQIODevice* dev, int indentLevel = 0 );
44
47
48 TQIODevice *device() const { return m_dev; }
49
57 void startDocument( const char* rootElemName, const char* publicId = 0, const char* systemId = 0 );
58
60 void endDocument();
61
70 void startElement( const char* tagName, bool indentInside = true );
71
76 inline void addAttribute( const char* attrName, const TQString& value ) {
77 addAttribute( attrName, value.utf8() );
78 }
79
82 inline void addAttribute( const char* attrName, int value ) {
83 TQCString str;
84 str.setNum( value );
85 addAttribute( attrName, str.data() );
86 }
87
90 inline void addAttribute( const char* attrName, uint value ) {
91 TQCString str;
92 str.setNum( value );
93 addAttribute( attrName, str.data() );
94 }
95
100 void addAttribute( const char* attrName, double value );
107 void addAttributePt( const char* attrName, double value );
108
110 inline void addAttribute( const char* attrName, const TQCString& value ) {
111 addAttribute( attrName, value.data() );
112 }
113
116 void addAttribute( const char* attrName, const char* value );
121 void endElement();
126 inline void addTextNode( const TQString& str ) {
127 addTextNode( str.utf8() );
128 }
129
130 inline void addTextNode( const TQCString& cstr ) {
131 addTextNode( cstr.data() );
132 }
133
140 void addTextNode( const char* cstr );
141
151 void addProcessingInstruction( const char* cstr );
152
159 void addCompleteElement( const char* cstr );
160
168 void addCompleteElement( TQIODevice* dev );
169
170 // #### Maybe we want to subclass KoXmlWriter for manifest files.
178 void addManifestEntry( const TQString& fullPath, const TQString& mediaType );
179
184 void addConfigItem( const TQString & configName, const TQString& value );
186 void addConfigItem( const TQString & configName, bool value );
188 void addConfigItem( const TQString & configName, int value );
190 void addConfigItem( const TQString & configName, double value );
192 void addConfigItem( const TQString & configName, long value );
194 void addConfigItem( const TQString & configName, short value );
195
196 // TODO addConfigItem for datetime and base64Binary
197
208 void addTextSpan( const TQString& text );
215 void addTextSpan( const TQString& text, const TQMap<int, int>& tabCache );
216
221 int indentLevel() const { return m_tags.size() + m_baseIndentLevel; }
222
223private:
224 struct Tag {
225 Tag( const char* t = 0, bool ind = true )
226 : tagName( t ), hasChildren( false ), lastChildIsText( false ),
227 openingTagClosed( false ), indentInside( ind ) {}
228 const char* tagName;
229 bool hasChildren;
230 bool lastChildIsText;
231 bool openingTagClosed;
232 bool indentInside;
233 };
234
236 void writeIndent();
237
238 // writeCString is much faster than writeString.
239 // Try to use it as much as possible, especially with constants.
240 void writeString( const TQString& str );
241
242 // unused and possibly incorrect if length != size
243 //inline void writeCString( const TQCString& cstr ) {
244 // m_dev->writeBlock( cstr.data(), cstr.size() - 1 );
245 //}
246
247 inline void writeCString( const char* cstr ) {
248 m_dev->writeBlock( cstr, tqstrlen( cstr ) );
249 }
250 inline void writeChar( char c ) {
251 m_dev->putch( c );
252 }
253 inline void closeStartElement( Tag& tag ) {
254 if ( !tag.openingTagClosed ) {
255 tag.openingTagClosed = true;
256 writeChar( '>' );
257 }
258 }
259 char* escapeForXML( const char* source, int length ) const;
260 bool prepareForChild();
261 void prepareForTextNode();
262 void init();
263
264 TQIODevice* m_dev;
265 TQValueStack<Tag> m_tags;
266 int m_baseIndentLevel;
267
268 class Private;
269 Private *d;
270
271 char* m_indentBuffer; // maybe make it static, but then it needs a KStaticDeleter,
272 // and would eat 1K all the time... Maybe refcount it :)
273 char* m_escapeBuffer; // can't really be static if we want to be thread-safe
274 static const int s_escapeBufferLen = 10000;
275
276 KoXmlWriter( const KoXmlWriter & ); // forbidden
277 KoXmlWriter& operator=( const KoXmlWriter & ); // forbidden
278};
279
280#endif /* XMLWRITER_H */
281
void startElement(const char *tagName, bool indentInside=true)
Start a new element, as a child of the current element.
void addTextNode(const TQString &str)
Overloaded version of addTextNode( const char* ), which is a bit slower because it needs to convert s...
int indentLevel() const
void endDocument()
Call this to terminate an XML document.
void addTextNode(const TQCString &cstr)
Overloaded version of the one taking a const char* argument.
KoXmlWriter(TQIODevice *dev, int indentLevel=0)
Create a KoXmlWriter instance to write out an XML document into the given TQIODevice.
void startDocument(const char *rootElemName, const char *publicId=0, const char *systemId=0)
Start the XML document.
void addAttribute(const char *attrName, const TQCString &value)
Overloaded version of the one taking a const char* argument, for convenience.
void addAttribute(const char *attrName, uint value)
Add an attribute whose value is an unsigned integer.
Definition KoXmlWriter.h:90
void addAttribute(const char *attrName, int value)
Add an attribute whose value is an integer.
Definition KoXmlWriter.h:82
void addAttribute(const char *attrName, const TQString &value)
Overloaded version of addAttribute( const char*, const char* ), which is a bit slower because it need...
Definition KoXmlWriter.h:76