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)
31 XMLAttributes::hasAttribute (const char * name) const
33 return (findAttribute(name) != -1);
37 XMLAttributes::getValue (const char * name) const
39 int pos = findAttribute(name);
47 ////////////////////////////////////////////////////////////////////////
48 // Implementation of XMLAttributesDefault.
49 ////////////////////////////////////////////////////////////////////////
51 XMLAttributesDefault::XMLAttributesDefault ()
55 XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
58 for (int i = 0; i < s; i++)
59 addAttribute(atts.getName(i), atts.getValue(i));
62 XMLAttributesDefault::~XMLAttributesDefault ()
67 XMLAttributesDefault::size () const
69 return _atts.size() / 2;
73 XMLAttributesDefault::getName (int i) const
75 return _atts[i*2].c_str();
79 XMLAttributesDefault::getValue (int i) const
81 return _atts[i*2+1].c_str();
85 XMLAttributesDefault::addAttribute (const char * name, const char * value)
87 _atts.push_back(name);
88 _atts.push_back(value);
92 XMLAttributesDefault::setName (int i, const char * name)
98 XMLAttributesDefault::setValue (int i, const char * name)
104 XMLAttributesDefault::setValue (const char * name, const char * value)
106 int pos = findAttribute(name);
109 setValue(pos, value);
111 addAttribute(name, value);
117 ////////////////////////////////////////////////////////////////////////
118 // Attribute list wrapper for Expat.
119 ////////////////////////////////////////////////////////////////////////
121 class ExpatAtts : public XMLAttributes
124 ExpatAtts (const char ** atts) : _atts(atts) {}
126 virtual int size () const;
127 virtual const char * getName (int i) const;
128 virtual const char * getValue (int i) const;
135 ExpatAtts::size () const
138 for (int i = 0; _atts[i] != 0; i += 2)
144 ExpatAtts::getName (int i) const
150 ExpatAtts::getValue (int i) const
157 ////////////////////////////////////////////////////////////////////////
158 // Static callback functions for Expat.
159 ////////////////////////////////////////////////////////////////////////
161 #define VISITOR (*((XMLVisitor*)userData))
164 start_element (void * userData, const char * name, const char ** atts)
166 VISITOR.startElement(name, ExpatAtts(atts));
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