00001 /// \file CATXMLParser.h 00002 /// \brief XML Parser 00003 /// \ingroup CAT 00004 /// 00005 /// Copyright (c) 2003-2008 by Michael Ellison. 00006 /// See COPYING.txt for the \ref gaslicense License (MIT License). 00007 /// 00008 // $Author: mikeellison $ 00009 // $Date: 2008-01-21 21:40:31 -0600 (Mon, 21 Jan 2008) $ 00010 // $Revision: $ 00011 // $NoKeywords: $ 00012 00013 00014 #ifndef _CATXMLPARSER_H_ 00015 #define _CATXMLPARSER_H_ 00016 00017 #include "CATInternal.h" 00018 #include "CATXMLFactory.h" 00019 #include "CATStream.h" 00020 00021 #include <stack> 00022 #define XML_STATIC 00023 #define XML_UNICODE_WCHAR_T 00024 #include "expat.h" 00025 00026 /// Size to use for buffered reading from files/streams 00027 const int kMaxXMLBufferSize = 1024; 00028 00029 /// \class CATXMLParser 00030 /// \brief XML PArser 00031 /// \ingroup CAT 00032 class CATXMLParser 00033 { 00034 public: 00035 /// Parses a file with the given factory. The root 00036 /// node will be placed into root. 00037 /// 00038 /// \param path Path to file 00039 /// \param factory Factory to use for object creation. 00040 /// Must be derived from CATXMLFactory. 00041 /// \param root Ptr to receive root object of tree on success. 00042 /// \return CATRESULT CATRESULT_SUCCESS on success. 00043 static CATResult Parse( const CATWChar* path, 00044 CATXMLFactory* factory, 00045 CATXMLObject*& root); 00046 00047 /// Parses a memory buffer with the given factory. The root 00048 /// node will be placed into root. 00049 /// 00050 /// \param memoryBuf Pointer to memory buffer containing 00051 /// full XML file. 00052 /// \param bufLenBytes size of buffer in bytes (NOT characters) 00053 /// \param factory Factory to use for object creation. 00054 /// Must be derived from CATXMLFactory. 00055 /// \param root Ptr to receive root object of tree on success. 00056 /// \return CATRESULT CATRESULT_SUCCESS on success. 00057 static CATResult ParseMemory( const void* memoryBuf, 00058 CATInt32 bufLenBytes, 00059 CATXMLFactory* factory, 00060 CATXMLObject*& root); 00061 00062 /// Parses a stream with the given factory. Stream location should 00063 /// be at the start of the XML. Root node is placed in root on 00064 /// success; 00065 /// 00066 /// \param stream Stream to read XML data from 00067 /// \param factory Factory to use for object creation. 00068 /// \param root Ptr to receive root object of tree. 00069 /// \return CATRESULT CATRESULT_SUCCESS on success. 00070 static CATResult ParseStream(CATStream* stream, 00071 CATXMLFactory* factory, 00072 CATXMLObject*& root); 00073 00074 static CATResult Write( const CATString& filename, 00075 CATXMLObject* rootNode); 00076 00077 protected: 00078 CATXMLParser(); 00079 virtual ~CATXMLParser(); 00080 00081 // expat XML callbacks used during a Parse 00082 static void XMLCALL StartElement( void *userData, 00083 const XML_Char *name, 00084 const XML_Char **atts); 00085 00086 static void XMLCALL EndElement( void *userData, 00087 const XML_Char *name); 00088 00089 static void XMLCALL CharacterHandler( void *userData, 00090 const XML_Char *s, 00091 int len); 00092 00093 private: 00094 std::stack<CATXMLObject*> fObjectStack; ///< Current object stack during a parse 00095 CATXMLObject* fCurParent; ///< Last object pushed onto the stack (parent of current) 00096 CATXMLFactory* fFactory; ///< Factory to create objects with 00097 CATXMLObject** fRootObjPtr; ///< Root object ptr 00098 }; 00099 00100 #endif //_CATXMLPARSER_H_