| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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: |
| 36 |
SAXElement( const char* name, const char** atts ) |
| 37 |
: m_name( name ), m_atts( atts ){ |
| 38 |
} |
| 39 |
const char* name() const { |
| 40 |
return m_name; |
| 41 |
} |
| 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 |
} |
| 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; |
| 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; |
| 83 |
Formatted( const char* format, ... ) |
| 84 |
: m_format( format ){ |
| 85 |
va_start( m_arguments, format ); |
| 86 |
} |
| 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> |
| 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 |
template<typename TextOutputStreamType> |
| 106 |
inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Formatted& formatted ){ |
| 107 |
char buffer[1024]; |
| 108 |
ostream.write( buffer, vsnprintf( buffer, 1023, formatted.m_format, formatted.m_arguments ) ); |
| 109 |
return ostream; |
| 110 |
} |
| 111 |
|
| 112 |
class XMLSAXImporter |
| 113 |
{ |
| 114 |
XMLImporter& m_importer; |
| 115 |
xmlSAXHandler m_sax; |
| 116 |
|
| 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 ); |
| 120 |
} |
| 121 |
static void endElement( void *user_data, const xmlChar *name ){ |
| 122 |
reinterpret_cast<XMLSAXImporter*>( user_data )->m_importer.popElement( reinterpret_cast<const char*>( name ) ); |
| 123 |
} |
| 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 ) ); |
| 127 |
} |
| 128 |
|
| 129 |
static void warning( void *user_data, const char *msg, ... ){ |
| 130 |
va_list args; |
| 131 |
va_start( args, msg ); |
| 132 |
globalErrorStream() << "XML WARNING: " << FormattedVA( msg, args ); |
| 133 |
va_end( args ); |
| 134 |
} |
| 135 |
static void error( void *user_data, const char *msg, ... ){ |
| 136 |
va_list args; |
| 137 |
va_start( args, msg ); |
| 138 |
globalErrorStream() << "XML ERROR: " << FormattedVA( msg, args ); |
| 139 |
va_end( args ); |
| 140 |
} |
| 141 |
|
| 142 |
public: |
| 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; |
| 149 |
m_sax.getEntity = 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; |
| 160 |
m_sax.reference = 0; |
| 161 |
m_sax.characters = characters; |
| 162 |
m_sax.ignorableWhitespace = 0; |
| 163 |
m_sax.processingInstruction = 0; |
| 164 |
m_sax.comment = 0; |
| 165 |
m_sax.warning = warning; |
| 166 |
m_sax.error = error; |
| 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; |
| 172 |
} |
| 173 |
|
| 174 |
xmlSAXHandler* callbacks(){ |
| 175 |
return &m_sax; |
| 176 |
} |
| 177 |
void* context(){ |
| 178 |
return this; |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
class XMLStreamParser : public XMLExporter |
| 183 |
{ |
| 184 |
enum unnamed0 { BUFSIZE = 1024 }; |
| 185 |
public: |
| 186 |
XMLStreamParser( TextInputStream& istream ) |
| 187 |
: m_istream( istream ){ |
| 188 |
} |
| 189 |
virtual void exportXML( XMLImporter& importer ){ |
| 190 |
bool wellFormed = false; |
| 191 |
|
| 192 |
char chars[BUFSIZE]; |
| 193 |
std::size_t res = m_istream.read( chars, 4 ); |
| 194 |
if ( res > 0 ) { |
| 195 |
XMLSAXImporter sax( importer ); |
| 196 |
|
| 197 |
xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt( sax.callbacks(), sax.context(), chars, static_cast<int>( res ), 0 ); |
| 198 |
ctxt->replaceEntities = 1; |
| 199 |
|
| 200 |
while ( ( res = m_istream.read( chars, BUFSIZE ) ) > 0 ) |
| 201 |
{ |
| 202 |
xmlParseChunk( ctxt, chars, static_cast<int>( res ), 0 ); |
| 203 |
} |
| 204 |
xmlParseChunk( ctxt, chars, 0, 1 ); |
| 205 |
|
| 206 |
wellFormed = ( ctxt->wellFormed == 1 ); |
| 207 |
|
| 208 |
xmlFreeParserCtxt( ctxt ); |
| 209 |
} |
| 210 |
|
| 211 |
(void) wellFormed; |
| 212 |
|
| 213 |
} |
| 214 |
private: |
| 215 |
TextInputStream& m_istream; |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#endif |