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)
37 void write(const char c)
41 void writeEscaped(const char c)
77 std::size_t write(const char* buffer, std::size_t length)
79 const char*const end = buffer + length;
80 for(const char* p = buffer; p != end; ++p)
89 inline XMLEntityOutputStream& operator<<(XMLEntityOutputStream& ostream, const T& t)
91 return ostream_write(ostream, t);
95 class XMLStreamWriter : public XMLImporter, public XMLAttrVisitor
106 : m_state(eStartElement)
111 XMLEntityOutputStream m_ostream;
112 std::vector<state_t> m_elements;
114 void write_cdata(const char* buffer, std::size_t length)
116 m_ostream << StringRange(buffer, buffer + length);
118 void write_string(const char* string)
122 void write_quoted_string(const char* string)
124 m_ostream.write('"');
126 m_ostream.write('"');
129 XMLStreamWriter(TextOutputStream& ostream)
132 m_elements.push_back(state_t());
133 m_elements.back().m_state = state_t::eContent;
134 m_ostream.write('<');
135 m_ostream.write('?');
137 visit("version", "1.0");
138 m_ostream.write('?');
139 m_ostream.write('>');
142 void pushElement(const XMLElement& element)
144 if(m_elements.back().m_state == state_t::eStartElement)
146 m_elements.back().m_state = state_t::eContent;
147 m_ostream.write('>');
150 m_elements.push_back(state_t());
152 m_ostream.write('<');
153 write_string(element.name());
154 element.forEachAttribute(*this);
156 void popElement(const char* name)
158 if(m_elements.back().m_state == state_t::eStartElement)
160 m_ostream.write('/');
161 m_ostream.write('>');
162 m_elements.pop_back();
166 m_ostream.write('<');
167 m_ostream.write('/');
169 m_ostream.write('>');
170 m_elements.pop_back();
173 std::size_t write(const char* data, std::size_t length)
175 if(m_elements.back().m_state == state_t::eStartElement)
177 m_elements.back().m_state = state_t::eContent;
178 m_ostream.write('>');
180 write_cdata(data, length);
184 void visit(const char* name, const char* value)
186 m_ostream.write(' ');
188 m_ostream.write('=');
189 write_quoted_string(value);