1 20 package org.apache.cactus.internal.client; 21 22 import org.apache.cactus.internal.WebTestResult; 23 24 32 public class WebTestResultParser 33 { 34 37 protected String exceptionClassname; 38 39 42 protected String exceptionMessage; 43 44 47 protected String exceptionStacktrace; 48 49 57 public WebTestResult parse(String theData) throws ParsingException 58 { 59 String 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 87 protected String readRootElement(String theData) throws ParsingException 88 { 89 String startRootString = "<" + WebTestResult.XML_ROOT_ELEMENT + ">"; 90 String endRootString = "</" + WebTestResult.XML_ROOT_ELEMENT + ">"; 91 String buffer; 92 93 String 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 120 protected String readExceptionClassname(String theData) 121 throws ParsingException 122 { 123 String startString = "<" + WebTestResult.XML_EXCEPTION_ELEMENT + " " 124 + WebTestResult.XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\""; 125 String endString = "</" + WebTestResult.XML_EXCEPTION_ELEMENT + ">"; 126 String 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 154 protected String readExceptionMessage(String theData) 155 throws ParsingException 156 { 157 String startString = "<" + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT 158 + "><![CDATA["; 159 String endString = "]]></" 160 + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT + ">"; 161 String 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 187 protected String readExceptionStacktrace(String theData) 188 throws ParsingException 189 { 190 String startString = "<" 191 + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + "><![CDATA["; 192 String endString = "]]></" 193 + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + ">"; 194 String 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 217 private String formatError(String 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 |