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);
115 } else if (elementName.equals("verdict")) {
117 currentVerdict = attributes.getValue("desc");
118 //view.setTestVerdict(currentTestID, currentVerdict);
120 doAsyncRunnable(new Runnable() {
124 view.setTestVerdict(currentTestID, currentVerdict);
129 } else if (elementName.equals("exceptions")) {
133 } else if (elementName.equals("exception")) {
135 final String exception = attributes.getValue("desc");
137 doAsyncRunnable(new Runnable() {
141 view.addTestException(currentTestID, exception);
151 public static void main(String[] args) {
153 XMLReportHandler handler = new XMLReportHandler();
155 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
156 handler.handle(xml, null);
158 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
159 handler.handle(xml, null);
161 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
162 handler.handle(xml, null);
164 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
165 handler.handle(xml, null);