X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fxml%2Feasyxml.hxx;h=7f0c916e2e20642a8988dd8ec22d3c5e979c4f5f;hb=819833a56030570718b9cc2aa1a52daa34631e18;hp=b042ab395c29cee48a730b4d95a3aaf66ce1c886;hpb=02d54dd18780728645be36a926322caf1c152485;p=simgear.git diff --git a/simgear/xml/easyxml.hxx b/simgear/xml/easyxml.hxx index b042ab39..7f0c916e 100644 --- a/simgear/xml/easyxml.hxx +++ b/simgear/xml/easyxml.hxx @@ -8,22 +8,14 @@ #ifndef __EASYXML_HXX #define __EASYXML_HXX -#ifdef HAVE_CONFIG_H -# include -#endif - #include +#include -#include STL_IOSTREAM -#include STL_STRING +#include +#include #include -#if !defined(SG_HAVE_NATIVE_SGI_COMPILERS) -SG_USING_STD(istream); -#endif -SG_USING_STD(string); -SG_USING_STD(vector); - +typedef struct XML_ParserStruct* XML_Parser; /** * Interface for XML attributes. @@ -225,7 +217,25 @@ public: virtual void setValue (const char * name, const char * value); private: - vector _atts; + std::vector _atts; +}; + +//////////////////////////////////////////////////////////////////////// +// Attribute list wrapper for Expat. +//////////////////////////////////////////////////////////////////////// + +class ExpatAtts : public XMLAttributes +{ +public: + ExpatAtts (const char ** atts) : _atts(atts) {} + + virtual int size () const; + virtual const char * getName (int i) const; + virtual const char * getValue (int i) const; + + virtual const char * getValue (const char * name) const; +private: + const char ** _atts; }; @@ -243,6 +253,14 @@ private: class XMLVisitor { public: + /// Constructor + XMLVisitor() : parser(0), line(-1), column(-1) {} + + /** + * Virtual destructor. + */ + virtual ~XMLVisitor () {} + /** * Callback for the start of an XML document. @@ -354,28 +372,85 @@ public: */ virtual void warning (const char * message, int line, int column) {} + /** Set the path to the file that is parsed. + * + * This method will be called to store the path to the parsed file. Note that + * the XML parser makes no use of this copy of the path. The intent is + * to be capable of refering to the path to the parsed file if needed. + * + * @param _path The path to the parsed file. + * @see #getPath + */ + void setPath(const std::string& _path) { path = _path; } + + /** Get the path to the parsed file. + * + * This method will be called if the application needs to access the path to + * the parsed file. This information is typically needed if an error is found + * so the file where it occurred can be retrieved to help the user locate the + * error. + * + * @return the path to the parsed file. + * @see #setPath + */ + const std::string& getPath(void) const { return path; } - /** - * Callback for a fatal XML parsing error. - * - * The XML parser will use this method to report any fatal errors - * during parsing. Once the first error callback is received, - * normal processing will stop, though additional errors may be - * reported. The application should take any appropriate - * error-handling procedures when it receives this callback, and - * should not attempt to use any of the data found in the XML - * document. - * - * @param message The error message from the parser. - * @param line The number of the line that generated the error. - * @param column The character position in the line that generated - * the error. + /** Save the current position in the parsed file. + * + * This method will be called to save the position at which the file is + * currently parsed. Note that the XML parser makes no use of that + * information. The intent is to be capable of refering to the position in + * the parsed file if needed. + * + * @see #getColumn + * @see #getLine */ - virtual void error (const char * message, int line, int column) = 0; + void savePosition(void); + + /** Get the saved column number in the parsed file. + * + * This method will be called if the application needs to get the column + * number that has been saved during the last call to savePosition(). This + * information is typically needed if an error is found so the position at + * which it occurred can be retrieved to help the user locate the error. + * + * @return the save column number. + * @see #savePosition + */ + int getColumn(void) const { return column; } + + /** Get the saved line number in the parsed file. + * + * This method will be called if the application needs to get the line + * number that has been saved during the last call to savePosition(). This + * information is typically needed if an error is found so the position at + * which it occurred can be retrieved to help the user locate the error. + * + * @return the save line number. + * @see #savePosition + */ + int getLine(void) const { return line; } + + /** Set the XML parser. + * + * This method will be called so the #XMLVisitor instance can internally use + * the XML parser for its housekeeping. The intent is that #XMLVisitor will + * only call the reporting functions of the XML parser and will not interfer + * with the XML parser current state. Doing otherwise will result in an + * unpredictable behavior of the XML parser. + * + * @param _parser the XML parser + */ + void setParser(XML_Parser _parser) { parser = _parser; } +private: + XML_Parser parser; + std::string path; + int line, column; }; /** + * @relates XMLVisitor * Read an XML document. * * This function reads an XML document from the input stream provided, @@ -388,11 +463,56 @@ public: * @param input The byte input stream containing the XML document. * @param visitor An object that contains callbacks for XML parsing * events. - * @return true if the parse succeeded, false if there was a fatal - * error. + * @param path A string describing the original path of the resource. + * @exception Throws sg_io_exception or sg_xml_exception if there + * is a problem reading the file. + * @see XMLVisitor + */ +extern void readXML (std::istream &input, XMLVisitor &visitor, + const std::string &path=""); + + +/** + * @relates XMLVisitor + * Read an XML document. + * + * This function reads an XML document from the input stream provided, + * and invokes the callback methods in the visitor object to pass the + * parsing events back to the application. When this function + * returns, the parser will have reported all of the data in the XML + * document to the application through the visitor callback methods, + * and XML processing will be complete. + * + * @param path The file name of the XML resource. + * @param visitor An object that contains callbacks for XML parsing + * events. + * @exception Throws sg_io_exception or sg_xml_exception if there + * is a problem reading the file. + * @see XMLVisitor + */ +extern void readXML (const std::string &path, XMLVisitor &visitor); + + +/** + * @relates XMLVisitor + * Read an XML document. + * + * This function reads an XML document from the buffer provided, + * and invokes the callback methods in the visitor object to pass the + * parsing events back to the application. When this function + * returns, the parser will have reported all of the data in the XML + * document to the application through the visitor callback methods, + * and XML processing will be complete. + * + * @param buf The xml data buffer. + * @param size The size of the data buffer in bytes + * @param visitor An object that contains callbacks for XML parsing + * events. + * @exception Throws sg_io_exception or sg_xml_exception if there + * is a problem reading the file. * @see XMLVisitor */ -extern bool readXML (istream &input, XMLVisitor &visitor); +extern void readXML (const char *buf, const int size, XMLVisitor &visitor); #endif // __EASYXML_HXX