KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > logger > XMLTestCaseGenerator


1 /*
2  * ====================================================================
3  *
4  * Copyright 1999-2002 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20  
21 package org.apache.slide.webdav.logger;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.slide.webdav.util.WebdavStatus;
29 import org.jdom.CDATA;
30 import org.jdom.Document;
31 import org.jdom.Element;
32 import org.jdom.output.XMLOutputter;
33
34  
35 /**
36  * This class writes the header, the command and the body of an HTML request
37  * or response in an XML structure.
38  *
39  */

40
41
42 public class XMLTestCaseGenerator {
43     
44
45     /** The XML outputter */
46     private XMLOutputter xmlOut = new XMLOutputter(org.jdom.output.Format.getPrettyFormat()); // indent: 2 spaces, newlines=true
47

48     
49     private XHttpServletRequestFacade request;
50     private XHttpServletResponseFacade response;
51     
52     private Element root = new Element( "test" );
53     private Document doc = new Document( root );
54     
55         
56     /*
57      * Name of the thread
58      */

59     private String JavaDoc threadName;
60     
61     public void setThreadName( String JavaDoc threadName ) {
62         this.threadName = threadName;
63     }
64     public String JavaDoc getThreadName() {
65         return threadName;
66     }
67     
68         
69     
70     /*
71      * Constructs an XMLTestCaseGenerator object.
72      */

73     public XMLTestCaseGenerator( XHttpServletRequestFacade request, XHttpServletResponseFacade response){
74         this.request = request;
75         this.response = response;
76     }
77     
78     /*
79      * this method writes the data as XML.
80      */

81     public String JavaDoc toString() {
82         String JavaDoc result = "";
83             
84         root.addContent( printXMLstep() );
85         try {
86             result = xmlOut.outputString( doc.getRootElement().getChild("step") );
87         } catch ( Exception JavaDoc e ) {
88             e.printStackTrace();
89         }
90         return result;
91     }
92
93     /*
94      * this method prints the XML step attribute.
95      */

96     private Element printXMLstep() {
97         Element stepElem = new Element( "step" );
98 // stepElem.setAttribute(new Attribute("executeIn", getThreadName()));
99
stepElem.addContent( printXMLrequest() );
100         stepElem.addContent( printXMLresponse() );
101         return stepElem;
102     }
103     /*
104      * this method prints the XML request attribute.
105      */

106     private Element printXMLrequest() {
107         Element stepElem = new Element( "request" );
108         stepElem.addContent( printXMLrequestCommand() );
109         Iterator JavaDoc it = printXMLrequestHeaders();
110         while ( it.hasNext() ) {
111             stepElem.addContent( (Element)it.next() );
112         }
113         stepElem.addContent( printXMLrequestBody() );
114         return stepElem;
115     }
116                                   
117     /*
118      * this method prints the XML request attribute.
119      */

120     private Element printXMLresponse() {
121         Element respElem = new Element( "response" );
122         respElem.addContent( printXMLresponseCommand() );
123         Iterator JavaDoc it = printXMLresponseHeaders();
124         while ( it.hasNext() ) {
125             respElem.addContent( (Element)it.next() );
126         }
127         respElem.addContent( printXMLresponseBody() );
128         return respElem;
129     }
130                                   
131     /*
132      * this method prints the XML request command attribute.
133      */

134     private Element printXMLrequestCommand() {
135         Element reqComElem = new Element( "command" );
136         reqComElem.addContent( request.getMethod() + " " + request.getRequestURI() + " " + getProtocol() );
137         return reqComElem;
138     }
139                                   
140                                   
141     /*
142      * this method prints the XML request header attributes.
143      */

144     private Iterator JavaDoc printXMLrequestHeaders() {
145         Vector JavaDoc vector = new Vector JavaDoc();
146         Enumeration JavaDoc e = request.getHeaderNames();
147         if ( e != null ) {
148             while ( e.hasMoreElements() ) {
149                 String JavaDoc headerName = (String JavaDoc)e.nextElement();
150                 String JavaDoc headerValue = request.getHeader(headerName);
151                 Element elem = new Element( "header" );
152                 elem.addContent( headerName + ": " + headerValue );
153                 vector.add( elem );
154             }
155         }
156         return vector.iterator();
157     }
158                                   
159     /*
160      * this method prints the XML request body attribute.
161      */

162     private Element printXMLrequestBody(){
163         Element bodyElem = new Element( "body" );
164         try {
165             bodyElem.addContent( new CDATA(request.getRequestBody()) );
166         }
167         catch (IOException JavaDoc e) {
168             e.printStackTrace();
169         }
170         return bodyElem;
171     }
172
173     /*
174      * this method prints the XML response command attribute.
175      */

176     private Element printXMLresponseCommand() {
177         Element respComElem = new Element( "command" );
178         respComElem.addContent(
179             getProtocol() + " " +
180             response.getStatus() + " " +
181             WebdavStatus.getStatusText(response.getStatus()));
182         return respComElem;
183     }
184         
185     /*
186      * Returns the protocol without a trailing CRLF
187      */

188     private String JavaDoc getProtocol() {
189         String JavaDoc result = request.getProtocol();
190         while (result.endsWith("\n")) {
191             result = result.substring(0, result.length()-"\n".length()-1);
192         }
193         return result;
194     }
195                                   
196     /*
197      * this method prints the XML response header attributes.
198      */

199     private Iterator JavaDoc printXMLresponseHeaders() {
200         Vector JavaDoc vector = new Vector JavaDoc();
201         XResponseHeader respHeader;
202         Enumeration JavaDoc e = response.getResponseHeaders();
203         if ( e != null ) {
204             while ( e.hasMoreElements() ) {
205                 Element elem = new Element( "header" );
206                 elem.addContent( ((XResponseHeader)e.nextElement()).toString() );
207                 vector.add( elem );
208             }
209         }
210         return vector.iterator();
211     }
212                                   
213     /*
214      * this method prints the XML response body attribute.
215      */

216     private Element printXMLresponseBody() {
217         Element bodyElem = new Element( "body" );
218         try {
219             bodyElem.addContent( new CDATA(response.getResponseBody()) );
220         } catch ( IOException JavaDoc e ) {
221             e.printStackTrace();
222         }
223         return bodyElem;
224     }
225
226 }
227
Popular Tags