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 "sg_expat.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);
139 ////////////////////////////////////////////////////////////////////////
140 // Attribute list wrapper for Expat.
141 ////////////////////////////////////////////////////////////////////////
144 ExpatAtts::size () const
147 for (int i = 0; _atts[i] != 0; i += 2)
153 ExpatAtts::getName (int i) const
159 ExpatAtts::getValue (int i) const
165 ExpatAtts::getValue (const char * name) const
167 return XMLAttributes::getValue(name);
171 ////////////////////////////////////////////////////////////////////////
172 // Static callback functions for Expat.
173 ////////////////////////////////////////////////////////////////////////
175 #define VISITOR (*((XMLVisitor *)userData))
178 start_element (void * userData, const char * name, const char ** atts)
180 VISITOR.startElement(name, ExpatAtts(atts));
184 end_element (void * userData, const char * name)
186 VISITOR.endElement(name);
190 character_data (void * userData, const char * s, int len)
192 VISITOR.data(s, len);
196 processing_instruction (void * userData,
200 VISITOR.pi(target, data);
207 ////////////////////////////////////////////////////////////////////////
208 // Implementation of XMLReader.
209 ////////////////////////////////////////////////////////////////////////
212 readXML (istream &input, XMLVisitor &visitor, const string &path)
214 XML_Parser parser = XML_ParserCreate(0);
215 XML_SetUserData(parser, &visitor);
216 XML_SetElementHandler(parser, start_element, end_element);
217 XML_SetCharacterDataHandler(parser, character_data);
218 XML_SetProcessingInstructionHandler(parser, processing_instruction);
223 while (!input.eof()) {
225 // FIXME: get proper error string from system
227 sg_io_exception ex ("Problem reading file",
229 XML_GetCurrentLineNumber(parser),
230 XML_GetCurrentColumnNumber(parser)),
231 "SimGear XML Parser");
232 XML_ParserFree(parser);
236 input.read(buf,16384);
237 if (!XML_Parse(parser, buf, input.gcount(), false)) {
238 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
240 XML_GetCurrentLineNumber(parser),
241 XML_GetCurrentColumnNumber(parser)),
242 "SimGear XML Parser");
243 XML_ParserFree(parser);
249 // Verify end of document.
250 if (!XML_Parse(parser, buf, 0, true)) {
251 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
253 XML_GetCurrentLineNumber(parser),
254 XML_GetCurrentColumnNumber(parser)),
255 "SimGear XML Parser");
256 XML_ParserFree(parser);
260 XML_ParserFree(parser);
265 readXML (const string &path, XMLVisitor &visitor)
267 ifstream input(path.c_str());
270 readXML(input, visitor, path);
271 } catch (sg_io_exception &) {
274 } catch (sg_throwable &) {
279 throw sg_io_exception("Failed to open file", sg_location(path),
280 "SimGear XML Parser");
286 readXML (const char *buf, const int size, XMLVisitor &visitor)
288 XML_Parser parser = XML_ParserCreate(0);
289 XML_SetUserData(parser, &visitor);
290 XML_SetElementHandler(parser, start_element, end_element);
291 XML_SetCharacterDataHandler(parser, character_data);
292 XML_SetProcessingInstructionHandler(parser, processing_instruction);
296 if (!XML_Parse(parser, buf, size, false)) {
297 sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
298 sg_location("In-memory XML buffer",
299 XML_GetCurrentLineNumber(parser),
300 XML_GetCurrentColumnNumber(parser)),
301 "SimGear XML Parser");
302 XML_ParserFree(parser);
306 XML_ParserFree(parser);
310 // end of easyxml.cxx