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>
12 #include <simgear/structure/exception.hxx>
18 SG_USING_STD(istream);
24 * Interface for XML attributes.
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
31 * @see XMLAttributesDefault
46 virtual ~ XMLAttributes ();
50 * Get the number of attributes present.
52 * @return The number of attributes in the list (may be 0).
54 virtual int size () const = 0;
58 * Get the name of an attribute by index.
60 * The index must be less than the size of the list and greater
61 * than or equal to zero.
63 * @param i The index of the attribute (zero-based).
66 virtual const char * getName (int i) const = 0;
70 * Get the string value of an attribute by index.
72 * The index must be less than the size of the list and greater
73 * than or equal to zero.
75 * @param i The index of the attribute (zero-based).
78 virtual const char * getValue (int i) const = 0;
82 * Look up the index of an attribute by name.
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.
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.
92 virtual int findAttribute (const char * name) const;
96 * Test whether an attribute is present.
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.
102 virtual bool hasAttribute (const char * name) const;
106 * Look up the value of an attribute by name.
108 * This method provides a convenient short-cut to invoking
109 * {@link #findAttribute} and {@link #getValue(const char *)}.
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.
115 virtual const char * getValue (const char * name) const;
120 * Default mutable attributes implementation.
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.
130 class XMLAttributesDefault : public XMLAttributes
135 * Default constructor.
137 XMLAttributesDefault ();
143 * This constructor is especially useful for taking a static
144 * snapshot of an attribute list for later use.
146 * @param atts The attribute list to copy.
148 XMLAttributesDefault (const XMLAttributes & atts);
154 virtual ~XMLAttributesDefault ();
158 * Count the attributes in the list.
160 virtual int size () const;
164 * Get the name of an attribute by index.
166 virtual const char * getName (int i) const;
170 * Get the value of an attribute by index.
172 virtual const char * getValue (int i) const;
176 * Add an attribute to an attribute list.
178 * The name is required to be unique in the list; the value is not.
180 * @param name The name of the attribute to add.
181 * @param value The value of the attribute to add.
183 virtual void addAttribute (const char * name, const char * value);
187 * Set an attribute name by index.
189 * This method will not extend the list; the attribute must
192 * @param i The index of the attribute (zero-based).
193 * @param name The new name.
195 virtual void setName (int i, const char * name);
199 * Set an attribute value by index.
201 * This method will not extend the list; the attribute must
204 * @param i The index of the attribute (zero-based).
205 * @param value The new value.
207 virtual void setValue (int i, const char * value);
211 * Set an attribute value by name.
213 * This method will not extend the list; the attribute must
216 * @param name The name of the attribute that will have the new
218 * @param value The new value.
220 virtual void setValue (const char * name, const char * value);
223 vector<string> _atts;
228 * Visitor class for an XML document.
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.
243 * Virtual destructor.
245 virtual ~XMLVisitor () {}
249 * Callback for the start of an XML document.
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,
258 virtual void startXML () {}
262 * Callback for the end of an XML document.
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
275 virtual void endXML () {}
279 * Callback for the start of an XML element.
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.
292 * @param name The name of the element that is starting (not null).
293 * @param atts The element's attributes (not null).
296 virtual void startElement (const char * name, const XMLAttributes &atts) {}
300 * Callback for the end of an XML element.
302 * The XML parser will invoke this method at the end of every XML element.
304 * @param name The name of the element that is ending (not null).
307 virtual void endElement (const char * name) {}
311 * Callback for a chunk of character data.
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.
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
324 virtual void data (const char * s, int length) {}
328 * Callback for an XML processing instruction.
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.
338 * @param target The processing instruction target (not null).
339 * @param data The processing instruction data (not null).
341 virtual void pi (const char * target, const char * data) {}
345 * Callback for an XML parsing warning.
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.
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
356 virtual void warning (const char * message, int line, int column) {}
361 * @relates XMLVisitor
362 * Read an XML document.
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.
371 * @param input The byte input stream containing the XML document.
372 * @param visitor An object that contains callbacks for XML parsing
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.
379 extern void readXML (istream &input, XMLVisitor &visitor,
380 const string &path="");
384 * @relates XMLVisitor
385 * Read an XML document.
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.
394 * @param path The file name of the XML resource.
395 * @param visitor An object that contains callbacks for XML parsing
397 * @exception Throws sg_io_exception or sg_xml_exception if there
398 * is a problem reading the file.
401 extern void readXML (const string &path, XMLVisitor &visitor);
405 * @relates XMLVisitor
406 * Read an XML document.
408 * This function reads an XML document from the buffer provided,
409 * and invokes the callback methods in the visitor object to pass the
410 * parsing events back to the application. When this function
411 * returns, the parser will have reported all of the data in the XML
412 * document to the application through the visitor callback methods,
413 * and XML processing will be complete.
415 * @param buf The xml data buffer.
416 * @param size The size of the data buffer in bytes
417 * @param visitor An object that contains callbacks for XML parsing
419 * @exception Throws sg_io_exception or sg_xml_exception if there
420 * is a problem reading the file.
423 extern void readXML (const char *buf, const int size, XMLVisitor &visitor);
426 #endif // __EASYXML_HXX