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.
11 #include <simgear/compiler.h>
13 #include <simgear/misc/exception.hxx>
19 SG_USING_STD(istream);
25 * Interface for XML attributes.
27 * This interface is used to provide a list of attributes to the
28 * application. The interface is a pure abstract class so that
29 * different implementations can be substituted for the sake of
32 * @see XMLAttributesDefault
47 virtual ~ XMLAttributes ();
51 * Get the number of attributes present.
53 * @return The number of attributes in the list (may be 0).
55 virtual int size () const = 0;
59 * Get the name of an attribute by index.
61 * The index must be less than the size of the list and greater
62 * than or equal to zero.
64 * @param i The index of the attribute (zero-based).
67 virtual const char * getName (int i) const = 0;
71 * Get the string value of an attribute by index.
73 * The index must be less than the size of the list and greater
74 * than or equal to zero.
76 * @param i The index of the attribute (zero-based).
79 virtual const char * getValue (int i) const = 0;
83 * Look up the index of an attribute by name.
85 * Attribute names must be unique. This method will return
86 * an index that can be used with the {@link #getValue(const char *)}
87 * method if the attribute is found.
89 * @param name The name of the attribute.
90 * @return The index of the attribute with the name specified,
91 * or -1 if no such attribute is present in the list.
93 virtual int findAttribute (const char * name) const;
97 * Test whether an attribute is present.
99 * @param name The name of the attribute.
100 * @return true if an attribute with the specified name is present
101 * in the attribute list, false otherwise.
103 virtual bool hasAttribute (const char * name) const;
107 * Look up the value of an attribute by name.
109 * This method provides a convenient short-cut to invoking
110 * {@link #findAttribute} and {@link #getValue(const char *)}.
112 * @param name The name of the attribute to look up.
113 * @return The attribute's value as a string, or 0 if no
114 * attribute was found with the name specified.
116 virtual const char * getValue (const char * name) const;
121 * Default mutable attributes implementation.
123 * This class provides a default implementation of the {@link
124 * XMLAttributes} interface. The implementation is mutable, so
125 * that it is possible to modify the attribute list when necessary.
126 * This class is particularly useful for taking a snapshot of
127 * an attribute list during parsing.
131 class XMLAttributesDefault : public XMLAttributes
136 * Default constructor.
138 XMLAttributesDefault ();
144 * This constructor is especially useful for taking a static
145 * snapshot of an attribute list for later use.
147 * @param atts The attribute list to copy.
149 XMLAttributesDefault (const XMLAttributes & atts);
155 virtual ~XMLAttributesDefault ();
159 * Count the attributes in the list.
161 virtual int size () const;
165 * Get the name of an attribute by index.
167 virtual const char * getName (int i) const;
171 * Get the value of an attribute by index.
173 virtual const char * getValue (int i) const;
177 * Add an attribute to an attribute list.
179 * The name is required to be unique in the list; the value is not.
181 * @param name The name of the attribute to add.
182 * @param value The value of the attribute to add.
184 virtual void addAttribute (const char * name, const char * value);
188 * Set an attribute name by index.
190 * This method will not extend the list; the attribute must
193 * @param i The index of the attribute (zero-based).
194 * @param name The new name.
196 virtual void setName (int i, const char * name);
200 * Set an attribute value by index.
202 * This method will not extend the list; the attribute must
205 * @param i The index of the attribute (zero-based).
206 * @param value The new value.
208 virtual void setValue (int i, const char * value);
212 * Set an attribute value by name.
214 * This method will not extend the list; the attribute must
217 * @param name The name of the attribute that will have the new
219 * @param value The new value.
221 virtual void setValue (const char * name, const char * value);
224 vector<string> _atts;
229 * Visitor class for an XML document.
231 * This interface uses the Visitor pattern. The XML parser walks
232 * through the XML document and invokes the appropriate method in
233 * this visitor for each piece of markup it finds. By default,
234 * the methods do nothing; the application must subclass the visitor
235 * and override the methods for the events it's interested in.
236 * All applications are required to provide an implementation
237 * for the {@link #error} callback.
244 * Virtual destructor.
246 virtual ~XMLVisitor () {}
250 * Callback for the start of an XML document.
252 * The XML parser will invoke this method once, at the beginning of
253 * the XML document, before any other methods are invoked. The
254 * application can use this callback to set up data structures,
259 virtual void startXML () {}
263 * Callback for the end of an XML document.
265 * The XML parser will invoke this method once, at the end of the
266 * XML document, after all other methods are invoked, and only
267 * if there have been no parsing errors. The application can use
268 * this callback to close or write files, finalize data structures,
269 * and so on, but the application will need to be prepared to
270 * clean up any resources without this callback in the event of
276 virtual void endXML () {}
280 * Callback for the start of an XML element.
282 * The XML parser will invoke this method at the beginning of every
283 * XML element. Start and end element calls will be balanced
284 * and properly nested: every element has both a start and end
285 * callback (even if it was specified with an XML empty element tag),
286 * there is exactly one root element, and every element must end
287 * before its parent does. Elements may not overlap.
288 * Note that the attribute list provided is volatile; it's contents
289 * are not guaranteed to persist after the end of the callback.
290 * If the application needs to keep a copy of the attribute list,
291 * it can make the copy with the {@link XMLAttributesDefault} class.
293 * @param name The name of the element that is starting (not null).
294 * @param atts The element's attributes (not null).
297 virtual void startElement (const char * name, const XMLAttributes &atts) {}
301 * Callback for the end of an XML element.
303 * The XML parser will invoke this method at the end of every XML element.
305 * @param name The name of the element that is ending (not null).
308 virtual void endElement (const char * name) {}
312 * Callback for a chunk of character data.
314 * The XML parser will invoke this method once for every chunk of
315 * character data in the XML document, including whitespace
316 * separating elements (as required by the XML recommendation).
317 * Note that character data may be chunked arbitrarily: the
318 * character data content of an element may be returned in one
319 * large chunk or several consecutive smaller chunks.
321 * @param s A pointer to the beginning of the character data (not null).
322 * @param length The number of characters in the chunk (may
325 virtual void data (const char * s, int length) {}
329 * Callback for an XML processing instruction.
331 * The XML parser will invoke this method once for every processing
332 * instruction in the XML document. Note that the XML declaration
333 * and the Text declaration are NOT PROCESSING INSTRUCTIONS and
334 * will not be reported through this callback. Processing
335 * instructions are not all that useful, but the XML recommendation
336 * requires that they be reported. Most applications can safely
337 * ignore this callback and use the empty default implementation.
339 * @param target The processing instruction target (not null).
340 * @param data The processing instruction data (not null).
342 virtual void pi (const char * target, const char * data) {}
346 * Callback for an XML parsing warning.
348 * The XML parser will use this callback to report any non-fatal warnings
349 * during parsing. It is the responsibility of the application to
350 * deal with the warning in some appropriate way.
352 * @param message The warning message from the parser.
353 * @param line The number of the line that generated the warning.
354 * @param column The character position in the line that generated
357 virtual void warning (const char * message, int line, int column) {}
362 * @relates XMLVisitor
363 * Read an XML document.
365 * This function reads an XML document from the input stream provided,
366 * and invokes the callback methods in the visitor object to pass the
367 * parsing events back to the application. When this function
368 * returns, the parser will have reported all of the data in the XML
369 * document to the application through the visitor callback methods,
370 * and XML processing will be complete.
372 * @param input The byte input stream containing the XML document.
373 * @param visitor An object that contains callbacks for XML parsing
375 * @param path A string describing the original path of the resource.
376 * @exception Throws sg_io_exception or sg_xml_exception if there
377 * is a problem reading the file.
380 extern void readXML (istream &input, XMLVisitor &visitor,
381 const string &path="");
385 * @relates XMLVisitor
386 * Read an XML document.
388 * This function reads an XML document from the input stream provided,
389 * and invokes the callback methods in the visitor object to pass the
390 * parsing events back to the application. When this function
391 * returns, the parser will have reported all of the data in the XML
392 * document to the application through the visitor callback methods,
393 * and XML processing will be complete.
395 * @param path The file name of the XML resource.
396 * @param visitor An object that contains callbacks for XML parsing
398 * @exception Throws sg_io_exception or sg_xml_exception if there
399 * is a problem reading the file.
402 extern void readXML (const string &path, XMLVisitor &visitor);
405 #endif // __EASYXML_HXX