KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > WebTestResultParser


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 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 package org.apache.cactus.internal.client;
21
22 import org.apache.cactus.internal.WebTestResult;
23
24 /**
25  * Parse a string representing a Test result and transform it into a
26  * <code>WebTestResult</code> object.
27  *
28  * @see WebTestResult
29  *
30  * @version $Id: WebTestResultParser.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
31  */

32 public class WebTestResultParser
33 {
34     /**
35      * Parsed exception class name
36      */

37     protected String JavaDoc exceptionClassname;
38
39     /**
40      * Parsed exception message
41      */

42     protected String JavaDoc exceptionMessage;
43
44     /**
45      * Parsed exception stack trace
46      */

47     protected String JavaDoc exceptionStacktrace;
48
49     /**
50      * Parse a string and transform it into a <code>WebTestResult</code> object.
51      *
52      * @param theData the string to parse
53      * @return the <code>WebTestResult</code> object corresponding to the data
54      * string
55      * @exception ParsingException if an error happens during parsing
56      */

57     public WebTestResult parse(String JavaDoc theData) throws ParsingException
58     {
59         String JavaDoc buffer;
60         WebTestResult result;
61
62         buffer = readRootElement(theData);
63
64         if (buffer.length() == 0)
65         {
66             result = new WebTestResult();
67         }
68         else
69         {
70             buffer = readExceptionClassname(buffer);
71             buffer = readExceptionMessage(buffer);
72             buffer = readExceptionStacktrace(buffer);
73             result = new WebTestResult(this.exceptionClassname,
74                 this.exceptionMessage, this.exceptionStacktrace);
75         }
76
77         return result;
78     }
79
80     /**
81      * Read the {@link WebTestResult#XML_ROOT_ELEMENT} portion.
82      *
83      * @param theData the string buffer to parse
84      * @return the string buffer minus what has been read
85      * @exception ParsingException if an error happens during parsing
86      */

87     protected String JavaDoc readRootElement(String JavaDoc theData) throws ParsingException
88     {
89         String JavaDoc startRootString = "<" + WebTestResult.XML_ROOT_ELEMENT + ">";
90         String JavaDoc endRootString = "</" + WebTestResult.XML_ROOT_ELEMENT + ">";
91         String JavaDoc buffer;
92
93         // It is possible that some end of line character are inserted at the
94
// end of the string. This is valid, which is why we trim the string
95
// before perfoming the checks.
96
String JavaDoc trimmedData = theData.trim();
97
98         if (trimmedData.startsWith(startRootString)
99             && trimmedData.endsWith(endRootString))
100         {
101             buffer = trimmedData.substring(startRootString.length(),
102                 trimmedData.length() - endRootString.length());
103         }
104         else
105         {
106             throw new ParsingException(formatError(theData));
107         }
108
109         return buffer;
110     }
111
112     /**
113      * Read the {@link WebTestResult#XML_EXCEPTION_CLASSNAME_ATTRIBUTE} portion
114      * and extract the exception classname.
115      *
116      * @param theData the string buffer to parse
117      * @return the string buffer minus what has been read
118      * @exception ParsingException if an error happens during parsing
119      */

120     protected String JavaDoc readExceptionClassname(String JavaDoc theData)
121         throws ParsingException
122     {
123         String JavaDoc startString = "<" + WebTestResult.XML_EXCEPTION_ELEMENT + " "
124             + WebTestResult.XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"";
125         String JavaDoc endString = "</" + WebTestResult.XML_EXCEPTION_ELEMENT + ">";
126         String JavaDoc buffer;
127
128         if (theData.startsWith(startString) && theData.endsWith(endString))
129         {
130             int pos = theData.indexOf('\"', startString.length());
131
132             this.exceptionClassname = theData.substring(startString.length(),
133                 pos);
134             buffer = theData.substring(startString.length()
135                 + this.exceptionClassname.length() + 2,
136                 theData.length() - endString.length());
137         }
138         else
139         {
140             throw new ParsingException(formatError(theData));
141         }
142
143         return buffer;
144     }
145
146     /**
147      * Read the {@link WebTestResult#XML_EXCEPTION_MESSAGE_ELEMENT} portion
148      * and extract the exception message.
149      *
150      * @param theData the string buffer to parse
151      * @return the string buffer minus what has been read
152      * @exception ParsingException if an error happens during parsing
153      */

154     protected String JavaDoc readExceptionMessage(String JavaDoc theData)
155         throws ParsingException
156     {
157         String JavaDoc startString = "<" + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT
158             + "><![CDATA[";
159         String JavaDoc endString = "]]></"
160             + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT + ">";
161         String JavaDoc buffer;
162
163         if (theData.startsWith(startString))
164         {
165             int pos = theData.indexOf(endString, startString.length());
166
167             this.exceptionMessage = theData.substring(startString.length(),
168                 pos);
169             buffer = theData.substring(pos + endString.length());
170         }
171         else
172         {
173             throw new ParsingException(formatError(theData));
174         }
175
176         return buffer;
177     }
178
179     /**
180      * Read the {@link WebTestResult#XML_EXCEPTION_STACKTRACE_ELEMENT} portion
181      * and extract the exception stacktrace.
182      *
183      * @param theData the string buffer to parse
184      * @return the string buffer minus what has been read
185      * @exception ParsingException if an error happens during parsing
186      */

187     protected String JavaDoc readExceptionStacktrace(String JavaDoc theData)
188         throws ParsingException
189     {
190         String JavaDoc startString = "<"
191             + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + "><![CDATA[";
192         String JavaDoc endString = "]]></"
193             + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + ">";
194         String JavaDoc buffer;
195
196         if (theData.startsWith(startString))
197         {
198             int pos = theData.indexOf(endString, startString.length());
199
200             this.exceptionStacktrace = theData.substring(startString.length(),
201                 pos);
202             buffer = theData.substring(pos + endString.length());
203         }
204         else
205         {
206             throw new ParsingException(formatError(theData));
207         }
208
209         return buffer;
210     }
211
212     /**
213      * @param theData the data to format
214      * @return the first 100 characters (or less if the data has fewer
215      * characters) of the invalid data as it can be very big
216      */

217     private String JavaDoc formatError(String JavaDoc theData)
218     {
219         int nbChars = theData.length() > 100 ? 100 : theData.length();
220
221         return "Not a valid response. First " + nbChars
222             + " characters of the reponse: ["
223             + theData.substring(0, nbChars) + "]";
224     }
225 }
226
Popular Tags