#ifndef __XML_H
#define __XML_H

#ifndef __ASSOC_H
#include "assoc.h"
#endif

class TXmlItem;

typedef bool (*XmlItemCallback)(TXmlItem& item, long jolly);

class TXmlItem : public TObject
{
  DECLARE_DYNAMIC_CLASS(TXmlItem);

	TString m_strTag;
  TString* m_strText;
	TAssoc_array* m_Attributes;
  TArray* m_Children;

protected:
  int GetWord(istream& input, TString& str) const;
  int ReadTag(istream& input);

  TXmlItem& AddChild(TXmlItem* pItem);

public:
  const TString& GetTag() const { return m_strTag; }
  void SetTag(const char* strTag) { m_strTag = strTag; }

  const TString& GetText() const 
  { 
    if (m_strText != NULL)
      return *m_strText;
    return EMPTY_STRING; 
  }
  void SetText(const char* str) 
  { 
    if (m_strText == NULL)
      m_strText = new TString(str); 
    else
      *m_strText = str;
  }

	TXmlItem& AddEnclosedText(const char* str);
  bool GetEnclosedText(TString& str) const;

  TXmlItem& SetAttr(const char* strAttr, const char* strVal);
	const TString& GetAttr(const char* strAttr) const;
  int GetIntAttr(const char* strAttr) const;

  TXmlItem& AddChild(const char* strTag);
	TXmlItem& AddSoapString(const char* name, const char* value, bool typized = false);
	TXmlItem& AddSoapInt(const char* name, int value, bool typized = false);

  int GetChildren() const;
  TXmlItem* GetChild(size_t n) const;
  void RemoveLastChild();

  void Destroy();
	bool Read(istream& input);
	void Write(ostream& output, int nTab) const;
  void AsString(TString& str) const;

	void Save(const char* strFilename) const;

	TXmlItem* ForEach(XmlItemCallback cb, long jolly = 0);
  TXmlItem* FindFirst(const char* strTag) const;
	
	TXmlItem();
	~TXmlItem();
};

TXmlItem& operator<<(TXmlItem& item, const char* str);

//ostream& operator<<(ostream& outf, const char* str);
//ostream& operator<<(ostream& outf, TString str);
//istream& operator>>(istream& inf, TString& str);
void Spaces(ostream& outf, int nSpaces);
void WriteXmlString(ostream& outf, const char* str);
int hex2int(const char* str);

#define endl "\r\n";

#endif