moving report handling related classes to own package + other minor changes.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / reporthandling / XMLReportHandler.java
1 /*************************************************************************
2  * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
3  *
4  * Plugin for PHP unit Testing.
5  * www.phpeclipse.de
6  * 
7  *************************************************************************/
8
9
10 package net.sourceforge.phpeclipse.phpunit.reporthandling;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17
18 import javax.xml.parsers.FactoryConfigurationError;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.parsers.SAXParser;
21 import javax.xml.parsers.SAXParserFactory;
22
23 import net.sourceforge.phpeclipse.phpunit.PHPUnitView;
24
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.DefaultHandler;
28
29 public class XMLReportHandler extends DefaultHandler {
30         
31         private String currentVerdict;
32         private PHPUnitView view;       
33         private String currentCommand;
34         private String currentTestCount;
35         private String currentTestID;
36         private String currentTestName;
37         private String currentTestParentTestSuiteName;
38         
39         private void doAsyncRunnable(Runnable runnable) {
40                 
41                 view.getSite().getShell().getDisplay().asyncExec(runnable);
42         }
43         
44         public void handle(String report, PHPUnitView view) {
45
46                 //TODO : how to parse directly a string?
47                 // now doing it with a stream.
48                 this.view = view;
49                 SAXParser parser;
50                 
51                 System.out.println("handling: " + report);
52                 
53                 try {
54                         
55                         File file = new File("tmp3.xml");
56                         FileOutputStream out = null;
57                         FileInputStream in = null;
58                         out = new FileOutputStream(file);
59                         //OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
60                         report += "\n \r";
61                         out.write(report.getBytes("UTF8"));
62                         out.close();
63                         in = new FileInputStream(file);
64                         parser = SAXParserFactory.newInstance().newSAXParser();
65                         parser.parse(in, this);
66                         in.close();
67                         file.delete();
68                         
69                 } catch (ParserConfigurationException e) {
70                         // TODO Auto-generated catch block
71                         e.printStackTrace();
72                 } catch (SAXException e) {
73                         // TODO Auto-generated catch block
74                         e.printStackTrace();
75                 } catch (FactoryConfigurationError e) {
76                         // TODO Auto-generated catch block
77                         e.printStackTrace();
78                 }  catch (FileNotFoundException e1) {
79                         // TODO Auto-generated catch block
80                         e1.printStackTrace();
81                 } catch (IOException e) {
82                         // TODO Auto-generated catch block
83                         e.printStackTrace();
84                 }
85
86                         
87                 
88         }
89         
90         /* (non-Javadoc)
91          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
92          */
93         public void endElement(String arg0, String arg1, String elementName)
94                 throws SAXException {
95                 
96                 // send this current command to view    
97
98         }
99
100         /* (non-Javadoc)
101          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
102          */
103         public void startElement(
104                 String arg0,
105                 String arg1,
106                 String elementName,
107                 Attributes attributes)
108                 throws SAXException {
109                         
110                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);  
111                 
112                 
113                 if(elementName.equals("report")) {
114                         
115                         currentCommand = attributes.getValue("command");
116                         currentTestCount = attributes.getValue("testCount");
117                         currentTestID = attributes.getValue("testID");
118                         currentTestName = attributes.getValue("testName");
119                         currentTestParentTestSuiteName = attributes.getValue("parentTestSuiteName");
120                         
121                         doAsyncRunnable(new Runnable() {
122                                 
123                                 public void run() {
124                                         
125                                         //view.handleCommand(currentCommand, currentTestCount, currentTestID, );
126                                         view.handleCommand(currentCommand, new String[] {currentTestID, currentTestCount,  currentTestName, currentTestParentTestSuiteName});           
127                                         
128                                 }                       
129                         });
130
131                 } else if (elementName.equals("verdict")) {
132                         
133                         currentVerdict = attributes.getValue("desc");                   
134                         //view.setTestVerdict(currentTestID, currentVerdict);
135                         
136                         doAsyncRunnable(new Runnable() {
137                                 
138                                 public void run() {
139                                         
140                                         view.setTestVerdict(currentTestID, currentVerdict);             
141                                 }
142                         
143                         });                     
144
145                 } else if (elementName.equals("exceptions")) {
146                         
147                         //do nothing
148                         
149                 } else if (elementName.equals("exception")) {
150                         
151                         final String exception = attributes.getValue("desc");
152                         
153                         doAsyncRunnable(new Runnable() {
154                                 
155                                 public void run() {
156                                         
157                                         view.addTestException(currentTestID, exception);        
158                                 }
159                         
160                         });                     
161                         
162                 }
163                 
164                 
165         }
166
167         public static void main(String[] args) {
168                 
169                 XMLReportHandler handler = new XMLReportHandler();
170                 String xml = "";
171                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
172                 handler.handle(xml, null);      
173                 
174                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
175                 handler.handle(xml, null);
176                 
177                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
178                 handler.handle(xml, null);
179                 
180                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
181                 handler.handle(xml, null);              
182         }
183
184 }