/*
** A program to convert the XML rendered by KOffice into LATEX.
**
** Copyright (C) 2000-2002 Robert JACOLIN
**
** 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.
**
** To receive a copy of the GNU Library General Public License, write to the
** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
**
*/

#include <kdebug.h>
#include <KoStore.h>

#include "xmlparser.h"
#include "tqfile.h"

/* Init static data */
FileHeader* XmlParser::_fileHeader = 0;
Document* XmlParser::_root = 0;
KoStore* XmlParser::_in = NULL;

XmlParser::XmlParser(TQString filename): _filename(filename)
{
	TQFile f(filename);
	if(!f.open(IO_ReadOnly))
		return;
	if(!_document.setContent(&f))
	{
		f.close();
		return;
	}
	f.close();
	//_eltCurrent = _document.documentElement();
}

XmlParser::XmlParser(TQByteArray in)
{
	_document.setContent(in);
}

XmlParser::XmlParser(const KoStore* in)
{
  _in = const_cast<KoStore*>(in);
	if(!_in->open("root"))
	{
		kdError(30522) << "Unable to open input file!" << endl;
		return;
	}
	/* input file Reading */
	TQByteArray array = _in->read(_in->size());
	_document.setContent(array);
}

XmlParser::XmlParser()
{
}

XmlParser::~XmlParser()
{
	if(_in != NULL)
		_in->close();
}

TQDomNode XmlParser::getChild(TQDomNode balise, TQString name)
{
	TQDomNode node = getChild(balise, name, 0);
	kdDebug(30522) << node.nodeName() << endl;
	return node;
}

bool XmlParser::isChild(TQDomNode balise, TQString name)
{
	if(balise.isElement())
		return balise.toElement().elementsByTagName(name).count();
	return false;
}

TQDomNode XmlParser::getChild(TQDomNode balise, TQString name, int index)
{
	if(balise.isElement()) {
		TQDomNodeList children = balise.toElement().elementsByTagName(name);
		if ( children.count() )
			return children.item(index);
	}
	return TQDomNode();
}

TQDomNode XmlParser::getChild(TQDomNode balise, int index)
{
	TQDomNodeList children = balise.childNodes();
	if ( children.count() )
		return children.item(index);
	return TQDomNode();
}

TQString XmlParser::getData(TQDomNode balise, int index)
{
	return getChild(getChild(balise, index), 0).nodeValue();
}

TQString XmlParser::getData(TQDomNode balise, TQString name)
{
	return getChild(getChild(balise, name), 0).nodeValue();
}

int XmlParser::getNbChild(TQDomNode balise)
{
	return balise.childNodes().count();
}

int XmlParser::getNbChild(TQDomNode balise, TQString name)
{
	if(balise.isElement())
		return balise.toElement().elementsByTagName(name).count();
	return -1;
}

TQString  XmlParser::getChildName(TQDomNode balise, int index)
{
	return balise.childNodes().item(index).nodeName();
}

TQString  XmlParser::getAttr(TQDomNode balise, TQString name) const
{
	if(balise.isElement())
		return balise.toElement().attributeNode(name).value();
	return TQString();
}
