00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.parser;
00020
00021 import java.io.ByteArrayInputStream;
00022 import java.io.ObjectInputStream;
00023 import java.util.Map;
00024
00025 import javax.xml.namespace.QName;
00026
00027 import org.apache.ws.commons.util.NamespaceContextImpl;
00028 import org.apache.xmlrpc.common.TypeFactory;
00029 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
00030
00031 import org.xml.sax.Attributes;
00032 import org.xml.sax.SAXException;
00033 import org.xml.sax.SAXParseException;
00034
00035
00039 public class XmlRpcResponseParser extends RecursiveTypeParserImpl {
00040 private int level;
00041 private boolean isSuccess;
00042 private int errorCode;
00043 private String errorMessage;
00044 private Throwable errorCause;
00045
00051 public XmlRpcResponseParser(XmlRpcStreamRequestConfig pConfig,
00052 TypeFactory pTypeFactory) {
00053 super(pConfig, new NamespaceContextImpl(), pTypeFactory);
00054 }
00055
00056 protected void addResult(Object pResult) throws SAXException {
00057 if (isSuccess) {
00058 super.setResult(pResult);
00059 } else {
00060 Map map = (Map) pResult;
00061 Integer faultCode = (Integer) map.get("faultCode");
00062 if (faultCode == null) {
00063 throw new SAXParseException("Missing faultCode", getDocumentLocator());
00064 }
00065 try {
00066 errorCode = faultCode.intValue();
00067 } catch (NumberFormatException e) {
00068 throw new SAXParseException("Invalid faultCode: " + faultCode,
00069 getDocumentLocator());
00070 }
00071 errorMessage = (String) map.get("faultString");
00072 Object exception = map.get("faultCause");
00073 if (exception != null) {
00074 try {
00075 byte[] bytes = (byte[]) exception;
00076 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
00077 ObjectInputStream ois = new ObjectInputStream(bais);
00078 errorCause = (Throwable) ois.readObject();
00079 ois.close();
00080 bais.close();
00081 } catch (Throwable t) {
00082
00083 }
00084 }
00085 }
00086 }
00087
00088 public void startDocument() throws SAXException {
00089 super.startDocument();
00090 level = 0;
00091 isSuccess = false;
00092 errorCode = 0;
00093 errorMessage = null;
00094 }
00095
00096 public void startElement(String pURI, String pLocalName, String pQName,
00097 Attributes pAttrs) throws SAXException {
00098 switch (level++) {
00099 case 0:
00100 if (!"".equals(pURI) || !"methodResponse".equals(pLocalName)) {
00101 throw new SAXParseException("Expected methodResponse element, got "
00102 + new QName(pURI, pLocalName),
00103 getDocumentLocator());
00104 }
00105 break;
00106 case 1:
00107 if ("".equals(pURI) && "params".equals(pLocalName)) {
00108 isSuccess = true;
00109 } else if ("".equals(pURI) && "fault".equals(pLocalName)) {
00110 isSuccess = false;
00111 } else {
00112 throw new SAXParseException("Expected params or fault element, got "
00113 + new QName(pURI, pLocalName),
00114 getDocumentLocator());
00115 }
00116 break;
00117 case 2:
00118 if (isSuccess) {
00119 if (!"".equals(pURI) || !"param".equals(pLocalName)) {
00120 throw new SAXParseException("Expected param element, got "
00121 + new QName(pURI, pLocalName),
00122 getDocumentLocator());
00123 }
00124 } else {
00125 if ("".equals(pURI) && "value".equals(pLocalName)) {
00126 startValueTag();
00127 } else {
00128 throw new SAXParseException("Expected value element, got "
00129 + new QName(pURI, pLocalName),
00130 getDocumentLocator());
00131 }
00132 }
00133 break;
00134 case 3:
00135 if (isSuccess) {
00136 if ("".equals(pURI) && "value".equals(pLocalName)) {
00137 startValueTag();
00138 } else {
00139 throw new SAXParseException("Expected value element, got "
00140 + new QName(pURI, pLocalName),
00141 getDocumentLocator());
00142 }
00143 } else {
00144 super.startElement(pURI, pLocalName, pQName, pAttrs);
00145 }
00146 break;
00147 default:
00148 super.startElement(pURI, pLocalName, pQName, pAttrs);
00149 break;
00150 }
00151 }
00152
00153 public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
00154 switch (--level) {
00155 case 0:
00156 if (!"".equals(pURI) || !"methodResponse".equals(pLocalName)) {
00157 throw new SAXParseException("Expected /methodResponse element, got "
00158 + new QName(pURI, pLocalName),
00159 getDocumentLocator());
00160 }
00161 break;
00162 case 1:
00163 {
00164 String tag;
00165 if (isSuccess) {
00166 tag = "params";
00167 } else {
00168 tag = "fault";
00169 }
00170 if (!"".equals(pURI) || !tag.equals(pLocalName)) {
00171 throw new SAXParseException("Expected /" + tag + " element, got "
00172 + new QName(pURI, pLocalName),
00173 getDocumentLocator());
00174 }
00175 break;
00176 }
00177 case 2:
00178 if (isSuccess) {
00179 if (!"".equals(pURI) || !"param".equals(pLocalName)) {
00180 throw new SAXParseException("Expected /param, got "
00181 + new QName(pURI, pLocalName),
00182 getDocumentLocator());
00183 }
00184 } else {
00185 if ("".equals(pURI) && "value".equals(pLocalName)) {
00186 endValueTag();
00187 } else {
00188 throw new SAXParseException("Expected /value, got "
00189 + new QName(pURI, pLocalName),
00190 getDocumentLocator());
00191 }
00192 }
00193 break;
00194 case 3:
00195 if (isSuccess) {
00196 if ("".equals(pURI) && "value".equals(pLocalName)) {
00197 endValueTag();
00198 } else {
00199 throw new SAXParseException("Expected /value, got "
00200 + new QName(pURI, pLocalName),
00201 getDocumentLocator());
00202 }
00203 } else {
00204 super.endElement(pURI, pLocalName, pQName);
00205 }
00206 break;
00207 default:
00208 super.endElement(pURI, pLocalName, pQName);
00209 break;
00210 }
00211 }
00212
00220 public boolean isSuccess() { return isSuccess; }
00221
00225 public int getErrorCode() { return errorCode; }
00226
00230 public String getErrorMessage() { return errorMessage; }
00231
00235 public Throwable getErrorCause() { return errorCause; }
00236 }