00001 00002 00003 #ifndef _GXMLTREE_H_ 00004 #define _GXMLTREE_H_ 00005 00006 #include "GHashTable.h" 00007 #include "GRefCount.h" 00008 #include "GDom.h" 00009 00011 #define GXT_NO_ENTITIES 0x0001 00013 #define GXT_NO_PRETTY_WHITESPACE 0x0002 00015 #define GXT_PRETTY_WHITESPACE 0x0004 00017 #define GXT_KEEP_WHITESPACE 0x0008 00019 #define GXT_NO_DOM 0x0010 00020 00021 class GXmlTree; 00022 class GXmlTreePrivate; 00023 00024 class LgiClass GXmlAlloc : public GRefCount 00025 { 00026 public: 00027 virtual ~GXmlAlloc() {} 00028 00029 virtual void *Alloc(size_t Size) = 0; 00030 virtual void Free(void *Ptr) = 0; 00031 00032 char *Alloc(const char *s, int len = -1) 00033 { 00034 if (!s) return 0; 00035 if (len < 0) len = strlen(s); 00036 char *p = (char*) Alloc(len+1); 00037 if (!p) return 0; 00038 memcpy(p, s, len); 00039 p[len] = 0; 00040 return p; 00041 } 00042 }; 00043 00045 class LgiClass GXmlAttr 00046 { 00047 friend class GXmlTag; 00048 friend class GXmlTree; 00049 00051 char *Name; 00053 char *Value; 00054 00055 public: 00056 GXmlAttr() 00057 { 00058 Name = 0; 00059 Value = 0; 00060 } 00061 00062 char *GetName() { return Name; } 00063 char *GetValue() { return Value; } 00064 }; 00065 00072 class LgiClass GXmlTag : virtual public GDom 00073 { 00074 friend class GXmlTree; 00075 00076 void ParseAttribute(GXmlTree *Tree, GXmlAlloc *Alloc, char *&t, bool &NoChildren, bool &TypeDef); 00077 00078 protected: 00079 GAutoRefPtr<GXmlAlloc> Allocator; 00080 00081 bool Write; 00082 GXmlAttr *_Attr(const char *Name, bool Write); 00083 bool GetVariant(const char *Name, GVariant &Value, char *Array); 00084 bool SetVariant(const char *Name, GVariant &Value, char *Array); 00085 00086 public: 00089 char *Tag; 00091 char *Content; 00093 GXmlTag *Parent; 00095 GArray<GXmlAttr> Attr; 00098 List<GXmlTag> Children; 00099 00101 GXmlTag 00102 ( 00104 const char *tag = 0, 00106 GXmlAlloc *alloc = 0 00107 ); 00109 GXmlTag(const GXmlTag &t); 00110 virtual ~GXmlTag(); 00111 00113 bool Dump(int Depth = 0); 00115 void Empty(bool Deep); 00117 void EmptyAttributes(); 00119 void EmptyChildren(); 00120 00122 bool IsTag(const char *s) { return Tag && s ? stricmp(Tag, s) == 0 : false; } 00123 00125 char *GetAttr(const char *Name); 00127 int GetAsInt(const char *Name); 00129 bool SetAttr(const char *Name, const char *Value); 00131 bool SetAttr(const char *Name, int Value); 00133 bool DelAttr(const char *Name); 00134 00136 bool SerializeAttr(const char *Attr, int &Int); 00138 bool SerializeAttr(const char *Attr, char *&Str); 00140 bool SerializeAttr(const char *Attr, double &Dbl); 00141 00143 virtual bool Serialize() { return false; } 00145 GXmlTag *GetTag(const char *Name, bool Create = false); 00147 GXmlTag *CreateTag(const char *Name, char *Content = 0); 00149 virtual void InsertTag(GXmlTag *t); 00151 virtual void RemoveTag(); 00152 00154 GXmlTag &operator =(GXmlTag &t); 00156 bool Copy(GXmlTag &t, bool Deep = false); 00157 }; 00158 00163 class GXmlFactory 00164 { 00165 public: 00167 virtual GXmlTag *Create(char *Tag) = 0; 00168 }; 00169 00171 class LgiClass GXmlTree 00172 { 00173 friend class GXmlTag; 00174 GXmlTreePrivate *d; 00175 00176 protected: 00177 GXmlTag *Parse(GXmlTag *Tag, GXmlAlloc *Alloc, char *&t, bool &NoChildren, bool InTypeDef); 00178 void Output(GXmlTag *t, int Depth); 00179 00180 public: 00182 GXmlTree 00183 ( 00185 int Flags = 0 00186 ); 00187 ~GXmlTree(); 00188 00190 bool Read 00191 ( 00193 GXmlTag *Root, 00195 GStreamI *File, 00198 GXmlFactory *Factory = 0 00199 ); 00201 bool Write 00202 ( 00204 GXmlTag *Root, 00206 GStreamI *File 00207 ); 00209 char *GetErrorMsg(); 00211 GHashTable *NoChildTags(); 00213 char *GetStyleFile(char **StyleType = 0); 00215 void SetStyleFile(char *stylefile, char *styletype = "text/css"); 00216 00218 GHashTbl<const char*,char16> *GetEntityTable(); 00220 char *DecodeEntities(char *s, int len = -1); 00222 char *EncodeEntities(char *s, int len = -1, const char *extra_characters = 0); 00224 bool EncodeEntities(GStreamI *out, char *s, int len, const char *extra_characters = 0); 00225 }; 00226 00227 #endif