F:/KPlato/koffice/libs/store/KoXmlWriter.h

Aller à la documentation de ce fichier.
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>
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     // #### Maybe we want to subclass KoXmlWriter for manifest files.
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     // TODO addConfigItem for datetime and base64Binary
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     // writeCString is much faster than writeString.
00232     // Try to use it as much as possible, especially with constants.
00233     void writeString( const QString& str );
00234 
00235     // unused and possibly incorrect if length != size
00236     //inline void writeCString( const QCString& cstr ) {
00237     //    m_dev->write( cstr.data(), cstr.size() - 1 );
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; // maybe make it static, but then it needs a KStaticDeleter,
00265                           // and would eat 1K all the time... Maybe refcount it :)
00266     char* m_escapeBuffer; // can't really be static if we want to be thread-safe
00267     static const int s_escapeBufferLen = 10000;
00268 
00269     KoXmlWriter( const KoXmlWriter & ); // forbidden
00270     KoXmlWriter& operator=( const KoXmlWriter & ); // forbidden
00271 };
00272 
00273 #endif /* XMLWRITER_H */
00274 

Généré le Wed Nov 22 23:41:14 2006 pour KPlato par  doxygen 1.5.1-p1