00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
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; 
00092 
00093     
00094     tXmlParser() : m_Doc(0) { SetMode(DOM); };
00095     virtual ~tXmlParser();
00096 
00097     
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     
00105     bool LoadXmlFile(const char* filename, const char* uri="");
00106 
00107     
00108     bool LoadWithoutParsing(const char* filename, const char* uri="");
00109     bool LoadWithParsing(const char* filename, const char* uri="");
00110 
00111     
00112     
00113     
00114     
00115     bool Parse();
00116 
00117     
00118     
00119     virtual bool ParseDom();
00120 
00121     
00122     
00123     bool ParseSax();
00124 
00125     
00126     virtual void startElement(tString &element, tAttributeList &attributes);
00127     virtual void endElement(tString &element);
00128     virtual void startDocument();
00129     virtual void endDocument();
00130 
00131     
00132     
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