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)
40 const char* name() const
44 const char* attribute(const char* name) const
48 for(const char** att = m_atts; *att != 0; att+=2)
50 if(strcmp(*att, name) == 0)
58 void forEachAttribute(XMLAttrVisitor& visitor) const
62 for(const char** att = m_atts; *att != 0; att+=2)
64 visitor.visit(*att, *(att+1));
80 FormattedVA(const char* format, va_list& m_arguments)
81 : m_format(format), m_arguments(m_arguments)
91 Formatted(const char* format, ...)
94 va_start(m_arguments, format);
104 #define vsnprintf std::vsnprintf
108 template<typename TextOutputStreamType>
109 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FormattedVA& formatted)
112 ostream.write(buffer, vsnprintf(buffer, 1023, formatted.m_format, formatted.m_arguments));
116 template<typename TextOutputStreamType>
117 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Formatted& formatted)
120 ostream.write(buffer, vsnprintf(buffer, 1023, formatted.m_format, formatted.m_arguments));
126 XMLImporter& m_importer;
129 static void startElement(void *user_data, const xmlChar *name, const xmlChar **atts)
131 SAXElement element(reinterpret_cast<const char*>(name), reinterpret_cast<const char**>(atts));
132 reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer.pushElement(element);
134 static void endElement(void *user_data, const xmlChar *name)
136 reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer.popElement(reinterpret_cast<const char*>(name));
138 static void characters(void *user_data, const xmlChar *ch, int len)
140 reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer
141 << StringRange(reinterpret_cast<const char*>(ch), reinterpret_cast<const char*>(ch + len));
144 static void warning(void *user_data, const char *msg, ...)
148 globalErrorStream() << "XML WARNING: " << FormattedVA(msg, args);
151 static void error(void *user_data, const char *msg, ...)
155 globalErrorStream() << "XML ERROR: " << FormattedVA(msg, args);
160 XMLSAXImporter(XMLImporter& importer) : m_importer(importer)
162 m_sax.internalSubset = 0;
163 m_sax.isStandalone = 0;
164 m_sax.hasInternalSubset = 0;
165 m_sax.hasExternalSubset = 0;
166 m_sax.resolveEntity = 0;
168 m_sax.entityDecl = 0;
169 m_sax.notationDecl = 0;
170 m_sax.attributeDecl = 0;
171 m_sax.elementDecl = 0;
172 m_sax.unparsedEntityDecl = 0;
173 m_sax.setDocumentLocator = 0;
174 m_sax.startDocument = 0;
175 m_sax.endDocument = 0;
176 m_sax.startElement = startElement;
177 m_sax.endElement = endElement;
179 m_sax.characters = characters;
180 m_sax.ignorableWhitespace = 0;
181 m_sax.processingInstruction = 0;
183 m_sax.warning = warning;
185 m_sax.fatalError = 0;
186 m_sax.getParameterEntity = 0;
187 m_sax.cdataBlock = 0;
188 m_sax.externalSubset = 0;
189 m_sax.initialized = 1;
192 xmlSAXHandler* callbacks()
202 class XMLStreamParser : public XMLExporter
204 enum unnamed0 { BUFSIZE = 1024 };
206 XMLStreamParser(TextInputStream& istream)
210 virtual void exportXML(XMLImporter& importer)
212 bool wellFormed = false;
215 std::size_t res = m_istream.read(chars, 4);
218 XMLSAXImporter sax(importer);
220 xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt(sax.callbacks(), sax.context(), chars, static_cast<int>(res), 0);
221 ctxt->replaceEntities = 1;
223 while ((res = m_istream.read(chars, BUFSIZE)) > 0)
225 xmlParseChunk(ctxt, chars, static_cast<int>(res), 0);
227 xmlParseChunk(ctxt, chars, 0, 1);
229 wellFormed = (ctxt->wellFormed == 1);
231 xmlFreeParserCtxt(ctxt);
237 TextInputStream& m_istream;