00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef GXLIB_GXXMLABSTRACTPARSER_H
00019 #define GXLIB_GXXMLABSTRACTPARSER_H
00020
00021 #include "../../gx.h"
00022 #include "gxXmlElement.h"
00023 #include "gxXmlElementSpec.h"
00024 #include <expat/expat.h>
00025
00026
00037 class gxXmlAbstractParser
00038 {
00039 public:
00044 gxXmlAbstractParser( char * elemSpecs[] = NULL);
00045 virtual ~gxXmlAbstractParser();
00046
00047 int parse(const std::string & s, int isFinal = true);
00048 int parse(const char *s, int len, int isFinal = true);
00049
00053 void stop();
00054
00058 void error( const char * errorMessage, int errorCode = PARSE_APPERROR );
00059
00060 void clear();
00061
00062
00063
00064
00065
00066
00067
00068
00069 enum {
00070 PARSE_OK = XML_ERROR_NONE,
00071 PARSE_BADELEMENT = XML_ERROR_NONE + 400,
00072 PARSE_BADATTRIBUTE = XML_ERROR_NONE + 401,
00073 PARSE_CANTBEROOT = XML_ERROR_NONE + 402,
00074 PARSE_MUSTBEROOT = XML_ERROR_NONE + 403,
00075 PARSE_MISSINGATTRIBUTE = XML_ERROR_NONE + 404,
00076 PARSE_APPERROR = XML_ERROR_NONE + 500
00077 };
00078 int getLastError();
00079 std::string getLastErrorMessage();
00080 void setLastError( int error );
00081 void setLastErrorMessage( const char * message );
00082
00083 void addSpec( const char * spec );
00084 protected:
00085 typedef std::map<std::string, gxXmlElementSpec> element_specs_t;
00086 element_specs_t elementSpecs;
00087
00088 virtual int verifyElement();
00089
00090
00091 virtual void onElement() = 0;
00092
00093 gxXmlElement & getCurrentElement();
00094 bool hasParent();
00095 gxXmlElement & getParentElement();
00096 typedef std::list< gxXmlElement> context_t;
00097 context_t m_context;
00098
00099 private:
00100 int m_lastError;
00101 std::string m_lastErrorMessage;
00102
00103
00104 std::string m_lastErrorMessage2;
00105
00106 XML_Parser m_parser;
00107 static void XMLCALL startElement(void *userData, const char *name, const char **atts);
00108 static void XMLCALL endElement(void *userData, const char *name);
00109 static void XMLCALL characterData (void *userData, const XML_Char *s, int len);
00110
00111 void buildErrorMessage();
00112 const char * getXmlErrorName( int error );
00113 private:
00114 gxXmlAbstractParser( const gxXmlAbstractParser & rhs);
00115 gxXmlAbstractParser & operator=( const gxXmlAbstractParser & rhs);
00116 };
00117 #endif