lib/XmlWriter.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1992-2004 Trolltech AS. All Rights Reserved.
00003 
00004     This file ("Example Code"), is part of an example program for Qt,
00005     and is licensed under the Qt Commercial License Agreement,
00006     Agreement version 2.4 and higher as well as under the GPL and
00007     the QPL.
00008 
00009     The Example Code is licensed to you "as is" without warranty of
00010     any kind. To the maximum extent permitted by applicable law,
00011     Trolltech on behalf of itself and its suppliers, disclaims all
00012     warranties express or implied, including, but not limited to,
00013     implied warranties of merchantability and fitness for a particular
00014     purpose with regard to the Example Code. Trolltech does not
00015     warrant that the Example Code will satisfy your requirements or
00016     that it is without defect or error or that the operation thereof
00017     will be uninterrupted. All use of and reliance on the Example Code
00018     is at the sole risk of and responsibility of you and your
00019     customers.
00020 
00021     If you are a holder of a Qt Commercial License Agreement, you can
00022     use the code under that agreement or you can use it under GPL or
00023     QPL. If you are not a holder of a Qt Commercial License Agreement,
00024     you can use the code under the GPL or the QPL. Regardless of the
00025     applicable license agreement, the Example Code may be used,
00026     distributed and modified without limitation.
00027 */
00028 
00029 
00030 #ifndef XMLWRITER_H
00031 #define XMLWRITER_H
00032 
00033 #include <qmap.h>
00034 #include <qstring.h>
00035 #include <qtextstream.h>
00036 
00037 class AttrMap : public QMap<QString, QString>
00038 {
00039 public:
00040     AttrMap() { }
00041     AttrMap( const QString& name, const QString& value ) {
00042         insert( name, value );
00043     }
00044 };
00045 
00046 class XmlWriter
00047 {
00048 public:
00049     XmlWriter( QIODevice *device, QTextCodec *codec = 0 );
00050     ~XmlWriter();
00051 
00052     void writeRaw( const QString& xml );
00053     void writeString( const QString& string );
00054     void writeOpenTag( const QString& name, const AttrMap& attrs = AttrMap() );
00055     void writeCloseTag( const QString& name );
00056     void writeAtomTag( const QString& name, const AttrMap& attrs = AttrMap() );
00057     void writeTaggedString( const QString& name, const QString& string,
00058                             const AttrMap& attrs = AttrMap() );
00059     void newLine();
00060     void setIndentSize( int size ) { indentSize = size; }
00061     void setAutoNewLine( bool on ) { autoNewLine = on; }
00062 
00063 private:
00064     QString protect( const QString& string );
00065     QString opening( const QString& tag, const AttrMap& attrs = AttrMap() );
00066     void writePendingIndent();
00067 
00068     QTextStream out;
00069     QString indentStr;
00070     int indentSize;
00071     bool autoNewLine;
00072     bool atBeginningOfLine;
00073 };
00074 
00075 #endif