00001
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
00032
00033 #if !defined(AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_)
00034 #define AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_
00035
00036 #if _MSC_VER > 1000
00037 #pragma once
00038 #endif // _MSC_VER > 1000
00039
00040 #include <windows.h>
00041 #include <vector>
00042 #include <deque>
00043 #include <string>
00044 #include "CCTChar.h"
00045 #include <tchar.h>
00046
00047 struct _tagXMLAttr;
00048 typedef _tagXMLAttr XAttr, *LPXAttr;
00049 typedef std::vector<LPXAttr> XAttrs;
00050
00051 struct _tagXMLNode;
00052 typedef _tagXMLNode XNode, *LPXNode;
00053 typedef std::vector<LPXNode> XNodes, *LPXNodes;
00054
00055 struct _tagXMLDocument;
00056 typedef struct _tagXMLDocument XDoc, *LPXDoc;
00057
00058
00059 typedef struct _tagXmlEntity
00060 {
00061 TCHAR entity;
00062 TCHAR ref[10];
00063 int ref_len;
00064 }XENTITY,*LPXENTITY;
00065
00066 typedef struct _tagXMLEntitys : public std::vector<XENTITY>
00067 {
00068 LPXENTITY GetEntity( int entity );
00069 LPXENTITY GetEntity( LPCTSTR entity );
00070 int GetEntityCount( LPCTSTR str );
00071 std::tstring::size_type Ref2Entity( LPCTSTR estr, LPTSTR str, int len );
00072 std::tstring::size_type Entity2Ref( LPCTSTR str, LPTSTR estr, int estrlen );
00073 std::tstring Ref2Entity( LPCTSTR estr );
00074 std::tstring Entity2Ref( LPCTSTR str );
00075 std::tstring::size_type Ref2Entity( LPCTSTR estr, std::tstring& str, int elen );
00076 std::tstring::size_type Entity2Ref( LPCTSTR str, std::tstring& estr, int len );
00077
00078 _tagXMLEntitys(){};
00079 _tagXMLEntitys( LPXENTITY entities, int count );
00080 }XENTITYS,*LPXENTITYS;
00081 extern XENTITYS entityDefault;
00082 std::tstring XRef2Entity( LPCTSTR estr );
00083 std::tstring XEntity2Ref( LPCTSTR str );
00084
00085 typedef enum
00086 {
00087 PIE_PARSE_WELFORMED = 0,
00088 PIE_ALONE_NOT_CLOSED,
00089 PIE_NOT_CLOSED,
00090 PIE_NOT_NESTED,
00091 PIE_ATTR_NO_VALUE
00092 }PCODE;
00093
00094
00095 typedef struct _tagParseInfo
00096 {
00097 bool trim_value;
00098 bool entity_value;
00099 LPXENTITYS entitys;
00100 TCHAR escape_value;
00101 bool force_parse;
00102
00103 LPTSTR xml;
00104 bool erorr_occur;
00105 LPTSTR error_pointer;
00106 PCODE error_code;
00107 std::tstring error_string;
00108
00109 LPXDoc doc;
00110 _tagParseInfo() { trim_value = false; entity_value = true; force_parse = false; entitys = &entityDefault; xml = NULL; erorr_occur = false; error_pointer = NULL; error_code = PIE_PARSE_WELFORMED; escape_value = '\\'; }
00111 }PARSEINFO,*LPPARSEINFO;
00112 extern PARSEINFO piDefault;
00113
00114
00115 typedef struct _tagDispOption
00116 {
00117 bool newline;
00118 bool reference_value;
00119 char value_quotation_mark;
00120 LPXENTITYS entitys;
00121
00122 int tab_base;
00123 _tagDispOption() { newline = true; reference_value = true; entitys = &entityDefault; tab_base = 0; value_quotation_mark = '"'; }
00124 }DISP_OPT, *LPDISP_OPT;
00125 extern DISP_OPT optDefault;
00126
00127
00128 typedef struct _tagXMLAttr
00129 {
00130 std::tstring name;
00131 std::tstring value;
00132
00133 _tagXMLNode* parent;
00134
00135 std::tstring GetXML( LPDISP_OPT opt = &optDefault ) const;
00136 std::tstring GetValue( LPDISP_OPT opt = &optDefault ) const;
00137 }XAttr, *LPXAttr;
00138
00139 typedef enum
00140 {
00141 XNODE_ELEMENT,
00142 XNODE_PI,
00143 XNODE_COMMENT,
00144 XNODE_CDATA,
00145 XNODE_DOC,
00146 }NODE_TYPE;
00147
00148
00149 typedef struct _tagXMLNode
00150 {
00151
00152 std::tstring name;
00153 std::tstring value;
00154
00155
00156 LPXNode parent;
00157 XNodes childs;
00158 XAttrs attrs;
00159 NODE_TYPE type;
00160 LPXDoc doc;
00161
00162
00163 LPTSTR Load( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
00164 std::tstring GetXML( LPDISP_OPT opt = &optDefault ) const;
00165 std::tstring GetText( LPDISP_OPT opt = &optDefault ) const;
00166
00167
00168 LPTSTR LoadAttributes( LPCTSTR pszAttrs, LPPARSEINFO pi = &piDefault );
00169 LPTSTR LoadAttributes( LPCTSTR pszAttrs, LPCTSTR pszEnd, LPPARSEINFO pi = &piDefault );
00170 LPTSTR LoadProcessingInstrunction( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
00171 LPTSTR LoadComment( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
00172 LPTSTR LoadCDATA( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
00173
00174
00175 LPXAttr GetAttr( LPCTSTR attrname );
00176 LPCTSTR GetAttrValue( LPCTSTR attrname );
00177 XAttrs GetAttrs( LPCTSTR name );
00178
00179
00180 LPXNode GetChild( LPCTSTR name );
00181 LPCTSTR GetChildValue( LPCTSTR name );
00182 std::tstring GetChildText( LPCTSTR name, LPDISP_OPT opt = &optDefault );
00183 XNodes GetChilds( LPCTSTR name );
00184 XNodes GetChilds();
00185
00186 LPXAttr GetChildAttr( LPCTSTR name, LPCTSTR attrname );
00187 LPCTSTR GetChildAttrValue( LPCTSTR name, LPCTSTR attrname );
00188
00189
00190 LPXNode Find( LPCTSTR name );
00191 const LPXNode Find ( LPCTSTR name ) const;
00192 LPXNode Find( LPCTSTR name, LPCTSTR id );
00193 const LPXNode Find ( LPCTSTR name, LPCTSTR id ) const;
00194
00195
00196 std::tstring::size_type GetChildCount();
00197 LPXNode GetChild( XNodes::size_type i );
00198 XNodes::iterator GetChildIterator( LPXNode node );
00199 LPXNode CreateNode( LPCTSTR name = NULL, LPCTSTR value = NULL );
00200 LPXNode AppendChild( LPCTSTR name = NULL, LPCTSTR value = NULL );
00201 LPXNode AppendChild( LPXNode node );
00202 bool RemoveChild( LPXNode node );
00203 LPXNode DetachChild( LPXNode node );
00204
00205
00206 void CopyNode( LPXNode node );
00207 void CopyBranch( const LPXNode branch );
00208 void _CopyBranch( const LPXNode node, LPXDoc docNew = NULL );
00209 LPXNode AppendChildBranch( LPXNode node );
00210
00211
00212 LPXAttr GetAttr( XAttrs::size_type i );
00213 XAttrs::iterator GetAttrIterator( LPXAttr node );
00214 LPXAttr CreateAttr( LPCTSTR anem = NULL, LPCTSTR value = NULL );
00215 LPXAttr AppendAttr( LPCTSTR name = NULL, LPCTSTR value = NULL );
00216 LPXAttr AppendAttr( LPXAttr attr );
00217 bool RemoveAttr( LPXAttr attr );
00218 LPXAttr DetachAttr( LPXAttr attr );
00219
00220
00221 LPXNode operator [] ( int i ) { return GetChild(i); }
00222 XNode& operator = ( const XNode& node ) { CopyBranch((const LPXNode)&node); return *this; }
00223
00224 _tagXMLNode() { parent = NULL; doc = NULL; type = XNODE_ELEMENT; }
00225 ~_tagXMLNode();
00226
00227 void Close();
00228
00229 protected:
00230
00231 std::tstring GetTextFromValue(LPDISP_OPT opt) const;
00232
00233 }XNode, *LPXNode;
00234
00235
00236 typedef struct _tagXMLDocument : public XNode
00237 {
00238 PARSEINFO parse_info;
00239
00240 _tagXMLDocument() { parent = NULL; doc = this; type = XNODE_DOC; }
00241
00242 LPTSTR Load( LPCTSTR pszXml, LPPARSEINFO pi = NULL );
00243 LPXNode GetRoot();
00244 const LPXNode GetRoot() const;
00245
00246 const XDoc& operator = ( const XDoc& doc );
00247
00248 }XDoc, *LPXDoc;
00249
00250
00251 inline long XStr2Int( LPCTSTR str, long default_value = 0 )
00252 {
00253 return ( str && *str ) ? _ttol(str) : default_value;
00254 }
00255
00256 inline bool XIsEmptyString( const std::tstring& s )
00257 {
00258 std::tstring::const_iterator i = s.begin();
00259 while ((i != s.end()) && (_tcschr(_T(" \n\r\t"), *s.begin()) != NULL))
00260 i++;
00261
00262 return (i == s.end());
00263 }
00264
00265 #endif // !defined(AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_)