lib Library API Documentation

koxmlwriter.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef XMLWRITER_H
00021 #define XMLWRITER_H
00022 
00023 #include <qstring.h>
00024 #include <qvaluestack.h>
00025 #include <qmap.h>
00026 #include <koffice_export.h>
00027 
00028 class QIODevice;
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.utf8() );
00078     }
00082     inline void addAttribute( const char* attrName, int value ) {
00083         QCString str;
00084         str.setNum( value );
00085         addAttribute( attrName, str.data() );
00086     }
00090     inline void addAttribute( const char* attrName, uint value ) {
00091         QCString str;
00092         str.setNum( value );
00093         addAttribute( attrName, str.data() );
00094     }
00100     void addAttribute( const char* attrName, double value );
00107     void addAttributePt( const char* attrName, double value );
00108 
00110     inline void addAttribute( const char* attrName, const QCString& value ) {
00111         addAttribute( attrName, value.data() );
00112     }
00116     void addAttribute( const char* attrName, const char* value );
00121     void endElement();
00126     inline void addTextNode( const QString& str ) {
00127         addTextNode( str.utf8() );
00128     }
00130     inline void addTextNode( const QCString& cstr ) {
00131         addTextNode( cstr.data() );
00132     }
00140     void addTextNode( const char* cstr );
00141 
00151     void addProcessingInstruction( const char* cstr );
00152 
00159     void addCompleteElement( const char* cstr );
00160 
00168     void addCompleteElement( QIODevice* dev );
00169 
00170     // #### Maybe we want to subclass KoXmlWriter for manifest files.
00178     void addManifestEntry( const QString& fullPath, const QString& mediaType );
00179 
00184     void addConfigItem( const QString & configName, const QString& value );
00186     void addConfigItem( const QString & configName, bool value );
00188     void addConfigItem( const QString & configName, int value );
00190     void addConfigItem( const QString & configName, double value );
00192     void addConfigItem( const QString & configName, long value );
00194     void addConfigItem( const QString & configName, short value );
00195 
00196     // TODO addConfigItem for datetime and base64Binary
00197 
00208     void addTextSpan( const QString& text );
00214     void addTextSpan( const QString& text, const QMap<int, int>& tabCache );
00215 
00220     int indentLevel() const { return m_tags.size() + m_baseIndentLevel; }
00221 
00222 private:
00223     struct Tag {
00224         Tag( const char* t = 0, bool ind = true )
00225             : tagName( t ), hasChildren( false ), lastChildIsText( false ),
00226               openingTagClosed( false ), indentInside( ind ) {}
00227         const char* tagName;
00228         bool hasChildren; 
00229         bool lastChildIsText; 
00230         bool openingTagClosed; 
00231         bool indentInside; 
00232     };
00233 
00235     void writeIndent();
00236 
00237     // writeCString is much faster than writeString.
00238     // Try to use it as much as possible, especially with constants.
00239     void writeString( const QString& str );
00240 
00241     // unused and possibly incorrect if length != size
00242     //inline void writeCString( const QCString& cstr ) {
00243     //    m_dev->writeBlock( cstr.data(), cstr.size() - 1 );
00244     //}
00245 
00246     inline void writeCString( const char* cstr ) {
00247         m_dev->writeBlock( cstr, qstrlen( cstr ) );
00248     }
00249     inline void writeChar( char c ) {
00250         m_dev->putch( c );
00251     }
00252     inline void closeStartElement( Tag& tag ) {
00253         if ( !tag.openingTagClosed ) {
00254             tag.openingTagClosed = true;
00255             writeChar( '>' );
00256         }
00257     }
00258     char* escapeForXML( const char* source, int length ) const;
00259     bool prepareForChild();
00260     void prepareForTextNode();
00261     void init();
00262 
00263     QIODevice* m_dev;
00264     QValueStack<Tag> m_tags;
00265     int m_baseIndentLevel;
00266 
00267     class Private;
00268     Private *d;
00269 
00270     char* m_indentBuffer; // maybe make it static, but then it needs a KStaticDeleter,
00271                           // and would eat 1K all the time... Maybe refcount it :)
00272     char* m_escapeBuffer; // can't really be static if we want to be thread-safe
00273     static const int s_escapeBufferLen = 10000;
00274 
00275     KoXmlWriter( const KoXmlWriter & ); // forbidden
00276     KoXmlWriter& operator=( const KoXmlWriter & ); // forbidden
00277 };
00278 
00279 #endif /* XMLWRITER_H */
00280 
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:14 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003