2f8b5aa673215a85ebec0157d4979669ab3d8fca
[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 String currentVerdict;
21         private PHPUnitView view;       
22         private String currentCommand;
23         private String currentTestCount;
24         private String currentTestID;
25         
26         private void doAsyncRunnable(Runnable runnable) {
27                 
28                 view.getSite().getShell().getDisplay().asyncExec(runnable);
29         }
30         
31         public void handle(String report, PHPUnitView view) {
32
33                 //TODO : how to parse directly a string?
34                 // now doing it with a stream.
35                 this.view = view;
36                 SAXParser parser;
37                 
38                 System.out.println("handling: " + report);
39                 
40                 try {
41                         
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);
47                         report += "\n \r";
48                         out.write(report.getBytes("UTF8"));
49                         out.close();
50                         in = new FileInputStream(file);
51                         parser = SAXParserFactory.newInstance().newSAXParser();
52                         parser.parse(in, this);
53                         in.close();
54                         file.delete();
55                         
56                 } catch (ParserConfigurationException e) {
57                         // TODO Auto-generated catch block
58                         e.printStackTrace();
59                 } catch (SAXException e) {
60                         // TODO Auto-generated catch block
61                         e.printStackTrace();
62                 } catch (FactoryConfigurationError e) {
63                         // TODO Auto-generated catch block
64                         e.printStackTrace();
65                 }  catch (FileNotFoundException e1) {
66                         // TODO Auto-generated catch block
67                         e1.printStackTrace();
68                 } catch (IOException e) {
69                         // TODO Auto-generated catch block
70                         e.printStackTrace();
71                 }
72
73                         
74                 
75         }
76         
77         /* (non-Javadoc)
78          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
79          */
80         public void endElement(String arg0, String arg1, String elementName)
81                 throws SAXException {
82                 
83                 // send this current command to view    
84
85         }
86
87         /* (non-Javadoc)
88          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
89          */
90         public void startElement(
91                 String arg0,
92                 String arg1,
93                 String elementName,
94                 Attributes attributes)
95                 throws SAXException {
96                         
97                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);  
98                 
99                 
100                 if(elementName.equals("report")) {
101                         
102                         currentCommand = attributes.getValue("command");
103                         currentTestCount = attributes.getValue("testCount");
104                         currentTestID = attributes.getValue("testID");
105                         
106                         doAsyncRunnable(new Runnable() {
107                                 
108                                 public void run() {
109                                         
110                                         view.handleCommand(currentCommand, currentTestCount, currentTestID);            
111                                 }
112                         
113                         });
114
115                 } else if (elementName.equals("verdict")) {
116                         
117                         currentVerdict = attributes.getValue("desc");                   
118                         //view.setTestVerdict(currentTestID, currentVerdict);
119                         
120                         doAsyncRunnable(new Runnable() {
121                                 
122                                 public void run() {
123                                         
124                                         view.setTestVerdict(currentTestID, currentVerdict);             
125                                 }
126                         
127                         });                     
128
129                 } else if (elementName.equals("exceptions")) {
130                         
131                         //do nothing
132                         
133                 } else if (elementName.equals("exception")) {
134                         
135                         final String exception = attributes.getValue("desc");
136                         
137                         doAsyncRunnable(new Runnable() {
138                                 
139                                 public void run() {
140                                         
141                                         view.addTestException(currentTestID, exception);        
142                                 }
143                         
144                         });                     
145                         
146                 }
147                 
148                 
149         }
150
151         public static void main(String[] args) {
152                 
153                 XMLReportHandler handler = new XMLReportHandler();
154                 String xml = "";
155                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
156                 handler.handle(xml, null);      
157                 
158                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
159                 handler.handle(xml, null);
160                 
161                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
162                 handler.handle(xml, null);
163                 
164                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
165                 handler.handle(xml, null);              
166         }
167
168 }