/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CXmlDoc.cpp  -  description
                             -------------------
    begin                : Sun May 22 2003
    based on the code    : Mikko Lahteenmaki <Laza@Flashmail.com>
    copyright            : (C) 2003-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 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 Library
   General Public License for more details.

   You should have received a copy of the GNU Library 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.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <ctype.h>
#include <stdlib.h>

#include <sptk3/CStrings.h>
#include <sptk3/cxml>

using namespace std;
using namespace sptk;

int           CXmlDoc::m_indent_spaces = 2;
const string  CXmlDoc::nodeNameString("#document");

CXmlDoc::CXmlDoc()
: CXmlElement(*this) {
}

CXmlDoc::CXmlDoc(const char *aname, const char *public_id, const char *system_id)
: CXmlElement(*this), m_doctype(aname,public_id,system_id) {
}

CXmlNode *CXmlDoc::rootNode() {
	iterator itor = begin();
	iterator iend = end();
	for (; itor != iend; itor++ ) {
		CXmlNode *nd = *itor;
		if(nd->type()==DOM_ELEMENT)
			return nd;
	}
	return 0;
}

void CXmlDoc::clear() {
	CXmlElement::clear();
	CSharedStrings::clear();
}

CXmlNode *CXmlDoc::createElement(const char *tagname) {
	CXmlNode *node = new CXmlElement(this,tagname);
	return node;
}

void CXmlDoc::load(const CBuffer &buffer, bool htmlMode) {
   clear();
	CXmlLocator locator;
	CXmlParser::createDOM(*this, buffer, &locator, htmlMode);
}

void CXmlDoc::save(CBuffer &buffer,int /*indent*/) const {
   std::string tmp;
	CXmlNode *xml_pi = 0;

   buffer.reset();

   // Write XML PI
	const_iterator itor = begin();
	const_iterator iend = end();
	for (; itor != iend; itor++ ) {
		CXmlNode *node = *itor;
		if(node->type() == DOM_PI && upperCase(node->name())=="XML") {
			xml_pi = node;
			xml_pi->save(buffer);
		}
	}

	if (!docType().name().empty()) {
		buffer.append("<!DOCTYPE " + docType().name());
		if(!docType().systemID().empty()) {
			buffer.append(" SYSTEM \"" + docType().systemID() + "\"");

			if(!docType().publicID().empty()) {
				buffer.append(" PUBLIC \"" + docType().publicID() + "\"");
			}
		}
		if (docType().entities().size() > 0) {
			buffer.append(" [\n",3);
			const CXmlEntities& entities = docType().entities();
			CXmlEntities::const_iterator it = entities.begin();
			for(; it != entities.end(); it++) {
				buffer.append("<!ENTITY " + it->first + " \"" + it->second + "\">\n");
			}
			buffer.append("]",1);
		}
		buffer.append(">\n",2);
	}

   // call save() method of the first (and hopefully only) node in xmldocument
	for (itor = begin(); itor != iend; itor++ ) {
		CXmlNode *node = *itor;
		if (node == xml_pi) continue;
		node->save(buffer, 0);
	}
}
