FirteX-高性能全文索引和检索平台API Documentation |
00001 #ifndef _XML_H 00002 #define _XML_H 00003 00004 #if _MSC_VER > 1000 00005 #pragma once 00006 #endif // _MSC_VER > 1000 00007 00008 #include "typedefs.h" 00009 #include "StringUtils.h" 00010 #include "Misc.h" 00011 00012 #include <map> 00013 #include <list> 00014 using namespace std; 00015 00016 00017 00018 00019 namespace firtex 00020 { 00021 namespace utility 00022 { 00023 class CXMLNode; 00024 class CXMLElement; 00025 class CXMLAttribute; 00026 class CXMLNode 00027 { 00028 // Construction 00029 public: 00030 CXMLNode(CXMLElement* pParent = NULL, const tchar* pszName = NULL); 00031 virtual ~CXMLNode(); 00032 00033 // Attributes 00034 protected: 00035 int m_nNode; 00036 CXMLElement* m_pParent; 00037 tstring m_sName; 00038 tstring m_sValue; 00039 00040 enum { xmlNode, xmlElement, xmlAttribute }; 00041 00042 // Operations 00043 public: 00044 inline int getType() ; 00045 inline CXMLNode* asNode() ; 00046 inline CXMLElement* asElement() ; 00047 inline CXMLAttribute* asAttribute() ; 00048 public: 00049 inline CXMLElement* getParent() ; 00050 inline void Delete(); 00051 public: 00052 inline tstring getName() ; 00053 inline void setName(const tchar* pszValue); 00054 inline bool isNamed(const tchar* pszName) ; 00055 inline tstring getValue() ; 00056 inline void setValue(const tchar* pszValue); 00057 protected: 00058 static bool parseMatch(const tchar*& pszXML, const tchar* pszToken); 00059 static bool parseIdentifier(const tchar*& pszXML, tstring& strIdentifier); 00060 public: 00061 static tstring stringToValue(const tchar*& pszXML, int nLength); 00062 static void valueToString(const tchar* pszValue, tstring& strXML); 00063 static void uniformString(tstring& str); 00064 00065 friend class CXMLElement; 00066 }; 00067 00068 typedef list<CXMLElement*> ElementList; 00069 typedef ElementList::iterator ElementIterator; 00070 typedef map<tstring,CXMLAttribute*> AttributeMap; 00071 typedef AttributeMap::iterator AttributeIterator; 00072 typedef pair<tstring,CXMLAttribute*> AttributePair; 00073 class CXMLElement : public CXMLNode 00074 { 00075 // Construction 00076 public: 00077 CXMLElement(CXMLElement* pParent = NULL, const tchar* pszName = NULL); 00078 virtual ~CXMLElement(); 00079 00080 // Attributes 00081 protected: 00082 ElementList m_pElements; 00083 AttributeMap m_pAttributes; 00084 00085 // Operations 00086 public: 00087 CXMLElement* clone(CXMLElement* pParent = NULL); 00088 inline CXMLElement* detach(); 00089 public: 00090 inline CXMLElement* addElement(const tchar* pszName); 00091 inline CXMLElement* addElement(CXMLElement* pElement); 00092 inline int getElementCount() ; 00093 inline CXMLElement* getFirstElement() ; 00094 inline ElementIterator getElementIterator() ; 00095 inline CXMLElement* getNextElement(ElementIterator& iter) ; 00096 inline bool hasNextElement(ElementIterator& iter) ; 00097 inline CXMLElement* getElementByName(const tchar* pszName) ; 00098 inline CXMLElement* getElementByName(const tchar* pszName, bool bCreate); 00099 inline void removeElement(CXMLElement* pElement); 00100 void deleteAllElements(); 00101 public: 00102 inline CXMLAttribute* addAttribute(const tchar* pszName, const tchar* pszValue = NULL); 00103 inline CXMLAttribute* addAttribute(CXMLAttribute* pAttribute); 00104 inline int getAttributeCount() const; 00105 00106 inline AttributeIterator getAttributeIterator() ; 00107 inline CXMLAttribute* getNextAttribute(AttributeIterator& iter) ; 00108 inline bool hasNextAttribute(AttributeIterator& iter) ; 00109 inline CXMLAttribute* getAttribute(const tchar* pszName) ; 00110 inline tstring getAttributeValue(const tchar* pszName, const tchar* pszDefault = NULL) ; 00111 inline void removeAttribute(CXMLAttribute* pAttribute); 00112 inline void deleteAttribute(const tchar* pszName); 00113 void deleteAllAttributes(); 00114 public: 00115 tstring toString(bool bHeader = false, bool bNewline = false); 00116 void toString(tstring& strXML, bool bNewline = false); 00117 void toFile(const tstring& sPath, bool bHeader = false); 00118 bool parseString(const tchar*& strXML); 00119 bool equals(CXMLElement* pXML) ; 00120 //tstring getRecursiveWords(); 00121 //void addRecursiveWords(tstring& strWords); 00122 00123 static CXMLElement* fromString(const tchar* pszXML, bool bHeader = false); 00124 static CXMLElement* fromBytes(byte* pByte, size_t nByte, bool bHeader = false); 00125 static CXMLElement* fromFile(const tstring& sPath, bool bHeader = false); 00126 00127 00128 }; 00129 00130 00131 class CXMLAttribute : public CXMLNode 00132 { 00133 // Construction 00134 public: 00135 CXMLAttribute(CXMLElement* pParent, const tchar* pszName = NULL); 00136 virtual ~CXMLAttribute(); 00137 00138 // Attributes 00139 public: 00140 static const tchar* xmlnsSchema; 00141 static const tchar* xmlnsInstance; 00142 static const tchar* schemaName; 00143 00144 // Operations 00145 public: 00146 CXMLAttribute* clone(CXMLElement* pParent = NULL); 00147 void toString(tstring& strXML); 00148 bool parseString(const tchar*& strXML); 00149 bool equals(CXMLAttribute* pXML) ; 00150 }; 00151 00152 #include "XML.inl" 00153 00154 } 00155 } 00156 00157 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex