import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

import owql.jtp.*;

import org.jdom.Element;
import org.jdom.input.*;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.AbstractTransformer;
import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.SAXException;



/*
 * Created on 29.07.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Halgurt
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class OWQLTransformer extends AbstractTransformer {

    private String namespace;
	OWQLServerJTP owql;
	StringBuffer reqxml;
	boolean readOWQL;
	boolean transform;

	public OWQLTransformer (){

	namespace = "http://maria_sharapova.html";
	owql = new OWQLServerJTP ();
	readOWQL = false;
	transform    = false;
	reqxml = new StringBuffer ();
	}
	public Document transform(Document doc){
		return doc;
		}
    public void setup (SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException {

    }

    public void startElement (String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException {
        if (namespaceURI.equals (namespace) && localName.equals ("OWLQL")) {
            readOWQL = true;
            start = true;
        }

        else {
            super.startElement (namespaceURI, localName, qName, attributes);
        }

    }

    public void characters (char[] ch, int start, int length) {
        System.out.println(new String (ch, start, (ch.length)-start));
        System.out.println("Start: "+start+ "  Length: "+length+ "  ch-length: "+ch.length);


        if (readOWQL && (length != 0)) {
    		//System.out.println(super.builder.getDocument().getFirstChild().toString());
            //System.out.println(ch.toString());
            reqxml.append (ch, start, ch.length-start);
            //System.out.println(reqxml.toString());
            readOWQL = false;
        }
    }
    public void endElement (String namespaceURI, String localName, String qName) throws SAXException {
        if (namespaceURI.equals (namespace) && localName.equals ("OWLQL")) {
        	try {
            	StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                String query = (reqxml.toString()).substring (0, (reqxml.toString()).indexOf("</owql:OWLQL>")) 
                owql.printAnswersXML(query, pw);
                String ansstr = sw.toString();
                super.startElement("", "result", "result", (Attributes) new AttributesImpl());
                super.characters(ansstr.toCharArray(), 0, ansstr.length());
                super.endElement("", "result", "result");

                StringReader sr = new StringReader(ansstr);
                Element env = new SAXBuilder().build(sr).getRootElement();
            	}
            	catch (Exception e){
                    System.out.println ("An Error occured during Parameter instantiation ");
                    e.printStackTrace ();
            	}
            }
        }
}


