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_XMLPARSER_H )
23 #define INCLUDED_XML_XMLPARSER_H
28 #include "libxml/parser.h"
31 class TextInputStream;
33 class SAXElement : public XMLElement
36 SAXElement( const char* name, const char** atts )
37 : m_name( name ), m_atts( atts ){
39 const char* name() const {
42 const char* attribute( const char* name ) const {
44 for ( const char** att = m_atts; *att != 0; att += 2 )
46 if ( strcmp( *att, name ) == 0 ) {
53 void forEachAttribute( XMLAttrVisitor& visitor ) const {
55 for ( const char** att = m_atts; *att != 0; att += 2 )
57 visitor.visit( *att, *( att + 1 ) );
73 FormattedVA( const char* format, va_list& m_arguments )
74 : m_format( format ), m_arguments( m_arguments ){
83 Formatted( const char* format, ... )
85 va_start( m_arguments, format );
88 va_end( m_arguments );
94 #define vsnprintf std::vsnprintf
98 template<typename TextOutputStreamType>
99 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const FormattedVA& formatted ){
101 ostream.write( buffer, vsnprintf( buffer, 1023, formatted.m_format, formatted.m_arguments ) );
105 template<typename TextOutputStreamType>
106 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Formatted& formatted ){
108 ostream.write( buffer, vsnprintf( buffer, 1023, formatted.m_format, formatted.m_arguments ) );
114 XMLImporter& m_importer;
117 static void startElement( void *user_data, const xmlChar *name, const xmlChar **atts ){
118 SAXElement element( reinterpret_cast<const char*>( name ), reinterpret_cast<const char**>( atts ) );
119 reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer.pushElement( element );
121 static void endElement( void *user_data, const xmlChar *name ){
122 reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer.popElement( reinterpret_cast<const char*>( name ) );
124 static void characters( void *user_data, const xmlChar *ch, int len ){
125 reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer
126 << StringRange( reinterpret_cast<const char*>( ch ), reinterpret_cast<const char*>( ch + len ) );
129 static void warning( void *user_data, const char *msg, ... ){
131 va_start( args, msg );
132 globalErrorStream() << "XML WARNING: " << FormattedVA( msg, args );
135 static void error( void *user_data, const char *msg, ... ){
137 va_start( args, msg );
138 globalErrorStream() << "XML ERROR: " << FormattedVA( msg, args );
143 XMLSAXImporter( XMLImporter& importer ) : m_importer( importer ){
144 m_sax.internalSubset = 0;
145 m_sax.isStandalone = 0;
146 m_sax.hasInternalSubset = 0;
147 m_sax.hasExternalSubset = 0;
148 m_sax.resolveEntity = 0;
150 m_sax.entityDecl = 0;
151 m_sax.notationDecl = 0;
152 m_sax.attributeDecl = 0;
153 m_sax.elementDecl = 0;
154 m_sax.unparsedEntityDecl = 0;
155 m_sax.setDocumentLocator = 0;
156 m_sax.startDocument = 0;
157 m_sax.endDocument = 0;
158 m_sax.startElement = startElement;
159 m_sax.endElement = endElement;
161 m_sax.characters = characters;
162 m_sax.ignorableWhitespace = 0;
163 m_sax.processingInstruction = 0;
165 m_sax.warning = warning;
167 m_sax.fatalError = 0;
168 m_sax.getParameterEntity = 0;
169 m_sax.cdataBlock = 0;
170 m_sax.externalSubset = 0;
171 m_sax.initialized = 1;
174 xmlSAXHandler* callbacks(){
182 class XMLStreamParser : public XMLExporter
184 enum unnamed0 { BUFSIZE = 1024 };
186 XMLStreamParser( TextInputStream& istream )
187 : m_istream( istream ){
189 virtual void exportXML( XMLImporter& importer ){
190 bool wellFormed = false;
193 std::size_t res = m_istream.read( chars, 4 );
195 XMLSAXImporter sax( importer );
197 xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt( sax.callbacks(), sax.context(), chars, static_cast<int>( res ), 0 );
198 ctxt->replaceEntities = 1;
200 while ( ( res = m_istream.read( chars, BUFSIZE ) ) > 0 )
202 xmlParseChunk( ctxt, chars, static_cast<int>( res ), 0 );
204 xmlParseChunk( ctxt, chars, 0, 1 );
206 wellFormed = ( ctxt->wellFormed == 1 );
208 xmlFreeParserCtxt( ctxt );
214 TextInputStream& m_istream;