lib/XmlWriter.cpp

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 #include "XmlWriter.h"
00031 #include <qtextcodec.h>
00032 
00033 XmlWriter::XmlWriter( QIODevice *device, QTextCodec *codec )
00034     : indentSize( 4 ), autoNewLine( false ), atBeginningOfLine( true )
00035 {
00036     out.setDevice( device );
00037     if ( codec == 0 ) {
00038         out.setEncoding( QTextStream::UnicodeUTF8 );
00039     } else {
00040         out.setCodec( codec );
00041         out << "<?xml version=\"1.0\" encoding=\""
00042             << protect( codec->mimeName() ) << "\"?>\n";
00043     }
00044 }
00045 
00046 XmlWriter::~XmlWriter()
00047 {
00048     if ( autoNewLine && !atBeginningOfLine )
00049         out << endl;
00050 }
00051 
00052 QString XmlWriter::protect( const QString& string )
00053 {
00054     QString s = string;
00055     s.replace( "&", "&amp;" );
00056     s.replace( ">", "&gt;" );
00057     s.replace( "<", "&lt;" );
00058     s.replace( "\"", "&quot;" );
00059     s.replace( "\'", "&apos;" );
00060     return s;
00061 }
00062 
00063 QString XmlWriter::opening( const QString& tag, const AttrMap& attrs )
00064 {
00065     QString s = "<" + tag;
00066     AttrMap::ConstIterator a = attrs.begin();
00067     while ( a != attrs.end() ) {
00068         s += " " + a.key() + "=\"" + protect( *a ) + "\"";
00069         ++a;
00070     }
00071     s += ">";
00072     return s;
00073 }
00074 
00075 void XmlWriter::writePendingIndent()
00076 {
00077     if ( atBeginningOfLine ) {
00078         out << indentStr;
00079         atBeginningOfLine = false;
00080     }
00081 }
00082 
00083 void XmlWriter::newLine()
00084 {
00085     out << endl;
00086     atBeginningOfLine = true;
00087 }
00088 
00089 void XmlWriter::writeRaw( const QString& xml )
00090 {
00091     out << xml;
00092     atBeginningOfLine = false;
00093 }
00094 
00095 void XmlWriter::writeString( const QString& string )
00096 {
00097     out << protect( string );
00098     atBeginningOfLine = false;
00099 }
00100 
00101 void XmlWriter::writeOpenTag( const QString& name, const AttrMap& attrs )
00102 {
00103     writePendingIndent();
00104     out << opening( name, attrs );
00105     indentStr += QString().fill( ' ', indentSize );
00106     if ( autoNewLine )
00107         newLine();
00108 }
00109 
00110 void XmlWriter::writeCloseTag( const QString& name )
00111 {
00112     indentStr = indentStr.mid( indentSize );
00113     writePendingIndent();
00114     out << opening( "/" + name );
00115     if ( autoNewLine )
00116         newLine();
00117 }
00118 
00119 void XmlWriter::writeAtomTag( const QString& name, const AttrMap& attrs )
00120 {
00121     writePendingIndent();
00122     QString atom = opening( name, attrs );
00123     atom.insert( atom.length() - 1, "/" );
00124     out << atom;
00125     if ( autoNewLine )
00126         newLine();
00127 }
00128 
00129 void XmlWriter::writeTaggedString( const QString& name, const QString& string,
00130                                    const AttrMap& attrs )
00131 {
00132     writePendingIndent();
00133     out << opening( name, attrs );
00134     writeString( string );
00135     out << opening( "/" + name );
00136     if ( autoNewLine )
00137         newLine();
00138 }