gxXmlAbstractParser.h

Go to the documentation of this file.
00001 /*  Copyright (C) 2004  Guido Simone
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012 
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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    // Expat defines some XML_ERROR codes beginning at XML_ERROR_NONE.
00064    // Add our own error codes to handle the case where the XMl is valid
00065    // but it does not match the specs (invalid element name etc.).
00066    // An offset of 400 should be sufficient so there is no conflict
00067    // any time soon.  Also, the subclass may define its own error codes
00068    // so use an offset of 500 as the base for these error codes
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    // called when an element is complete
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    // additional info from element spec
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

Generated on Mon Nov 21 01:01:35 2005 for gxLib by  doxygen 1.4.5