00001 /// \file CATXMLObject.h 00002 /// \brief XML Object Base 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 #ifndef _CATXMLOBJECT_H_ 00014 #define _CATXMLOBJECT_H_ 00015 00016 #include "CATInternal.h" 00017 #include "CATString.h" 00018 #include <map> 00019 00020 class CATStream; 00021 /// CATXMLAttribs are currently just a string map of attribute name/value 00022 typedef bool (*CATXMLKEYCOMP)( const CATWChar* g1, const CATWChar* g2); 00023 typedef std::map<const CATWChar*, CATWChar*, CATXMLKEYCOMP> CATXMLAttribs ; 00024 typedef CATXMLAttribs::iterator CATXMLAttribsIter; 00025 00026 /// \class CATXMLObject 00027 /// \brief XML Object Base 00028 /// \ingroup CAT 00029 /// 00030 class CATXMLObject 00031 { 00032 public: 00033 /// Object constructor 00034 /// 00035 /// \param type Name of object type, this corresponds to the XML tag 00036 CATXMLObject(const CATWChar* type); 00037 00038 /// Destructor 00039 virtual ~CATXMLObject(); 00040 00041 /// Insert a child object into the XML 00042 /// \param child Child to add to object (parent becomes set) 00043 void AddChild(CATXMLObject* child); 00044 00045 /// Retrieve number of child xml objects 00046 CATUInt32 GetNumChildren(); 00047 00048 /// Retrieve a ptr to a specific child (do NOT delete - owned by object) 00049 /// \param index index in child vector (0 <= index < GetNumChildren) 00050 /// \return CATXMLObject* Requested xml object, or 0 if not found 00051 CATXMLObject* GetChild(CATUInt32 index) const; 00052 00053 /// Set the parent object (generally only called by parent/factory) 00054 /// \param parent Parent object 00055 void SetParent(CATXMLObject* parent); 00056 00057 /// Retrieves the parent object 00058 /// \return CATXMLObject* Parent object, or 0 if none. 00059 CATXMLObject* GetParent(); 00060 00061 /// Add an attribute to the object 00062 /// \param key String key value 00063 /// \param value String value for the key 00064 /// \return CATResult 0 on success 00065 CATResult AddAttribute(const CATWChar* key, const CATWChar* value); 00066 00067 int GetNumAttributes(); 00068 00069 CATString GetAttributeKeyByIndex(int index); 00070 00071 /// Retrieve the value for a specified attribute key 00072 /// \param key Name of key to retrieve value of 00073 /// \return const CATWChar* Value, or NULL if not found (may be empty if defined but clear) 00074 CATString GetAttribute(const CATWChar* key); 00075 00076 /// Templated attribute conversion with default val. 00077 /// Returns default value if attribute is not found or is empty. 00078 /// otherwise, converts the CATString to the proper type. 00079 template<class T> 00080 T GetAttribute(const CATWChar* key, T defaultVal) 00081 { 00082 if (key == 0) 00083 return defaultVal; 00084 CATString attrib = GetAttribute(key); 00085 00086 if (attrib.IsEmpty()) 00087 return defaultVal; 00088 00089 return (T)attrib; 00090 } 00091 00092 /// Sets the attributes to an existing CATXMLAttribs object. 00093 /// 00094 /// The object then takes control of the XMLAttribs object - DO NOT DELETE. 00095 /// The CATXMLObject becomes responsible for deletion. 00096 /// 00097 /// \param attribs Attributes to set for object - ownership is taken by XMLObject 00098 /// 00099 void SetAttributes(CATXMLAttribs* attribs); 00100 00101 /// Child classes should override this to parse out the 00102 /// attribute values. 00103 virtual CATResult ParseAttributes(); 00104 00105 /// Key comparator function 00106 static bool CATXMLKeyComp( const CATWChar* g1, const CATWChar* g2); 00107 00108 /// Retrieve the type (the tag name) for the object. 00109 const CATWChar* GetType(); 00110 00111 /// Append to the character data found between start and end tags of the object. 00112 void AppendData(const CATWChar* data, CATInt32 len); 00113 00114 /// Set the data directly. 00115 void SetData(const CATWChar* data); 00116 00117 /// Retrieve the data 00118 const CATWChar* GetData(); 00119 00120 /// WriteToStream() is a recursive function that writes the object 00121 /// and its children to a stream as XML. 00122 /// 00123 /// You must open the stream and write the header out first, then 00124 /// call this on the top level nodes to create a valid XML file. 00125 /// 00126 /// \param stream - the opened stream to write to. 00127 /// \return CATResult - CAT_SUCCESS on success. 00128 CATResult WriteToStream(CATStream* stream); 00129 00130 protected: 00131 CATWChar* fType; ///< Text type 00132 CATXMLAttribs* fAttribs; ///< Attributes of the object 00133 CATXMLObject* fParent; ///< Parent xml object 00134 std::vector<CATXMLObject*> fChildren; ///< Child objects in xml 00135 std::wstring fData; ///< Data from xml for object 00136 }; 00137 00138 #endif //_CATXMLOBJECT_H_