/***********************************************************************
*           See Accompanying licence file QSAS_licence                 *
***********************************************************************/

#ifndef _QIE_VOTABLE_H_
#define _QIE_VOTABLE_H_

#include <list> //temp
#include <vector>
#include <qobject.h>
#include "qt_version.h"

#include "qt_version.h"

#if QT_VERSION < 0x040000
#include <qxml.h>
#else
#include <QtXml/QXmlDefaultHandler>
#include <QtXml/QXmlSimpleReader>
#endif

#include "qie.h"
#include "QObject.h"
#include "QdTime.h"

class VOTableParser : public QXmlDefaultHandler
{
    public:

        VOTableParser(): currentIndex(0), startIndex(-1), endIndex(-1){}
        
        bool startDocument()
        {
            inTR = false;
            inTD = false;
//             isStartTime=false;
//             intervals.clear();
            return true;
        }

        bool startElement( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
        {
            if( qName == "TR" )
            {
                inTR=true;
                currentIndex=0;
            }
            else
            {
                if(inTR && qName == "TD")
                {
                    inTD=true;
                    ++currentIndex;
                }
                else if(qName=="FIELD")
                {
                    ++currentIndex;
                    for( int i=0; i<atts.count(); i++ )
                    {
                        if(atts.localName(i) == "ucd" )
                        {
                            if(atts.value(i)=="time.start")
                            {
                                startIndex=currentIndex;
                            }
                            else if(atts.value(i)=="time.stop")
                            {
                                endIndex=currentIndex;
                            }
                        }
                    }
                }
            }

            return true;
        }
        
        bool characters ( const QString & ch )
        {
            if(inTD)
            {
                if(currentIndex==startIndex)
                {
                    temp_interval.set_start(QDOS::Time(std::string(ch.latin1())));
                    if(startIndex>endIndex)
                        table.push_back(temp_interval);
                }
                else if(currentIndex==endIndex)
                {
                    temp_interval.set_end(QDOS::Time(std::string(ch.latin1())));
                    if(endIndex>startIndex)
                        table.push_back(temp_interval);
                }
            }
            return true;
        }
        
        bool endElement ( const QString & namespaceURI, const QString & localName, const QString & qName )
        {
            if(qName == "TD")
            {
                inTD=false;
            }
            if( qName == "TR" )
            {
                inTR = false;
                inTD = false;
            }

            return true;
        }

//         std::list< std::vector<QString> > intervals;
        QDOS::QdTimeIntervalSeq table;
    private:

        bool inTR;
        bool inTD;
        int currentIndex;
        int startIndex;
        int endIndex;
        QDOS::QdTimeInterval temp_interval;

};

QDOS::QdTimeIntervalSeq_var QiLoadVOTable(QString& fileName);
bool QiSaveVOTable(QDOS::QdTimeIntervalSeq_var& table, QString& fileName);

#endif
