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 PHPUnitView view;
 
  22         private String currentCommand;
 
  23         private String currentTestCount;
 
  24         private String currentTestID;
 
  26         public void handle(String report, PHPUnitView view) {
 
  28                 //TODO : how to parse directly a string?
 
  29                 // now doing it with a stream.
 
  33                 System.out.println("handling: " + report);
 
  37                         File file = new File("tmp2.xml");
 
  38                         FileOutputStream out = null;
 
  39                         FileInputStream in = null;
 
  40                         out = new FileOutputStream(file);
 
  41                         //OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
 
  43                         out.write(report.getBytes("UTF8"));
 
  45                         in = new FileInputStream(file);
 
  46                         parser = SAXParserFactory.newInstance().newSAXParser();
 
  47                         parser.parse(in, this);
 
  51                 } catch (ParserConfigurationException e) {
 
  52                         // TODO Auto-generated catch block
 
  54                 } catch (SAXException e) {
 
  55                         // TODO Auto-generated catch block
 
  57                 } catch (FactoryConfigurationError e) {
 
  58                         // TODO Auto-generated catch block
 
  60                 }  catch (FileNotFoundException e1) {
 
  61                         // TODO Auto-generated catch block
 
  63                 } catch (IOException e) {
 
  64                         // TODO Auto-generated catch block
 
  73          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
 
  75         public void endElement(String arg0, String arg1, String elementName)
 
  78                 // send this current command to view    
 
  83          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
 
  85         public void startElement(
 
  89                 Attributes attributes)
 
  92                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);  
 
  95                 if(elementName == "report") {
 
  97                         currentCommand = attributes.getValue("command");
 
  98                         currentTestCount = attributes.getValue("testCount");
 
  99                         currentTestID = attributes.getValue("testID");
 
 101                         //view.handleCommand(currentCommand, currentTestCount, currentTestID);
 
 103                         if (currentCommand == "testStarted") {
 
 105                                 //view.createNewTest("testName", currentTestID);
 
 106                                 //view.markTestStarted(currentTestID);
 
 108                         } else if (currentCommand == "testFinished") {
 
 110                                 // do nothing wait for verdict
 
 111                         } else if (currentCommand == "endAll") {
 
 113                                 //view.markTestingFinished();
 
 116                 } else if (elementName == "verdict") {
 
 118                         String verdict = attributes.getValue("desc");
 
 120 //                      if( verdict == "passed") 
 
 121 //                              view.markTestPassed(currentTestID);
 
 123 //                              view.markTestFail(currentTestID);
 
 125                 } else if (elementName == "exceptions") {
 
 128                 } else if (elementName == "exception") {
 
 135         public static void main(String[] args) {
 
 137                 XMLReportHandler handler = new XMLReportHandler();
 
 139                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
 
 140                 handler.handle(xml, null);      
 
 142                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
 
 143                 handler.handle(xml, null);
 
 145                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
 
 146                 handler.handle(xml, null);
 
 148                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
 
 149                 handler.handle(xml, null);