]> git.mxchange.org Git - simgear.git/blob - simgear/xml/easyxml.hxx
Move FGEventMgr and FGSubsystemMgr over to SimGear, add SGEventMgr to FlightGear...
[simgear.git] / simgear / xml / easyxml.hxx
1 /**
2  * \file easyxml.hxx
3  * Declarations for the SimGear XML parser.
4  * Written by David Megginson, 2000-2001
5  * This file is in the Public Domain, and comes with NO WARRANTY of any kind.
6  */
7
8 #ifndef __EASYXML_HXX
9 #define __EASYXML_HXX
10
11 #include <simgear/compiler.h>
12 #include <simgear/structure/exception.hxx>
13
14 #include STL_IOSTREAM
15 #include STL_STRING
16 #include <vector>
17
18 SG_USING_STD(istream);
19 SG_USING_STD(string);
20 SG_USING_STD(vector);
21
22
23 /**
24  * Interface for XML attributes.
25  *
26  * This interface is used to provide a list of attributes to the
27  * application.  The interface is a pure abstract class so that
28  * different implementations can be substituted for the sake of
29  * efficiency.
30  *
31  * @see XMLAttributesDefault
32  */
33 class XMLAttributes
34 {
35 public:
36
37   /**
38    * Constructor.
39    */
40   XMLAttributes ();
41
42
43   /**
44    * Destructor.
45    */
46   virtual ~ XMLAttributes ();
47
48
49   /**
50    * Get the number of attributes present.
51    *
52    * @return The number of attributes in the list (may be 0).
53    */
54   virtual int size () const = 0;
55
56
57   /**
58    * Get the name of an attribute by index.
59    *
60    * The index must be less than the size of the list and greater
61    * than or equal to zero.
62    *
63    * @param i The index of the attribute (zero-based).
64    * @see #size
65    */
66   virtual const char * getName (int i) const = 0;
67
68
69   /**
70    * Get the string value of an attribute by index.
71    *
72    * The index must be less than the size of the list and greater
73    * than or equal to zero.
74    *
75    * @param i The index of the attribute (zero-based).
76    * @see #size
77    */
78   virtual const char * getValue (int i) const = 0;
79
80
81   /**
82    * Look up the index of an attribute by name.
83    *
84    * Attribute names must be unique.  This method will return
85    * an index that can be used with the {@link #getValue(const char *)}
86    * method if the attribute is found.
87    *
88    * @param name The name of the attribute.
89    * @return The index of the attribute with the name specified,
90    * or -1 if no such attribute is present in the list.
91    */
92   virtual int findAttribute (const char * name) const;
93
94
95   /**
96    * Test whether an attribute is present.
97    *
98    * @param name The name of the attribute.
99    * @return true if an attribute with the specified name is present
100    * in the attribute list, false otherwise.
101    */
102   virtual bool hasAttribute (const char * name) const;
103
104
105   /**
106    * Look up the value of an attribute by name.
107    *
108    * This method provides a convenient short-cut to invoking
109    * {@link #findAttribute} and {@link #getValue(const char *)}.
110    *
111    * @param name The name of the attribute to look up.
112    * @return The attribute's value as a string, or 0 if no
113    * attribute was found with the name specified.
114    */
115   virtual const char * getValue (const char * name) const;
116 };
117
118
119 /**
120  * Default mutable attributes implementation.
121  *
122  * This class provides a default implementation of the {@link
123  * XMLAttributes} interface.  The implementation is mutable, so
124  * that it is possible to modify the attribute list when necessary.
125  * This class is particularly useful for taking a snapshot of
126  * an attribute list during parsing.
127  *
128  * @see XMLAttributes
129  */
130 class XMLAttributesDefault : public XMLAttributes
131 {
132 public:
133
134   /**
135    * Default constructor.
136    */
137   XMLAttributesDefault ();
138
139
140   /**
141    * Copy constructor.
142    *
143    * This constructor is especially useful for taking a static
144    * snapshot of an attribute list for later use.
145    *
146    * @param atts The attribute list to copy.
147    */
148   XMLAttributesDefault (const XMLAttributes & atts);
149
150
151   /**
152    * Destructor.
153    */
154   virtual ~XMLAttributesDefault ();
155
156
157   /**
158    * Count the attributes in the list.
159    */
160   virtual int size () const;
161
162
163   /**
164    * Get the name of an attribute by index.
165    */
166   virtual const char * getName (int i) const;
167
168
169   /**
170    * Get the value of an attribute by index.
171    */
172   virtual const char * getValue (int i) const;
173
174
175   /**
176    * Add an attribute to an attribute list.
177    *
178    * The name is required to be unique in the list; the value is not.
179    *
180    * @param name The name of the attribute to add.
181    * @param value The value of the attribute to add.
182    */
183   virtual void addAttribute (const char * name, const char * value);
184
185
186   /**
187    * Set an attribute name by index.
188    *
189    * This method will not extend the list; the attribute must
190    * already exist.
191    *
192    * @param i The index of the attribute (zero-based).
193    * @param name The new name.
194    */
195   virtual void setName (int i, const char * name);
196
197
198   /**
199    * Set an attribute value by index.
200    *
201    * This method will not extend the list; the attribute must
202    * already exist.
203    *
204    * @param i The index of the attribute (zero-based).
205    * @param value The new value.
206    */
207   virtual void setValue (int i, const char * value);
208
209
210   /**
211    * Set an attribute value by name.
212    *
213    * This method will not extend the list; the attribute must
214    * already exist.
215    *
216    * @param name The name of the attribute that will have the new
217    * value.
218    * @param value The new value.
219    */
220   virtual void setValue (const char * name, const char * value);
221
222 private:
223   vector<string> _atts;
224 };
225
226
227 /**
228  * Visitor class for an XML document.
229  *
230  * This interface uses the Visitor pattern.  The XML parser walks
231  * through the XML document and invokes the appropriate method in
232  * this visitor for each piece of markup it finds.  By default,
233  * the methods do nothing; the application must subclass the visitor
234  * and override the methods for the events it's interested in.
235  * All applications are required to provide an implementation
236  * for the {@link #error} callback.
237  */
238 class XMLVisitor
239 {
240 public:
241
242   /**
243    * Virtual destructor.
244    */
245   virtual ~XMLVisitor () {}
246
247
248   /**
249    * Callback for the start of an XML document.
250    *
251    * The XML parser will invoke this method once, at the beginning of
252    * the XML document, before any other methods are invoked.  The
253    * application can use this callback to set up data structures,
254    * open files, etc.
255    *
256    * @see #endXML
257    */
258   virtual void startXML () {}
259
260
261   /**
262    * Callback for the end of an XML document.
263    *
264    * The XML parser will invoke this method once, at the end of the
265    * XML document, after all other methods are invoked, and only
266    * if there have been no parsing errors.  The application can use
267    * this callback to close or write files, finalize data structures,
268    * and so on, but the application will need to be prepared to 
269    * clean up any resources without this callback in the event of
270    * an error.
271    *
272    * @see #startXML
273    * @see #error
274    */
275   virtual void endXML () {}
276
277
278   /**
279    * Callback for the start of an XML element.
280    *
281    * The XML parser will invoke this method at the beginning of every
282    * XML element.  Start and end element calls will be balanced
283    * and properly nested: every element has both a start and end
284    * callback (even if it was specified with an XML empty element tag),
285    * there is exactly one root element, and every element must end 
286    * before its parent does.  Elements may not overlap.
287    * Note that the attribute list provided is volatile; it's contents
288    * are not guaranteed to persist after the end of the callback.
289    * If the application needs to keep a copy of the attribute list,
290    * it can make the copy with the {@link XMLAttributesDefault} class.
291    *
292    * @param name The name of the element that is starting (not null).
293    * @param atts The element's attributes (not null).
294    * @see #endElement
295    */
296   virtual void startElement (const char * name, const XMLAttributes &atts) {}
297
298
299   /**
300    * Callback for the end of an XML element.
301    *
302    * The XML parser will invoke this method at the end of every XML element.
303    *
304    * @param name The name of the element that is ending (not null).
305    * @see #startElement
306    */
307   virtual void endElement (const char * name) {}
308
309
310   /**
311    * Callback for a chunk of character data.
312    *
313    * The XML parser will invoke this method once for every chunk of
314    * character data in the XML document, including whitespace
315    * separating elements (as required by the XML recommendation).
316    * Note that character data may be chunked arbitrarily: the
317    * character data content of an element may be returned in one
318    * large chunk or several consecutive smaller chunks.
319    *
320    * @param s A pointer to the beginning of the character data (not null).
321    * @param length The number of characters in the chunk (may
322    * be zero).
323    */
324   virtual void data (const char * s, int length) {}
325
326
327   /**
328    * Callback for an XML processing instruction.
329    *
330    * The XML parser will invoke this method once for every processing
331    * instruction in the XML document.  Note that the XML declaration
332    * and the Text declaration are NOT PROCESSING INSTRUCTIONS and
333    * will not be reported through this callback.  Processing
334    * instructions are not all that useful, but the XML recommendation
335    * requires that they be reported.  Most applications can safely
336    * ignore this callback and use the empty default implementation.
337    *
338    * @param target The processing instruction target (not null).
339    * @param data The processing instruction data (not null).
340    */
341   virtual void pi (const char * target, const char * data) {}
342
343
344   /**
345    * Callback for an XML parsing warning.
346    *
347    * The XML parser will use this callback to report any non-fatal warnings
348    * during parsing.  It is the responsibility of the application to
349    * deal with the warning in some appropriate way.
350    *
351    * @param message The warning message from the parser.
352    * @param line The number of the line that generated the warning.
353    * @param column The character position in the line that generated
354    * the warning.
355    */
356   virtual void warning (const char * message, int line, int column) {}
357 };
358
359
360 /**
361  * @relates XMLVisitor
362  * Read an XML document.
363  *
364  * This function reads an XML document from the input stream provided,
365  * and invokes the callback methods in the visitor object to pass the 
366  * parsing events back to the application.  When this function
367  * returns, the parser will have reported all of the data in the XML
368  * document to the application through the visitor callback methods,
369  * and XML processing will be complete.
370  *
371  * @param input The byte input stream containing the XML document.
372  * @param visitor An object that contains callbacks for XML parsing
373  * events.
374  * @param path A string describing the original path of the resource.
375  * @exception Throws sg_io_exception or sg_xml_exception if there
376  * is a problem reading the file.
377  * @see XMLVisitor
378  */
379 extern void readXML (istream &input, XMLVisitor &visitor,
380                      const string &path="");
381
382
383 /**
384  * @relates XMLVisitor
385  * Read an XML document.
386  *
387  * This function reads an XML document from the input stream provided,
388  * and invokes the callback methods in the visitor object to pass the 
389  * parsing events back to the application.  When this function
390  * returns, the parser will have reported all of the data in the XML
391  * document to the application through the visitor callback methods,
392  * and XML processing will be complete.
393  *
394  * @param path The file name of the XML resource.
395  * @param visitor An object that contains callbacks for XML parsing
396  * events.
397  * @exception Throws sg_io_exception or sg_xml_exception if there
398  * is a problem reading the file.
399  * @see XMLVisitor
400  */
401 extern void readXML (const string &path, XMLVisitor &visitor);
402
403
404 #endif // __EASYXML_HXX
405