#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 "query.h"
#include "value.h"


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

    KAboutData about("treeedit", I18N_NOOP("TreeEdit"), "Pre-0.0",
                     I18N_NOOP("Demo XML Actions"),
                     KAboutData::License_Unknown,
                     I18N_NOOP("(c) 2002 Richard Moore"));
    KCmdLineArgs::init(argc, argv, &about);
    KCmdLineArgs::addCmdLineOptions( options );
    KApplication app;

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

    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QString fn = args->arg(0);
    QFile file( fn );
    if ( !file.open( IO_ReadOnly ) )
	return false;

    QTextStream ts( &file );

    html->begin();
    html->write( ts.read() );
    html->end();

    DOM::Document doc = html->document();
    
    printf("Entering interactive mode.\n"
	   "Use debug() to print to the console. Press C-d or C-c to exit.\n\n");

    char buffer[1000];
    FILE *in = fdopen(0, "r");
    
    while (1) {
	printf("XPath> ");
	if (!fgets(buffer, 999, in))
	    break;
	QString query( buffer );
	query = query.stripWhiteSpace();

	XPath::Query xp;
	XPath::Value v = xp.select( query, doc.documentElement() );
	XPath::String s = v.toString();

	kdDebug() << "Value: " << v.qstring() << endl;
	kdDebug() << "String: " << s.qstring() << endl;
    }
}



