/*************************************************************************
* @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
*
* Plugin for PHP unit Testing.
* www.phpeclipse.de
*
*************************************************************************/
package net.sourceforge.phpeclipse.phpunit.reporthandling;
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 net.sourceforge.phpeclipse.phpunit.PHPUnitView;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLReportHandler extends DefaultHandler {
private String currentVerdict;
private PHPUnitView view;
private String currentCommand;
private String currentTestCount;
private String currentTestID;
private String currentTestName;
private String currentTestParentTestSuiteName;
private void doAsyncRunnable(Runnable runnable) {
view.getSite().getShell().getDisplay().asyncExec(runnable);
}
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("tmp3.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.equals("report")) {
currentCommand = attributes.getValue("command");
currentTestCount = attributes.getValue("testCount");
currentTestID = attributes.getValue("testID");
currentTestName = attributes.getValue("testName");
currentTestParentTestSuiteName = attributes
.getValue("parentTestSuiteName");
doAsyncRunnable(new Runnable() {
public void run() {
// view.handleCommand(currentCommand, currentTestCount,
// currentTestID, );
view.handleCommand(currentCommand, new String[] {
currentTestID, currentTestCount, currentTestName,
currentTestParentTestSuiteName });
}
});
} else if (elementName.equals("verdict")) {
currentVerdict = attributes.getValue("desc");
// view.setTestVerdict(currentTestID, currentVerdict);
doAsyncRunnable(new Runnable() {
public void run() {
view.setTestVerdict(currentTestID, currentVerdict);
}
});
} else if (elementName.equals("exceptions")) {
// do nothing
} else if (elementName.equals("exception")) {
final String exception = attributes.getValue("desc");
doAsyncRunnable(new Runnable() {
public void run() {
view.addTestException(currentTestID, 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);
}
}