1 package net.sourceforge.phpeclipse.phpunit;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
9 import javax.xml.parsers.FactoryConfigurationError;
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.parsers.SAXParser;
12 import javax.xml.parsers.SAXParserFactory;
14 import org.xml.sax.Attributes;
15 import org.xml.sax.SAXException;
16 import org.xml.sax.helpers.DefaultHandler;
18 public class XMLReportHandler extends DefaultHandler {
20 private String currentVerdict;
21 private PHPUnitView view;
22 private String currentCommand;
23 private String currentTestCount;
24 private String currentTestID;
26 private void doAsyncRunnable(Runnable runnable) {
28 view.getSite().getShell().getDisplay().asyncExec(runnable);
31 public void handle(String report, PHPUnitView view) {
33 //TODO : how to parse directly a string?
34 // now doing it with a stream.
38 System.out.println("handling: " + report);
42 File file = new File("tmp3.xml");
43 FileOutputStream out = null;
44 FileInputStream in = null;
45 out = new FileOutputStream(file);
46 //OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
48 out.write(report.getBytes("UTF8"));
50 in = new FileInputStream(file);
51 parser = SAXParserFactory.newInstance().newSAXParser();
52 parser.parse(in, this);
56 } catch (ParserConfigurationException e) {
57 // TODO Auto-generated catch block
59 } catch (SAXException e) {
60 // TODO Auto-generated catch block
62 } catch (FactoryConfigurationError e) {
63 // TODO Auto-generated catch block
65 } catch (FileNotFoundException e1) {
66 // TODO Auto-generated catch block
68 } catch (IOException e) {
69 // TODO Auto-generated catch block
78 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
80 public void endElement(String arg0, String arg1, String elementName)
83 // send this current command to view
88 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
90 public void startElement(
94 Attributes attributes)
97 System.out.println(arg0 + " - " + arg1 + " - " + elementName);
100 if(elementName.equals("report")) {
102 currentCommand = attributes.getValue("command");
103 currentTestCount = attributes.getValue("testCount");
104 currentTestID = attributes.getValue("testID");
106 doAsyncRunnable(new Runnable() {
110 view.handleCommand(currentCommand, currentTestCount, currentTestID);
114 } else if (elementName.equals("verdict")) {
116 currentVerdict = attributes.getValue("desc");
117 //view.setTestVerdict(currentTestID, currentVerdict);
119 doAsyncRunnable(new Runnable() {
123 view.setTestVerdict(currentTestID, currentVerdict);
128 } else if (elementName.equals("exceptions")) {
132 } else if (elementName.equals("exception")) {
134 final String exception = attributes.getValue("desc");
136 doAsyncRunnable(new Runnable() {
140 view.addTestException(currentTestID, exception);
150 public static void main(String[] args) {
152 XMLReportHandler handler = new XMLReportHandler();
154 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
155 handler.handle(xml, null);
157 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
158 handler.handle(xml, null);
160 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
161 handler.handle(xml, null);
163 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
164 handler.handle(xml, null);