package net.sourceforge.phpeclipse.phpunit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLReportHandler extends DefaultHandler {
	
	private PHPUnitView view;
	
	private String currentCommand;
	private String currentTestCount;
	private String currentTestID;
	
	public void handle(String report, PHPUnitView view) {
		//TODO : how to parse directly a string?
		// now doing it with a stream.
		this.view = view;
		SAXParser parser;
		
		System.out.println("handling: " + report);
		
		try {
			
			File file = new File("tmp2.xml");
			FileOutputStream out = null;
			FileInputStream in = null;
			out = new FileOutputStream(file);
			//OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
			report += "\n \r";
			out.write(report.getBytes("UTF8"));
			out.close();
			in = new FileInputStream(file);
			parser = SAXParserFactory.newInstance().newSAXParser();
			parser.parse(in, this);
			in.close();
			file.delete();
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String arg0, String arg1, String elementName)
		throws SAXException {
		
		// send this current command to view	
	}
	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(
		String arg0,
		String arg1,
		String elementName,
		Attributes attributes)
		throws SAXException {
			
		System.out.println(arg0 + " - " + arg1 + " - " + elementName);	
		
		
		if(elementName == "report") {
			
			currentCommand = attributes.getValue("command");
			currentTestCount = attributes.getValue("testCount");
			currentTestID = attributes.getValue("testID");
			
			//view.handleCommand(currentCommand, currentTestCount, currentTestID);
			
			if (currentCommand == "testStarted") {
				
				//view.createNewTest("testName", currentTestID);
				//view.markTestStarted(currentTestID);
				
			} else if (currentCommand == "testFinished") {
				
				// do nothing wait for verdict
			} else if (currentCommand == "endAll") {
				
				//view.markTestingFinished();
			}
			
		} else if (elementName == "verdict") {
			
			String verdict = attributes.getValue("desc");
			
//			if( verdict == "passed") 
//				view.markTestPassed(currentTestID);
//			else
//				view.markTestFail(currentTestID);
			
		} else if (elementName == "exceptions") {
			
			
		} else if (elementName == "exception") {
			
		}
		
		
	}
	public static void main(String[] args) {
		
		XMLReportHandler handler = new XMLReportHandler();
		String xml = "";
		xml = "   ";
		handler.handle(xml, null);	
		
		xml = "   ";
		handler.handle(xml, null);
		
		xml = "  ";
		handler.handle(xml, null);
		
		xml = "  ";
		handler.handle(xml, null);		
	}
}