1 // easyxml.cxx - implementation of EasyXML interfaces.
7 ////////////////////////////////////////////////////////////////////////
8 // Implementation of XMLAttributes.
9 ////////////////////////////////////////////////////////////////////////
11 XMLAttributes::XMLAttributes ()
15 XMLAttributes::~XMLAttributes ()
20 XMLAttributes::findAttribute (const char * name) const
23 for (int i = 0; i < s; i++)
24 if (strcmp(name, getName(i)) == 0)
30 XMLAttributes::hasAttribute (const char * name) const
32 return (findAttribute(name) != -1);
36 XMLAttributes::getValue (const char * name) const
38 int pos = findAttribute(name);
46 ////////////////////////////////////////////////////////////////////////
47 // Implementation of XMLAttributesDefault.
48 ////////////////////////////////////////////////////////////////////////
50 XMLAttributesDefault::XMLAttributesDefault ()
54 XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
57 for (int i = 0; i < s; i++)
58 addAttribute(atts.getName(i), atts.getValue(i));
61 XMLAttributesDefault::~XMLAttributesDefault ()
66 XMLAttributesDefault::size () const
68 return _atts.size() / 2;
72 XMLAttributesDefault::getName (int i) const
74 return _atts[i*2].c_str();
78 XMLAttributesDefault::getValue (int i) const
80 return _atts[i*2+1].c_str();
84 XMLAttributesDefault::addAttribute (const char * name, const char * value)
86 _atts.push_back(name);
87 _atts.push_back(value);
91 XMLAttributesDefault::setName (int i, const char * name)
97 XMLAttributesDefault::setValue (int i, const char * name)
103 XMLAttributesDefault::setValue (const char * name, const char * value)
105 int pos = findAttribute(name);
108 setValue(pos, value);
110 addAttribute(name, value);
116 ////////////////////////////////////////////////////////////////////////
117 // Attribute list wrapper for Expat.
118 ////////////////////////////////////////////////////////////////////////
120 class ExpatAtts : public XMLAttributes
123 ExpatAtts (const char ** atts) : _atts(atts) {}
126 const char * getName (int i) const;
127 const char * getValue (int i) const;
134 ExpatAtts::size () const
137 for (int i = 0; _atts[i] != 0; i += 2)
143 ExpatAtts::getName (int i) const
149 ExpatAtts::getValue (int i) const
156 ////////////////////////////////////////////////////////////////////////
157 // Static callback functions for Expat.
158 ////////////////////////////////////////////////////////////////////////
160 #define VISITOR (*((XMLVisitor*)userData))
163 start_element (void * userData, const char * name, const char ** atts)
165 ExpatAtts attributes(atts);
166 VISITOR.startElement(name, attributes);
170 end_element (void * userData, const char * name)
172 VISITOR.endElement(name);
176 character_data (void * userData, const char * s, int len)
178 VISITOR.data(s, len);
182 processing_instruction (void * userData,
186 VISITOR.pi(target, data);
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of XMLReader.
195 ////////////////////////////////////////////////////////////////////////
198 readXML (istream &input, XMLVisitor &visitor)
202 XML_Parser parser = XML_ParserCreate(0);
203 XML_SetUserData(parser, &visitor);
204 XML_SetElementHandler(parser, start_element, end_element);
205 XML_SetCharacterDataHandler(parser, character_data);
206 XML_SetProcessingInstructionHandler(parser, processing_instruction);
211 while (!input.eof()) {
213 input.read(buf,16384);
214 // TODO: deal with bad stream.
215 if (!XML_Parse(parser, buf, input.gcount(), false)) {
216 visitor.error(XML_ErrorString(XML_GetErrorCode(parser)),
217 XML_GetCurrentColumnNumber(parser),
218 XML_GetCurrentLineNumber(parser));
226 XML_ParserFree(parser);
231 // end of easyxml.cxx