put connection Listener and report listener as seperate threads.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / XMLReportHandler.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8
9 import javax.xml.parsers.FactoryConfigurationError;
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.parsers.SAXParser;
12 import javax.xml.parsers.SAXParserFactory;
13
14 import org.xml.sax.Attributes;
15 import org.xml.sax.SAXException;
16 import org.xml.sax.helpers.DefaultHandler;
17
18 public class XMLReportHandler extends DefaultHandler {
19         
20         private PHPUnitView view;
21         
22         private String currentCommand;
23         private String currentTestCount;
24         private String currentTestID;
25         
26         public void handle(String report, PHPUnitView view) {
27
28                 //TODO : how to parse directly a string?
29                 // now doing it with a stream.
30                 this.view = view;
31                 SAXParser parser;
32                 
33                 System.out.println("handling: " + report);
34                 
35                 try {
36                         
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);
42                         report += "\n \r";
43                         out.write(report.getBytes("UTF8"));
44                         out.close();
45                         in = new FileInputStream(file);
46                         parser = SAXParserFactory.newInstance().newSAXParser();
47                         parser.parse(in, this);
48                         in.close();
49                         file.delete();
50                         
51                 } catch (ParserConfigurationException e) {
52                         // TODO Auto-generated catch block
53                         e.printStackTrace();
54                 } catch (SAXException e) {
55                         // TODO Auto-generated catch block
56                         e.printStackTrace();
57                 } catch (FactoryConfigurationError e) {
58                         // TODO Auto-generated catch block
59                         e.printStackTrace();
60                 }  catch (FileNotFoundException e1) {
61                         // TODO Auto-generated catch block
62                         e1.printStackTrace();
63                 } catch (IOException e) {
64                         // TODO Auto-generated catch block
65                         e.printStackTrace();
66                 }
67
68                         
69                 
70         }
71         
72         /* (non-Javadoc)
73          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
74          */
75         public void endElement(String arg0, String arg1, String elementName)
76                 throws SAXException {
77                 
78                 // send this current command to view    
79
80         }
81
82         /* (non-Javadoc)
83          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
84          */
85         public void startElement(
86                 String arg0,
87                 String arg1,
88                 String elementName,
89                 Attributes attributes)
90                 throws SAXException {
91                         
92                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);  
93                 
94                 
95                 if(elementName == "report") {
96                         
97                         currentCommand = attributes.getValue("command");
98                         currentTestCount = attributes.getValue("testCount");
99                         currentTestID = attributes.getValue("testID");
100                         
101                         //view.handleCommand(currentCommand, currentTestCount, currentTestID);
102                         
103                         if (currentCommand == "testStarted") {
104                                 
105                                 //view.createNewTest("testName", currentTestID);
106                                 //view.markTestStarted(currentTestID);
107                                 
108                         } else if (currentCommand == "testFinished") {
109                                 
110                                 // do nothing wait for verdict
111                         } else if (currentCommand == "endAll") {
112                                 
113                                 //view.markTestingFinished();
114                         }
115                         
116                 } else if (elementName == "verdict") {
117                         
118                         String verdict = attributes.getValue("desc");
119                         
120 //                      if( verdict == "passed") 
121 //                              view.markTestPassed(currentTestID);
122 //                      else
123 //                              view.markTestFail(currentTestID);
124                         
125                 } else if (elementName == "exceptions") {
126                         
127                         
128                 } else if (elementName == "exception") {
129                         
130                 }
131                 
132                 
133         }
134
135         public static void main(String[] args) {
136                 
137                 XMLReportHandler handler = new XMLReportHandler();
138                 String xml = "";
139                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
140                 handler.handle(xml, null);      
141                 
142                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
143                 handler.handle(xml, null);
144                 
145                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
146                 handler.handle(xml, null);
147                 
148                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
149                 handler.handle(xml, null);              
150         }
151
152 }