2 * \file easyxml.cxx - implementation of EasyXML interfaces.
3 * Written by David Megginson, 2000-2001
4 * This file is in the Public Domain, and comes with NO WARRANTY of any kind.
8 # include <simgear_config.h>
11 #include <simgear/compiler.h>
13 #include <string.h> // strcmp()
15 #include "easyxml.hxx"
20 # include "xmlparse.h"
30 ////////////////////////////////////////////////////////////////////////
31 // Implementation of XMLAttributes.
32 ////////////////////////////////////////////////////////////////////////
34 XMLAttributes::XMLAttributes ()
38 XMLAttributes::~XMLAttributes ()
43 XMLAttributes::findAttribute (const char * name) const
46 for (int i = 0; i < s; i++) {
47 if (strcmp(name, getName(i)) == 0)
54 XMLAttributes::hasAttribute (const char * name) const
56 return (findAttribute(name) != -1);
60 XMLAttributes::getValue (const char * name) const
62 int pos = findAttribute(name);
70 ////////////////////////////////////////////////////////////////////////
71 // Implementation of XMLAttributesDefault.
72 ////////////////////////////////////////////////////////////////////////
74 XMLAttributesDefault::XMLAttributesDefault ()
78 XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
81 for (int i = 0; i < s; i++)
82 addAttribute(atts.getName(i), atts.getValue(i));
85 XMLAttributesDefault::~XMLAttributesDefault ()
90 XMLAttributesDefault::size () const
92 return _atts.size() / 2;
96 XMLAttributesDefault::getName (int i) const
98 return _atts[i*2].c_str();
102 XMLAttributesDefault::getValue (int i) const
104 return _atts[i*2+1].c_str();
108 XMLAttributesDefault::addAttribute (const char * name, const char * value)
110 _atts.push_back(name);
111 _atts.push_back(value);
115 XMLAttributesDefault::setName (int i, const char * name)
121 XMLAttributesDefault::setValue (int i, const char * name)
127 XMLAttributesDefault::setValue (const char * name, const char * value)
129 int pos = findAttribute(name);
132 setValue(pos, value);
134 addAttribute(name, value);
140 ////////////////////////////////////////////////////////////////////////
141 // Attribute list wrapper for Expat.
142 ////////////////////////////////////////////////////////////////////////
144 class ExpatAtts : public XMLAttributes
147 ExpatAtts (const char ** atts) : _atts(atts) {}
149 virtual int size () const;
150 virtual const char * getName (int i) const;
151 virtual const char * getValue (int i) const;
158 ExpatAtts::size () const
161 for (int i = 0; _atts[i] != 0; i += 2)
167 ExpatAtts::getName (int i) const
173 ExpatAtts::getValue (int i) const
180 ////////////////////////////////////////////////////////////////////////
181 // Static callback functions for Expat.
182 ////////////////////////////////////////////////////////////////////////
184 #define VISITOR (*((XMLVisitor *)userData))
187 start_element (void * userData, const char * name, const char ** atts)
189 VISITOR.startElement(name, ExpatAtts(atts));
193 end_element (void * userData, const char * name)
195 VISITOR.endElement(name);
199 character_data (void * userData, const char * s, int len)
201 VISITOR.data(s, len);
205 processing_instruction (void * userData,
209 VISITOR.pi(target, data);
216 ////////////////////////////////////////////////////////////////////////
217 // Implementation of XMLReader.
218 ////////////////////////////////////////////////////////////////////////
221 readXML (istream &input, XMLVisitor &visitor, const string &path)
223 XML_Parser parser = XML_ParserCreate(0);
224 XML_SetUserData(parser, &visitor);
225 XML_SetElementHandler(parser, start_element, end_element);
226 XML_SetCharacterDataHandler(parser, character_data);
227 XML_SetProcessingInstructionHandler(parser, processing_instruction);
232 while (!input.eof()) {
234 // FIXME: get proper error string from system
236 sg_io_exception ex ("Problem reading file",
238 XML_GetCurrentLineNumber(parser),
239 XML_GetCurrentColumnNumber(parser)),
240 "SimGear XML Parser");
241 XML_ParserFree(parser);
245 input.read(buf,16384);
246 if (!XML_Parse(parser, buf, input.gcount(), false)) {
247 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
249 XML_GetCurrentLineNumber(parser),
250 XML_GetCurrentColumnNumber(parser)),
251 "SimGear XML Parser");
252 XML_ParserFree(parser);
258 // Verify end of document.
259 if (!XML_Parse(parser, buf, 0, true)) {
260 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
262 XML_GetCurrentLineNumber(parser),
263 XML_GetCurrentColumnNumber(parser)),
264 "SimGear XML Parser");
265 XML_ParserFree(parser);
269 XML_ParserFree(parser);
274 readXML (const string &path, XMLVisitor &visitor)
276 ifstream input(path.c_str());
279 readXML(input, visitor, path);
280 } catch (sg_io_exception &) {
283 } catch (sg_throwable &) {
288 throw sg_io_exception("Failed to open file", sg_location(path),
289 "SimGear XML Parser");
295 readXML (const char *buf, const int size, XMLVisitor &visitor)
297 XML_Parser parser = XML_ParserCreate(0);
298 XML_SetUserData(parser, &visitor);
299 XML_SetElementHandler(parser, start_element, end_element);
300 XML_SetCharacterDataHandler(parser, character_data);
301 XML_SetProcessingInstructionHandler(parser, processing_instruction);
305 if (!XML_Parse(parser, buf, size, false)) {
306 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
307 sg_location("In-memory XML buffer",
308 XML_GetCurrentLineNumber(parser),
309 XML_GetCurrentColumnNumber(parser)),
310 "SimGear XML Parser");
311 XML_ParserFree(parser);
315 XML_ParserFree(parser);
319 // end of easyxml.cxx