00001 // See ../../license.txt for license information. 00002 // 00003 // parse.hpp 00004 // 00005 // NOTES 00006 // XML Parser for the persistence framework. 00007 // 00008 // 30-Jun-2003 phamilton Created 00009 // 00010 00011 #ifndef incPERSIST_XML_PARSE 00012 #define incPERSIST_XML_PARSE 00013 00014 // forwards 00015 #include <string> 00016 #include <vector> 00017 #include "../config.hpp" 00018 00019 // from expat.h 00020 typedef struct XML_ParserStruct *XML_Parser; 00021 00022 #ifdef XML_UNICODE_WCHAR_T 00023 typedef wchar_t XML_Char; 00024 #define S(_S) L##_S 00025 typedef std::wstring xmlstring; 00026 #else 00027 typedef char XML_Char; 00028 typedef std::string xmlstring; 00029 #define S(_S) _S 00030 #endif 00031 00032 // error states for the parser. 00033 #define PARSE_SUCCESS 0 00034 #define PARSE_BADXMLTYPE 1 00035 #define PARSE_XMLERROR 2 00036 #define PARSE_NOFILE 3 00037 00038 namespace ph { 00039 namespace persist { 00040 namespace xml { 00041 00042 class PERSIST_DECL parse_progress 00043 { 00044 public: 00045 virtual ~parse_progress() {}; 00046 00047 virtual void total(long n) = 0; 00048 virtual void progress(long n) = 0; 00049 }; 00050 00051 class PERSIST_DECL parse 00052 /* 00053 Abstract class used to parse an XML file. 00054 */ 00055 { 00056 private: 00057 XML_Parser _parser; 00058 int _error; 00059 std::string _filename; 00060 std::vector<xmlstring> _elementstack; 00061 00062 static void sstartelement_handler(void *data, const XML_Char *name, const XML_Char **atts); 00063 static void sendelement_handler(void *data, const XML_Char *name); 00064 static void scdata_handler(void *data, const XML_Char *s, int len); 00065 static void scomment_handler(void *data, const XML_Char *data); // data is 0 terminated 00066 static void sdefault_handler(void *v, const XML_Char *s, int len); 00067 00068 public: 00069 parse(std::ostream *errorhandler = NULL, bool silent=false) : 00070 _parser(NULL), _error(PARSE_SUCCESS), _silent(silent), _errorhandler(errorhandler) {}; 00071 virtual ~parse() {}; 00072 00073 static bool parse_xml(std::istream *stream, const std::string &streampath, 00074 parse *parser, parse_progress *progress); 00075 // use this to parse a piece of xml with a parser. 00076 00077 void startparse(const std::string &streamname); 00078 int doparse(char *buf, long len, int done); 00079 void endparse(); 00080 // call these while parseing. 00081 00082 int parsestream(std::istream *stream, const std::string &streamname); 00083 int doparsefile(const std::string &filename); 00084 // wrappers to parse a sttream or disk file. They call 00085 // the above functions in the right order for a disk file. 00086 00087 virtual void startelement_handler(const xmlstring &name, const std::vector<xmlstring> &atts) = 0; 00088 virtual void endelement_handler(const xmlstring &name) = 0; 00089 virtual void cdata_handler(const xmlstring &s, int len) {}; 00090 virtual void comment_handler(const xmlstring &s) {}; 00091 virtual void default_handler(const xmlstring &s, int len) {}; 00092 // override to process XML elements. 00093 00094 virtual void finish_handler() {}; 00095 // override to handle the finish of the parse. 00096 00097 int error() { return _error; }; 00098 00099 static bool decodexmldata(const xmlstring &s, xmlstring *news); 00100 static bool encodexmldata(const xmlstring &s, xmlstring *news); 00101 00102 protected: 00103 bool _silent; 00104 std::ostream *_errorhandler; 00105 00106 xmlstring current(int top=0); 00107 // return the current element. 00108 00109 xmlstring element(int top); 00110 // return the particular element. 00111 00112 xmlstring expectedattr(const std::vector<xmlstring> &atts, const xmlstring &token); 00113 xmlstring attr(const std::vector<xmlstring> &atts, const xmlstring &token); 00114 xmlstring attr(const std::vector<xmlstring> &atts, int index); 00115 xmlstring attrval(const std::vector<xmlstring> &atts, int index); 00116 // fiddle with attributes. 00117 00118 void expected_error(const xmlstring &token); 00119 void error(const std::string &string, bool detail=true); 00120 void error(const std::string &format, const std::string &s1, bool detail=true); 00121 void error(const std::string &format, const std::string &s1, const std::string &s2, bool detail=true); 00122 // report errors. 00123 }; 00124 00125 }; // xml 00126 }; // persist 00127 }; // ph 00128 00129 #endif // incPERSIST_XML_PARSE