00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef XMLWRITER_H
00021 #define XMLWRITER_H
00022
00023 #include <QString>
00024 #include <QStack>
00025 #include <QMap>
00026 #include <QIODevice>
00027 #include <koffice_export.h>
00028
00029
00036 class KOSTORE_EXPORT KoXmlWriter
00037 {
00038 public:
00043 KoXmlWriter( QIODevice* dev, int indentLevel = 0 );
00044
00046 ~KoXmlWriter();
00047
00048 QIODevice *device() const { return m_dev; }
00049
00057 void startDocument( const char* rootElemName, const char* publicId = 0, const char* systemId = 0 );
00058
00060 void endDocument();
00061
00070 void startElement( const char* tagName, bool indentInside = true );
00071
00076 inline void addAttribute( const char* attrName, const QString& value ) {
00077 addAttribute( attrName, value.toUtf8() );
00078 }
00082 inline void addAttribute( const char* attrName, int value ) {
00083 addAttribute( attrName, QByteArray::number( value ) );
00084 }
00088 inline void addAttribute( const char* attrName, uint value ) {
00089 addAttribute( attrName, QByteArray::number( value ) );
00090 }
00096 void addAttribute( const char* attrName, double value );
00103 void addAttributePt( const char* attrName, double value );
00104
00106 void addAttribute( const char* attrName, const QByteArray& value );
00107
00111 void addAttribute( const char* attrName, const char* value );
00116 void endElement();
00121 inline void addTextNode( const QString& str ) {
00122 addTextNode( str.toUtf8() );
00123 }
00125 void addTextNode( const QByteArray& cstr );
00133 void addTextNode( const char* cstr );
00134
00144 void addProcessingInstruction( const char* cstr );
00145
00152 void addCompleteElement( const char* cstr );
00153
00161 void addCompleteElement( QIODevice* dev );
00162
00163
00171 void addManifestEntry( const QString& fullPath, const QString& mediaType );
00172
00177 void addConfigItem( const QString & configName, const QString& value );
00179 void addConfigItem( const QString & configName, bool value );
00181 void addConfigItem( const QString & configName, int value );
00183 void addConfigItem( const QString & configName, double value );
00185 void addConfigItem( const QString & configName, long value );
00187 void addConfigItem( const QString & configName, short value );
00188
00189
00190
00201 void addTextSpan( const QString& text );
00208 void addTextSpan( const QString& text, const QMap<int, int>& tabCache );
00209
00214 int indentLevel() const { return m_tags.size() + m_baseIndentLevel; }
00215
00216 private:
00217 struct Tag {
00218 Tag( const char* t = 0, bool ind = true )
00219 : tagName( t ), hasChildren( false ), lastChildIsText( false ),
00220 openingTagClosed( false ), indentInside( ind ) {}
00221 const char* tagName;
00222 bool hasChildren;
00223 bool lastChildIsText;
00224 bool openingTagClosed;
00225 bool indentInside;
00226 };
00227
00229 void writeIndent();
00230
00231
00232
00233 void writeString( const QString& str );
00234
00235
00236
00237
00238
00239
00240 inline void writeCString( const char* cstr ) {
00241 m_dev->write( cstr, qstrlen( cstr ) );
00242 }
00243 inline void writeChar( char c ) {
00244 m_dev->putChar( c );
00245 }
00246 inline void closeStartElement( Tag& tag ) {
00247 if ( !tag.openingTagClosed ) {
00248 tag.openingTagClosed = true;
00249 writeChar( '>' );
00250 }
00251 }
00252 char* escapeForXML( const char* source, int length ) const;
00253 bool prepareForChild();
00254 void prepareForTextNode();
00255 void init();
00256
00257 QIODevice* m_dev;
00258 QStack<Tag> m_tags;
00259 int m_baseIndentLevel;
00260
00261 class Private;
00262 Private *d;
00263
00264 char* m_indentBuffer;
00265
00266 char* m_escapeBuffer;
00267 static const int s_escapeBufferLen = 10000;
00268
00269 KoXmlWriter( const KoXmlWriter & );
00270 KoXmlWriter& operator=( const KoXmlWriter & );
00271 };
00272
00273 #endif
00274