1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4 
5    This file is part of GtkRadiant.
6 
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.
11 
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.
16 
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
20  */
21 
22 #if !defined( INCLUDED_XML_XMLPARSER_H )
23 #define INCLUDED_XML_XMLPARSER_H
24 
25 #include <cstdio>
26 #include <string.h>
27 #include "ixml.h"
28 #include "libxml/parser.h"
29 #include "convert.h"
30 
31 class TextInputStream;
32 
33 class SAXElement : public XMLElement
34 {
35 public:
SAXElement(const char * name,const char ** atts)36 SAXElement( const char* name, const char** atts )
37 	: m_name( name ), m_atts( atts ){
38 }
name()39 const char* name() const {
40 	return m_name;
41 }
attribute(const char * name)42 const char* attribute( const char* name ) const {
43 	if ( m_atts != 0 ) {
44 		for ( const char** att = m_atts; *att != 0; att += 2 )
45 		{
46 			if ( strcmp( *att, name ) == 0 ) {
47 				return *( ++att );
48 			}
49 		}
50 	}
51 	return "";
52 }
forEachAttribute(XMLAttrVisitor & visitor)53 void forEachAttribute( XMLAttrVisitor& visitor ) const {
54 	if ( m_atts != 0 ) {
55 		for ( const char** att = m_atts; *att != 0; att += 2 )
56 		{
57 			visitor.visit( *att, *( att + 1 ) );
58 		}
59 	}
60 }
61 private:
62 const char* m_name;
63 const char** m_atts;
64 };
65 
66 #include <stdarg.h>
67 
68 class FormattedVA
69 {
70 public:
71 const char* m_format;
72 va_list& m_arguments;
FormattedVA(const char * format,va_list & m_arguments)73 FormattedVA( const char* format, va_list& m_arguments )
74 	: m_format( format ), m_arguments( m_arguments ){
75 }
76 };
77 
78 class Formatted
79 {
80 public:
81 const char* m_format;
82 mutable va_list m_arguments;
Formatted(const char * format,...)83 Formatted( const char* format, ... )
84 	: m_format( format ){
85 	va_start( m_arguments, format );
86 }
~Formatted()87 ~Formatted(){
88 	va_end( m_arguments );
89 }
90 };
91 
92 #ifdef _MSC_VER
93 #if _MSC_VER < 1400
94 #define vsnprintf std::vsnprintf
95 #endif
96 #endif
97 
98 template<typename TextOutputStreamType>
ostream_write(TextOutputStreamType & ostream,const FormattedVA & formatted)99 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const FormattedVA& formatted ){
100 	char buffer[1024];
101 	ostream.write( buffer, vsnprintf( buffer, 1023, formatted.m_format, formatted.m_arguments ) );
102 	return ostream;
103 }
104 
105 #if 0
106 template<typename TextOutputStreamType>
107 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Formatted& formatted ){
108 	char buffer[1024];
109 	ostream.write( buffer, vsnprintf( buffer, 1023, formatted.m_format, formatted.m_arguments ) );
110 	return ostream;
111 }
112 #endif
113 
114 class XMLSAXImporter
115 {
116 XMLImporter& m_importer;
117 xmlSAXHandler m_sax;
118 
startElement(void * user_data,const xmlChar * name,const xmlChar ** atts)119 static void startElement( void *user_data, const xmlChar *name, const xmlChar **atts ){
120 	SAXElement element( reinterpret_cast<const char*>( name ), reinterpret_cast<const char**>( atts ) );
121 	reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer.pushElement( element );
122 }
endElement(void * user_data,const xmlChar * name)123 static void endElement( void *user_data, const xmlChar *name ){
124 	reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer.popElement( reinterpret_cast<const char*>( name ) );
125 }
characters(void * user_data,const xmlChar * ch,int len)126 static void characters( void *user_data, const xmlChar *ch, int len ){
127 	reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer
128 	<< StringRange( reinterpret_cast<const char*>( ch ), reinterpret_cast<const char*>( ch + len ) );
129 }
130 
warning(void * user_data,const char * msg,...)131 static void warning( void *user_data, const char *msg, ... ){
132 	va_list args;
133 	va_start( args, msg );
134 	globalErrorStream() << "XML WARNING: " << FormattedVA( msg, args );
135 	va_end( args );
136 }
error(void * user_data,const char * msg,...)137 static void error( void *user_data, const char *msg, ... ){
138 	va_list args;
139 	va_start( args, msg );
140 	globalErrorStream() << "XML ERROR: " << FormattedVA( msg, args );
141 	va_end( args );
142 }
143 
144 public:
XMLSAXImporter(XMLImporter & importer)145 XMLSAXImporter( XMLImporter& importer ) : m_importer( importer ){
146 	m_sax.internalSubset = 0;
147 	m_sax.isStandalone = 0;
148 	m_sax.hasInternalSubset = 0;
149 	m_sax.hasExternalSubset = 0;
150 	m_sax.resolveEntity = 0;
151 	m_sax.getEntity = 0;
152 	m_sax.entityDecl = 0;
153 	m_sax.notationDecl = 0;
154 	m_sax.attributeDecl = 0;
155 	m_sax.elementDecl = 0;
156 	m_sax.unparsedEntityDecl = 0;
157 	m_sax.setDocumentLocator = 0;
158 	m_sax.startDocument = 0;
159 	m_sax.endDocument = 0;
160 	m_sax.startElement = startElement;
161 	m_sax.endElement = endElement;
162 	m_sax.reference = 0;
163 	m_sax.characters = characters;
164 	m_sax.ignorableWhitespace = 0;
165 	m_sax.processingInstruction = 0;
166 	m_sax.comment = 0;
167 	m_sax.warning = warning;
168 	m_sax.error = error;
169 	m_sax.fatalError = 0;
170 	m_sax.getParameterEntity = 0;
171 	m_sax.cdataBlock = 0;
172 	m_sax.externalSubset = 0;
173 	m_sax.initialized = 1;
174 }
175 
callbacks()176 xmlSAXHandler* callbacks(){
177 	return &m_sax;
178 }
context()179 void* context(){
180 	return this;
181 }
182 };
183 
184 class XMLStreamParser : public XMLExporter
185 {
186 enum unnamed0 { BUFSIZE = 1024 };
187 public:
XMLStreamParser(TextInputStream & istream)188 XMLStreamParser( TextInputStream& istream )
189 	: m_istream( istream ){
190 }
exportXML(XMLImporter & importer)191 virtual void exportXML( XMLImporter& importer ){
192 	bool wellFormed = false;
193 
194 	char chars[BUFSIZE];
195 	std::size_t res = m_istream.read( chars, 4 );
196 	if ( res > 0 ) {
197 		XMLSAXImporter sax( importer );
198 
199 		xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt( sax.callbacks(), sax.context(), chars, static_cast<int>( res ), 0 );
200 		ctxt->replaceEntities = 1;
201 
202 		while ( ( res = m_istream.read( chars, BUFSIZE ) ) > 0 )
203 		{
204 			xmlParseChunk( ctxt, chars, static_cast<int>( res ), 0 );
205 		}
206 		xmlParseChunk( ctxt, chars, 0, 1 );
207 
208 		wellFormed = ( ctxt->wellFormed == 1 );
209 
210 		xmlFreeParserCtxt( ctxt );
211 	}
212 
213 	//return wellFormed;
214 }
215 private:
216 TextInputStream& m_istream;
217 };
218 
219 
220 
221 #endif
222