- // Element.cpp: implementation of the CElement class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Element.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CElement::CElement(CElement* parent) :
- m_parent(NULL)
- {
- if (parent)
- {
- parent->m_children.AddTail(this);
- m_parent = parent;
- m_level = parent->m_level + 1;
- }
- else
- {
- m_level = 0;
- }
- }
- CElement::~CElement()
- {
- Free();
- }
- void CElement::Free()
- {
- while(m_children.GetCount() > 0)
- delete m_children.RemoveTail();
- }
- int CElement::Enumerate(LPENUM_CALLBACK_FUNC pFunc)
- {
- if (pFunc == NULL)
- return 0;
- int ret = 0;
- if ( (ret = (*pFunc)(this)) != 0)
- return ret;
- POSITION pos = m_children.GetHeadPosition();
- while(pos != NULL)
- {
- if ((ret = m_children.GetNext(pos)->Enumerate(pFunc)) != 0)
- return ret;
- }
- return 0;
- }
- int CElement::BuildElement(CString& xml)
- {
- int ret = 0;
- CString indent;
- for(int i = 0; i < m_level; i++)
- {
- indent += "t";
- }
- CString begin_tag = indent + "<" + m_tag + ">rn";
- xml += begin_tag;
- if (!m_content.IsEmpty())
- xml = xml + indent + "t" + m_content + "rn";
- POSITION pos = m_children.GetHeadPosition();
- while(pos != NULL)
- {
- CElement* child = m_children.GetNext(pos);
- _ASSERTE(child != NULL);
- if (!child)
- return -1;
- if ( (ret = child->BuildElement(xml)) != 0)
- return ret;
- }
- CString finish_tag = indent + "</" + m_tag + ">rn";
- xml += finish_tag;
- return 0;
- }
- int CElement::AddChild(CString tag, CString content)
- {
- CElement* child = new CElement(this);
- if (child == NULL)
- return -1;
- child->set_tag(tag);
- child->set_content(content);
- return 0;
- }
- HRESULT CElement::GetChildrenByTag(LPCTSTR tag, CElementList &list)
- {
- list.RemoveAll();
- POSITION pos = m_children.GetHeadPosition();
- while(pos != NULL)
- {
- CElement* child = m_children.GetNext(pos);
- _ASSERTE(child != NULL);
- if (!child)
- return E_UNEXPECTED;
- // NOTIFY: don't release child out by this list.
- if (tag != NULL)
- {
- if (child->get_tag() == tag)
- list.AddTail(child);
- }
- else
- list.AddTail(child);
- }
- return S_OK;
- }
- CElement* CElement::GetFirstChildByTag(LPCTSTR tag)
- {
- POSITION pos = m_children.GetHeadPosition();
- while(pos != NULL)
- {
- CElement* child = m_children.GetNext(pos);
- _ASSERTE(child != NULL);
- if (!child)
- return NULL;
- // NOTIFY: don't release child out by this list.
- if (tag != NULL)
- {
- if (child->get_tag() == tag)
- return child;
- }
- else
- return child;
- }
- return NULL;
- }