]> git.mxchange.org Git - simgear.git/blob - simgear/xml/easyxml.cxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / xml / easyxml.cxx
1 /**
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.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #  include <simgear_config.h>
9 #endif
10      
11 #include <simgear/compiler.h>
12
13 #include <string.h>             // strcmp()
14
15 #include "easyxml.hxx"
16      
17 #ifdef SYSTEM_EXPAT
18 #  include <expat.h>
19 #else
20 #  include "sg_expat.h"     
21 #endif
22      
23 #include <fstream>
24 #include <iostream>
25
26 using std::ifstream;
27
28
29 \f
30 ////////////////////////////////////////////////////////////////////////
31 // Implementation of XMLAttributes.
32 ////////////////////////////////////////////////////////////////////////
33
34 XMLAttributes::XMLAttributes ()
35 {
36 }
37
38 XMLAttributes::~XMLAttributes ()
39 {
40 }
41
42 int
43 XMLAttributes::findAttribute (const char * name) const
44 {
45   int s = size();
46   for (int i = 0; i < s; i++) {
47     if (strcmp(name, getName(i)) == 0)
48       return i;
49   }
50   return -1;
51 }
52
53 bool
54 XMLAttributes::hasAttribute (const char * name) const
55 {
56   return (findAttribute(name) != -1);
57 }
58
59 const char *
60 XMLAttributes::getValue (const char * name) const
61 {
62   int pos = findAttribute(name);
63   if (pos >= 0)
64     return getValue(pos);
65   else
66     return 0;
67 }
68
69 \f
70 ////////////////////////////////////////////////////////////////////////
71 // Implementation of XMLAttributesDefault.
72 ////////////////////////////////////////////////////////////////////////
73
74 XMLAttributesDefault::XMLAttributesDefault ()
75 {
76 }
77
78 XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
79 {
80   int s = atts.size();
81   for (int i = 0; i < s; i++)
82     addAttribute(atts.getName(i), atts.getValue(i));
83 }
84
85 XMLAttributesDefault::~XMLAttributesDefault ()
86 {
87 }
88
89 int
90 XMLAttributesDefault::size () const
91 {
92   return _atts.size() / 2;
93 }
94
95 const char *
96 XMLAttributesDefault::getName (int i) const
97 {
98   return _atts[i*2].c_str();
99 }
100
101 const char *
102 XMLAttributesDefault::getValue (int i) const
103 {
104   return _atts[i*2+1].c_str();
105 }
106
107 void
108 XMLAttributesDefault::addAttribute (const char * name, const char * value)
109 {
110   _atts.push_back(name);
111   _atts.push_back(value);
112 }
113
114 void
115 XMLAttributesDefault::setName (int i, const char * name)
116 {
117   _atts[i*2] = name;
118 }
119
120 void
121 XMLAttributesDefault::setValue (int i, const char * name)
122 {
123   _atts[i*2+1] = name;
124 }
125
126 void
127 XMLAttributesDefault::setValue (const char * name, const char * value)
128 {
129   int pos = findAttribute(name);
130   if (pos >= 0) {
131     setName(pos, name);
132     setValue(pos, value);
133   } else {
134     addAttribute(name, value);
135   }
136 }
137
138
139 ////////////////////////////////////////////////////////////////////////
140 // Attribute list wrapper for Expat.
141 ////////////////////////////////////////////////////////////////////////
142
143 int
144 ExpatAtts::size () const
145 {
146   int s = 0;
147   for (int i = 0; _atts[i] != 0; i += 2)
148     s++;
149   return s;
150 }
151
152 const char *
153 ExpatAtts::getName (int i) const
154 {
155   return _atts[i*2];
156 }
157
158 const char *
159 ExpatAtts::getValue (int i) const
160 {
161   return _atts[i*2+1];
162 }
163
164 const char * 
165 ExpatAtts::getValue (const char * name) const
166 {
167   return XMLAttributes::getValue(name);
168 }
169
170 \f
171 ////////////////////////////////////////////////////////////////////////
172 // Static callback functions for Expat.
173 ////////////////////////////////////////////////////////////////////////
174
175 #define VISITOR (*((XMLVisitor *)userData))
176
177 static void
178 start_element (void * userData, const char * name, const char ** atts)
179 {
180   VISITOR.startElement(name, ExpatAtts(atts));
181 }
182
183 static void
184 end_element (void * userData, const char * name)
185 {
186   VISITOR.endElement(name);
187 }
188
189 static void
190 character_data (void * userData, const char * s, int len)
191 {
192   VISITOR.data(s, len);
193 }
194
195 static void
196 processing_instruction (void * userData,
197                         const char * target,
198                         const char * data)
199 {
200   VISITOR.pi(target, data);
201 }
202
203 #undef VISITOR
204
205
206 \f
207 ////////////////////////////////////////////////////////////////////////
208 // Implementation of XMLReader.
209 ////////////////////////////////////////////////////////////////////////
210
211 void
212 readXML (istream &input, XMLVisitor &visitor, const string &path)
213 {
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);
219
220   visitor.startXML();
221
222   char buf[16384];
223   while (!input.eof()) {
224
225                                 // FIXME: get proper error string from system
226     if (!input.good()) {
227       sg_io_exception ex ("Problem reading file",
228                             sg_location(path,
229                                         XML_GetCurrentLineNumber(parser),
230                                         XML_GetCurrentColumnNumber(parser)),
231                             "SimGear XML Parser");
232       XML_ParserFree(parser);
233       throw ex;
234     }
235
236     input.read(buf,16384);
237     if (!XML_Parse(parser, buf, input.gcount(), false)) {
238       sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
239                             sg_location(path,
240                                         XML_GetCurrentLineNumber(parser),
241                                         XML_GetCurrentColumnNumber(parser)),
242                             "SimGear XML Parser");
243       XML_ParserFree(parser);
244       throw ex;
245     }
246
247   }
248
249                                 // Verify end of document.
250   if (!XML_Parse(parser, buf, 0, true)) {
251     sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)),
252                           sg_location(path,
253                                       XML_GetCurrentLineNumber(parser),
254                                       XML_GetCurrentColumnNumber(parser)),
255                           "SimGear XML Parser");
256     XML_ParserFree(parser);
257     throw ex;
258   }
259
260   XML_ParserFree(parser);
261   visitor.endXML();
262 }
263
264 void
265 readXML (const string &path, XMLVisitor &visitor)
266 {
267   ifstream input(path.c_str());
268   if (input.good()) {
269     try {
270       readXML(input, visitor, path);
271     } catch (sg_io_exception &) {
272       input.close();
273       throw;
274     } catch (sg_throwable &) {
275       input.close();
276       throw;
277     }
278   } else {
279     throw sg_io_exception("Failed to open file", sg_location(path),
280                           "SimGear XML Parser");
281   }
282   input.close();
283 }
284
285 void
286 readXML (const char *buf, const int size, XMLVisitor &visitor)
287 {
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);
293
294   visitor.startXML();
295
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);
303       throw ex;
304   }
305
306   XML_ParserFree(parser);
307   visitor.endXML();
308 }
309
310 // end of easyxml.cxx