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;
 
  25         private String currentTestName;
 
  26         private String currentTestParentTestSuiteName;
 
  28         private void doAsyncRunnable(Runnable runnable) {
 
  30                 view.getSite().getShell().getDisplay().asyncExec(runnable);
 
  33         public void handle(String report, PHPUnitView view) {
 
  35                 //TODO : how to parse directly a string?
 
  36                 // now doing it with a stream.
 
  40                 System.out.println("handling: " + report);
 
  44                         File file = new File("tmp3.xml");
 
  45                         FileOutputStream out = null;
 
  46                         FileInputStream in = null;
 
  47                         out = new FileOutputStream(file);
 
  48                         //OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
 
  50                         out.write(report.getBytes("UTF8"));
 
  52                         in = new FileInputStream(file);
 
  53                         parser = SAXParserFactory.newInstance().newSAXParser();
 
  54                         parser.parse(in, this);
 
  58                 } catch (ParserConfigurationException e) {
 
  59                         // TODO Auto-generated catch block
 
  61                 } catch (SAXException e) {
 
  62                         // TODO Auto-generated catch block
 
  64                 } catch (FactoryConfigurationError e) {
 
  65                         // TODO Auto-generated catch block
 
  67                 }  catch (FileNotFoundException e1) {
 
  68                         // TODO Auto-generated catch block
 
  70                 } catch (IOException e) {
 
  71                         // TODO Auto-generated catch block
 
  80          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
 
  82         public void endElement(String arg0, String arg1, String elementName)
 
  85                 // send this current command to view    
 
  90          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
 
  92         public void startElement(
 
  96                 Attributes attributes)
 
  99                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);  
 
 102                 if(elementName.equals("report")) {
 
 104                         currentCommand = attributes.getValue("command");
 
 105                         currentTestCount = attributes.getValue("testCount");
 
 106                         currentTestID = attributes.getValue("testID");
 
 107                         currentTestName = attributes.getValue("testName");
 
 108                         currentTestParentTestSuiteName = attributes.getValue("parentTestSuiteName");
 
 110                         doAsyncRunnable(new Runnable() {
 
 114                                         //view.handleCommand(currentCommand, currentTestCount, currentTestID, );
 
 115                                         view.handleCommand(currentCommand, new String[] {currentTestID, currentTestCount,  currentTestName, currentTestParentTestSuiteName});           
 
 120                 } else if (elementName.equals("verdict")) {
 
 122                         currentVerdict = attributes.getValue("desc");                   
 
 123                         //view.setTestVerdict(currentTestID, currentVerdict);
 
 125                         doAsyncRunnable(new Runnable() {
 
 129                                         view.setTestVerdict(currentTestID, currentVerdict);             
 
 134                 } else if (elementName.equals("exceptions")) {
 
 138                 } else if (elementName.equals("exception")) {
 
 140                         final String exception = attributes.getValue("desc");
 
 142                         doAsyncRunnable(new Runnable() {
 
 146                                         view.addTestException(currentTestID, exception);        
 
 156         public static void main(String[] args) {
 
 158                 XMLReportHandler handler = new XMLReportHandler();
 
 160                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
 
 161                 handler.handle(xml, null);      
 
 163                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
 
 164                 handler.handle(xml, null);
 
 166                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
 
 167                 handler.handle(xml, null);
 
 169                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
 
 170                 handler.handle(xml, null);