src/tools/tXmlParser.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2005  by 
00007 and the AA DevTeam (see the file AUTHORS(.txt) in the main source directory)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024   
00025 ***************************************************************************
00026 
00027 */
00028 
00029 // The base class for xml parsers
00030 
00031 #ifndef ArmageTron_XMLPARSER_H
00032 #define ArmageTron_XMLPARSER_H
00033 
00034 #include "defs.h"
00035 #include <libxml/parser.h>
00036 #include <libxml/tree.h>
00037 
00038 #include "tString.h"
00039 #include "tError.h"
00040 #include "tConsole.h"
00041 #include "tResourceManager.h"
00042 
00043 #include <map>
00044 #include <typeinfo>
00045 
00046 namespace tXmlParserNamespace {
00047 
00048 typedef std::map<tString, tString> tAttributeList;
00049 enum { SAX, DOM };
00050 
00052 class tXmlParser {
00053 public:
00055     class node {
00056         typedef char CHAR; 
00057         xmlNode *m_cur;  
00058     public:
00059         node(xmlNode *cur); 
00060         node() : m_cur(0) {} 
00061         bool IsOfType(CHAR const *name) const; 
00062         tString GetName(void) const; 
00063         bool HasProp(CHAR const *prop) const; 
00064         tString GetProp(CHAR const *prop) const; 
00065         bool GetPropBool(CHAR const *prop) const; 
00066 #ifndef _MSC_VER
00067         template<typename T> void GetProp(CHAR const *prop, T &target) const; 
00068 #else
00069         void GetProp(CHAR const *prop, REAL &target) const;
00070         void GetProp(CHAR const *prop, bool &target) const;
00071         void GetProp(CHAR const *prop, int &target) const;
00072         void GetProp(CHAR const *prop, tString &target) const;
00073 #endif
00074 
00075         bool IsOfType(tString const &name) const { return IsOfType(name.c_str());} 
00076         bool HasProp(tString const &prop) const { return HasProp(prop.c_str()); } 
00077         tString GetProp(tString const &prop) const { return GetProp(prop.c_str()); } 
00078         bool GetPropBool(tString const &prop) const { return GetPropBool(prop.c_str()); } 
00079         template<typename T> void GetProp(tString const &prop, T &target) const {GetProp(prop.c_str(), target);} 
00080 
00081         node &operator++(); 
00082         node const operator++(int); 
00083 
00084         operator bool () const; 
00085 
00086         node GetFirstChild(void) const; 
00087     };
00088 
00089     node GetRoot();
00090 
00091     xmlDocPtr m_Doc; /* The xml document */
00092 
00093     // If DOM isn't what you want, make sure you override this accordingly
00094     tXmlParser() : m_Doc(0) { SetMode(DOM); };
00095     virtual ~tXmlParser();
00096 
00097     // Provided just in case you need it, probably will never be used
00098     tXmlParser(int mode) { SetMode(mode); m_Doc = 0; };
00099 
00100     void SetMode(int mode) { m_Mode = mode; };
00101 
00102     virtual bool LoadFile(const char* filename, const char *uri="");
00103 
00104     // Loads and parses, which is usually what you want
00105     bool LoadXmlFile(const char* filename, const char* uri="");
00106 
00107     // Loads without parsing, used if you need special parameters in your parse function
00108     bool LoadWithoutParsing(const char* filename, const char* uri="");
00109     bool LoadWithParsing(const char* filename, const char* uri="");
00110 
00111     // This is a generic parse.  If you're in DOM mode, it will call your ParseDom
00112     // method, if you're in SAX mode, it will start the sax parser.
00113     // If you need to parse with special parameters, you need to make your own method
00114     // to do it.
00115     bool Parse();
00116 
00117     // The default implementation does nothing, override this to provide your own
00118     // dom walker or whatever.  Return true on success, false on failure
00119     virtual bool ParseDom();
00120 
00121     // Starts the sax parser.  You should override the callbacks listed below to use
00122     // this and call the constructor to initialize with sax mode
00123     bool ParseSax();
00124 
00125     // todo: add arguments to these methods
00126     virtual void startElement(tString &element, tAttributeList &attributes);
00127     virtual void endElement(tString &element);
00128     virtual void startDocument();
00129     virtual void endDocument();
00130 
00131     // internal callbacks, used to bridge libxml2's sax data to armagetron's internal
00132     // data types.  subclasses can safely ignore these
00133     void cb_startDocument();
00134     void cb_endDocument();
00135     void cb_startElement(const xmlChar* name, const xmlChar** attrs);
00136     void cb_endElement(const xmlChar *name);
00137     tString m_Filename;
00138 private:
00139     int m_Mode;
00140 protected:
00141     virtual bool ValidateXml(FILE* docfd, const char* uri=NULL, const char* filepath=NULL);
00142 };
00143 
00144 class tXmlResource : public tXmlParser {
00145 public:
00146     bool LoadFile(const char* filename, const char* uri="");
00147         tResourcePath const &Path() const {return m_Path;} 
00148 protected:
00149     bool ValidateXml(FILE* docfd, const char* uri, const char* filepath);
00150     tResourcePath m_Path; 
00151     node GetFileContents(void); 
00152 };
00153 
00154 #ifndef _MSC_VER
00158 template<typename T> void tXmlParser::node::GetProp(CHAR const *prop, T &target) const {
00159     if(!(GetProp(prop).Convert(target))) {
00160         tERR_WARN( "Property '" + tString(prop) + "' of node of type '" + GetName() + "' is '" + GetProp(prop) + "' which isn't of type '" + typeid(T).name() + "' as needed.");
00161     }
00162 }
00163 #endif
00164 
00165 }
00166 using tXmlParserNamespace::tXmlParser;
00167 using tXmlParserNamespace::tXmlResource;
00168 #endif

Generated on Sat Mar 15 22:56:02 2008 for Armagetron Advanced by  doxygen 1.5.4