lib

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., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, 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 );
00215     void addTextSpan( const QString& text, const QMap<int, int>& tabCache );
00216 
00221     int indentLevel() const { return m_tags.size() + m_baseIndentLevel; }
00222 
00223 private:
00224     struct Tag {
00225         Tag( const char* t = 0, bool ind = true )
00226             : tagName( t ), hasChildren( false ), lastChildIsText( false ),
00227               openingTagClosed( false ), indentInside( ind ) {}
00228         const char* tagName;
00229         bool hasChildren; 
00230         bool lastChildIsText; 
00231         bool openingTagClosed; 
00232         bool indentInside; 
00233     };
00234 
00236     void writeIndent();
00237 
00238     // writeCString is much faster than writeString.
00239     // Try to use it as much as possible, especially with constants.
00240     void writeString( const QString& str );
00241 
00242     // unused and possibly incorrect if length != size
00243     //inline void writeCString( const QCString& cstr ) {
00244     //    m_dev->writeBlock( cstr.data(), cstr.size() - 1 );
00245     //}
00246 
00247     inline void writeCString( const char* cstr ) {
00248         m_dev->writeBlock( cstr, qstrlen( cstr ) );
00249     }
00250     inline void writeChar( char c ) {
00251         m_dev->putch( c );
00252     }
00253     inline void closeStartElement( Tag& tag ) {
00254         if ( !tag.openingTagClosed ) {
00255             tag.openingTagClosed = true;
00256             writeChar( '>' );
00257         }
00258     }
00259     char* escapeForXML( const char* source, int length ) const;
00260     bool prepareForChild();
00261     void prepareForTextNode();
00262     void init();
00263 
00264     QIODevice* m_dev;
00265     QValueStack<Tag> m_tags;
00266     int m_baseIndentLevel;
00267 
00268     class Private;
00269     Private *d;
00270 
00271     char* m_indentBuffer; // maybe make it static, but then it needs a KStaticDeleter,
00272                           // and would eat 1K all the time... Maybe refcount it :)
00273     char* m_escapeBuffer; // can't really be static if we want to be thread-safe
00274     static const int s_escapeBufferLen = 10000;
00275 
00276     KoXmlWriter( const KoXmlWriter & ); // forbidden
00277     KoXmlWriter& operator=( const KoXmlWriter & ); // forbidden
00278 };
00279 
00280 #endif /* XMLWRITER_H */
00281 
KDE Home | KDE Accessibility Home | Description of Access Keys