#include <qfile.h>
#include <qtextstream.h>

#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <klocale.h>

#include <khtml_part.h>
#include <khtmlview.h>
#include <kparts/browserextension.h>
#include <kparts/part.h>

#include "dom/dom_doc.h"
#include "dom/dom_element.h"
#include "dom/dom_string.h"

#include "xpath_query.h"
#include "xpath_value.h"
#include "xpath_node.h"
#include "xpath_tests.h"

int main(int argc, char **argv)
{
    static KCmdLineOptions options[] = {
	{ "d", I18N_NOOP("Dump builtins"), 0 },
        { "+file", I18N_NOOP( "URL of a document to load" ), 0 },
        { 0, 0, 0 }
    };

    KAboutData about("xpathtest", I18N_NOOP("XPathTest"), "Pre-0.0",
                     I18N_NOOP("Test XPath"),
                     KAboutData::License_Unknown,
                     I18N_NOOP("(c) 2003 Richard Moore"));
    KCmdLineArgs::init(argc, argv, &about);
    KCmdLineArgs::addCmdLineOptions( options );
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    KApplication app;

    KHTMLPart *html = new KHTMLPart;
    KParts::URLArgs uargs( html->browserExtension()->urlArgs() );
    uargs.serviceType = "text/xml";
    html->browserExtension()->setURLArgs( uargs );

    QString doc;
    if ( args->count() ) {
	QString fn = args->arg(0);
	QFile file( fn );
	if ( !file.open( IO_ReadOnly ) )
	    return false;

	QTextStream ts( &file );
	doc = ts.read();
    }
    else {
	doc = QString("<test><item></item></test>");
    }

    html->begin();
    html->write( doc );
    html->end();

    kdDebug() << "Doc:" << endl << doc << endl;

    if ( args->isSet("d") )
	XPath::Tests::test_ast( html );
    else
	XPath::Tests::interactive( html );
}



