/*  $Id: dom_element.cpp,v 1.14 2004/04/15 00:02:12 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "Core/precomp.h"
#include "API/Core/XML/dom_element.h"
#include "API/Core/XML/dom_node_list.h"
#include "API/Core/XML/dom_attr.h"
#include "API/Core/XML/dom_document.h"
#include "dom_node_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_DomElement construction:

CL_DomElement::CL_DomElement()
{
}

CL_DomElement::CL_DomElement(CL_DomDocument &doc, const std::string &tag_name)
: CL_DomNode(doc, ELEMENT_NODE)
{
	impl->node_name = tag_name;
}

CL_DomElement::CL_DomElement(const CL_SharedPtr<CL_DomNode_Generic> &impl) : CL_DomNode(impl)
{
}

CL_DomElement::~CL_DomElement()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomElement attributes:

std::string CL_DomElement::get_tag_name() const
{
	if (impl) return impl->node_name;
	return std::string();
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomElement operations:

bool CL_DomElement::has_attribute(const std::string &name) const
{
	if (impl)
	{
		for(int i = 0; i < (int)impl->attributes.get_length(); ++i)
			if (impl->attributes.item(i).to_attr().get_name() == name)
				return true;
	}
	return false;
}

std::string CL_DomElement::get_attribute(const std::string &name) const
{
	if (impl)
	{
		for(int i = 0; i < (int)impl->attributes.get_length(); ++i)
			if (impl->attributes.item(i).to_attr().get_name() == name)
				return impl->attributes.item(i).to_attr().get_value();
	}
	return std::string();
}

std::string CL_DomElement::get_attribute(const std::string &name, const std::string &default_value) const
{
	if (impl)
	{
		for(int i = 0; i < (int)impl->attributes.get_length(); ++i)
			if (impl->attributes.item(i).to_attr().get_name() == name)
				return impl->attributes.item(i).to_attr().get_value();
	}
	return default_value;
}

void CL_DomElement::set_attribute(const std::string &name, const std::string &value)
{
	if (impl)
	{
		for(int i = 0; i < (int)impl->attributes.get_length(); ++i)
			if (impl->attributes.item(i).to_attr().get_name() == name)
			{
				impl->attributes.item(i).to_attr().set_value(value);
				return;
			}

		CL_DomAttr attr(get_owner_document(), name);
		impl->attributes.set_named_item(attr);
		attr.impl->node_value = value;
	}
}

void CL_DomElement::remove_attribute(const std::string &name)
{
	if (impl)
	{
		impl->attributes.remove_named_item(name);
	}
}

CL_DomNodeList CL_DomElement::get_elements_by_tag_name(const std::string &name)
{
	return CL_DomNodeList();
}

std::string CL_DomElement::get_text() const
{
	std::string str;
	if (has_child_nodes() == false)
		return str;

	CL_DomNode cur = get_first_child();
	while (!cur.is_null())
	{
		if (cur.is_text())
			str.append(cur.get_node_value());
		cur = cur.get_next_sibling();
	}
	return str;
}

void CL_DomElement::normalize()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomElement implementation:
