#include "xmlbase.h"

// ----------------------------------------------------------------------------
const XMLCh* IXmlBase::GetNodeAttribute( const DOMNamedNodeMap* nodeMap, const char* name )
{	
	XMLCh tmpStr[256];
	DOMNode* attributeNode;

	if ( nodeMap == NULL || name == NULL )
		return NULL;

	XMLString::transcode(name, tmpStr, 255);
	attributeNode = nodeMap->getNamedItem(tmpStr);
	if ( attributeNode )
	{
		return attributeNode->getNodeValue();
	}
	return NULL;
}

// ----------------------------------------------------------------------------
bool IXmlBase::XmlValueToBoolean( const char* valueStr )
{
	if ( valueStr && XMLString::compareString(valueStr, "true") == 0 )
		return true;
	return false;
}

// ----------------------------------------------------------------------------
CXmlNode::CXmlNode()
{
	m_type = eXmlUiType_Node;
}

// ----------------------------------------------------------------------------
bool CXmlNode::LoadFromDOMNode(const DOMNode* node)
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Attribute
	DOMNamedNodeMap* nodeMap = node->getAttributes();
	if (nodeMap)
		LoadNodeAttribute(nodeMap);

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());
			LoadNodeElement(node, nodeName);			
			XMLString::release(&nodeName);
		}

		node = node->getNextSibling();
	}

	return true;
}

// ----------------------------------------------------------------------------
CXmlValue::CXmlValue()
{
	m_val = 0.0f;
}

// ----------------------------------------------------------------------------
bool CXmlValue::LoadFromDOMNode( const DOMNode* node )
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			if ( XMLString::compareString(nodeName, "AbsValue") == 0 ||
				XMLString::compareString(nodeName, "RelValue") == 0 )
			{
				DOMNamedNodeMap* nodeMap = node->getAttributes();
				if ( nodeMap )
				{
					const XMLCh* nodeValue;
					if ( (nodeValue = GetNodeAttribute(nodeMap, "val")) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_val = (float)atof(tmp);
						XMLString::release(&tmp);
					}					
				}
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}
	return true;
}

// ----------------------------------------------------------------------------
CXmlDimension::CXmlDimension()
{
	m_x = 0.0f;
	m_y = 0.0f;
}

// ----------------------------------------------------------------------------
bool CXmlDimension::LoadFromDOMNode( const DOMNode* node )
{	
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			if ( XMLString::compareString(nodeName, "AbsDimension") == 0 ||
				XMLString::compareString(nodeName, "RelDimension") == 0 )
			{
				DOMNamedNodeMap* nodeMap = node->getAttributes();
				if ( nodeMap )
				{
					const XMLCh* nodeValue;
					if ( (nodeValue = GetNodeAttribute(nodeMap, "x")) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_x = (float)atof(tmp);
						XMLString::release(&tmp);
					}
					if ( (nodeValue = GetNodeAttribute(nodeMap, "y")) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_y = (float)atof(tmp);
						XMLString::release(&tmp);
					}
				}
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}	

	return true;
}

// ----------------------------------------------------------------------------
CXmlInset::CXmlInset()
{
	m_left = 0.0f;
	m_top = 0.0f;
	m_right = 0.0f;
	m_bottom = 0.0f;
}

// ----------------------------------------------------------------------------
bool CXmlInset::LoadFromDOMNode( const DOMNode* node )
{	
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			if ( XMLString::compareString( nodeName, "AbsInset" ) == 0 ||
				XMLString::compareString( nodeName, "RelInset" ) == 0 )
			{
				DOMNamedNodeMap* nodeMap = node->getAttributes();
				if ( nodeMap )
				{
					const XMLCh* nodeValue;
					if ( (nodeValue = GetNodeAttribute( nodeMap, "left" )) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_left = (float)atof(tmp);
						XMLString::release(&tmp);
					}
					if ( (nodeValue = GetNodeAttribute( nodeMap, "top" )) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_top = (float)atof(tmp);
						XMLString::release(&tmp);
					}
					if ( (nodeValue = GetNodeAttribute( nodeMap, "right" )) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_right = (float)atof(tmp);
						XMLString::release(&tmp);
					}
					if ( (nodeValue = GetNodeAttribute( nodeMap, "bottom" )) != NULL )
					{
						char* tmp = XMLString::transcode(nodeValue);
						m_bottom = (float)atof(tmp);
						XMLString::release(&tmp);
					}
				}
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}

	return true;
}

// ----------------------------------------------------------------------------
CXmlColor::CXmlColor()
{
	m_a = 1.0f;
	m_r = 1.0f;
	m_g = 1.0f;
	m_b = 1.0f;
}

// ----------------------------------------------------------------------------
bool CXmlColor::LoadFromDOMNode( const DOMNode* node )
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	DOMNamedNodeMap* nodeMap = node->getAttributes();
	if ( nodeMap )
	{
		const XMLCh* nodeValue;
		if ( (nodeValue = GetNodeAttribute( nodeMap, "r" )) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_r = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute( nodeMap, "g" )) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_g = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute( nodeMap, "b" )) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_b = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute( nodeMap, "a" )) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_a = (float)atof(tmp);
			XMLString::release(&tmp);
		}
	}

	return true;	
}