2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_XML_XMLWRITER_H )
23 #define INCLUDED_XML_XMLWRITER_H
29 class XMLEntityOutputStream
31 SingleCharacterOutputStream m_ostream;
33 XMLEntityOutputStream( TextOutputStream& ostream )
34 : m_ostream( ostream ){
36 void write( const char c ){
39 void writeEscaped( const char c ){
74 std::size_t write( const char* buffer, std::size_t length ){
75 const char*const end = buffer + length;
76 for ( const char* p = buffer; p != end; ++p )
85 inline XMLEntityOutputStream& operator<<( XMLEntityOutputStream& ostream, const T& t ){
86 return ostream_write( ostream, t );
90 class XMLStreamWriter : public XMLImporter, public XMLAttrVisitor
101 : m_state( eStartElement )
106 XMLEntityOutputStream m_ostream;
107 std::vector<state_t> m_elements;
109 void write_cdata( const char* buffer, std::size_t length ){
110 m_ostream << StringRange( buffer, buffer + length );
112 void write_string( const char* string ){
115 void write_quoted_string( const char* string ){
116 m_ostream.write( '"' );
118 m_ostream.write( '"' );
121 XMLStreamWriter( TextOutputStream& ostream )
122 : m_ostream( ostream ){
123 m_elements.push_back( state_t() );
124 m_elements.back().m_state = state_t::eContent;
125 m_ostream.write( '<' );
126 m_ostream.write( '?' );
127 write_string( "xml" );
128 visit( "version", "1.0" );
129 m_ostream.write( '?' );
130 m_ostream.write( '>' );
133 void pushElement( const XMLElement& element ){
134 if ( m_elements.back().m_state == state_t::eStartElement ) {
135 m_elements.back().m_state = state_t::eContent;
136 m_ostream.write( '>' );
139 m_elements.push_back( state_t() );
141 m_ostream.write( '<' );
142 write_string( element.name() );
143 element.forEachAttribute( *this );
145 void popElement( const char* name ){
146 if ( m_elements.back().m_state == state_t::eStartElement ) {
147 m_ostream.write( '/' );
148 m_ostream.write( '>' );
149 m_elements.pop_back();
153 m_ostream.write( '<' );
154 m_ostream.write( '/' );
155 write_string( name );
156 m_ostream.write( '>' );
157 m_elements.pop_back();
160 std::size_t write( const char* data, std::size_t length ){
161 if ( m_elements.back().m_state == state_t::eStartElement ) {
162 m_elements.back().m_state = state_t::eContent;
163 m_ostream.write( '>' );
165 write_cdata( data, length );
169 void visit( const char* name, const char* value ){
170 m_ostream.write( ' ' );
171 write_string( name );
172 m_ostream.write( '=' );
173 write_quoted_string( value );