Coverage Report - org.apache.xmlrpc.parser.XmlRpcResponseParser
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcResponseParser
36% 
78% 
5,875
 
 1  
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.parser;
 17  
 
 18  
 import java.util.Map;
 19  
 
 20  
 import javax.xml.namespace.QName;
 21  
 
 22  
 import org.apache.ws.commons.util.NamespaceContextImpl;
 23  
 import org.apache.xmlrpc.common.TypeFactory;
 24  
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 25  
 
 26  
 import org.xml.sax.Attributes;
 27  
 import org.xml.sax.SAXException;
 28  
 import org.xml.sax.SAXParseException;
 29  
 
 30  
 
 31  
 /** A SAX parser for an {@link org.apache.xmlrpc.server.XmlRpcServer}'s
 32  
  * response.
 33  
  */
 34  
 public class XmlRpcResponseParser extends RecursiveTypeParserImpl {
 35  
         private int level;
 36  
         private boolean isSuccess;
 37  
         private int errorCode;
 38  
         private String errorMessage;
 39  
 
 40  
         /** Creates a new instance.
 41  
          * @param pConfig The response configuration.
 42  
          * @param pTypeFactory The type factory for creating instances of
 43  
          * {@link TypeParser}.
 44  
          */
 45  
         public XmlRpcResponseParser(XmlRpcStreamRequestConfig pConfig,
 46  
                                                                 TypeFactory pTypeFactory) {
 47  280
                 super(pConfig, new NamespaceContextImpl(), pTypeFactory);
 48  280
         }
 49  
 
 50  
         protected void addResult(Object pResult) throws SAXException {
 51  280
                 if (isSuccess) {
 52  280
                         super.setResult(pResult);
 53  
                 } else {
 54  0
                         Map map = (Map) pResult;
 55  0
                         Integer faultCode = (Integer) map.get("faultCode");
 56  0
                         if (faultCode == null) {
 57  0
                                 throw new SAXParseException("Missing faultCode", getDocumentLocator());
 58  
                         }
 59  
                         try {
 60  0
                                 errorCode = faultCode.intValue();
 61  0
                         } catch (NumberFormatException e) {
 62  0
                                 throw new SAXParseException("Invalid faultCode: " + faultCode,
 63  0
                                                                                         getDocumentLocator());
 64  
                         }
 65  0
                         errorMessage = (String) map.get("faultString");
 66  
                 }
 67  280
         }
 68  
 
 69  
         public void startDocument() throws SAXException {
 70  280
                 super.startDocument();
 71  280
                 level = 0;
 72  280
         }
 73  
 
 74  
         public void startElement(String pURI, String pLocalName, String pQName,
 75  
                                                          Attributes pAttrs) throws SAXException {
 76  1744
                 switch (level++) {
 77  
                         case 0:
 78  280
                                 if (!"".equals(pURI)  ||  !"methodResponse".equals(pLocalName)) {
 79  0
                                         throw new SAXParseException("Expected methodResponse element, got "
 80  0
                                                                                                 + new QName(pURI, pLocalName),
 81  0
                                                                                                 getDocumentLocator());
 82  
                                 }
 83  0
                                 break;
 84  
                         case 1:
 85  280
                                 if ("".equals(pURI)  &&  "params".equals(pLocalName)) {
 86  280
                                         isSuccess = true;
 87  0
                                 } else if ("".equals(pURI)  &&  "fault".equals(pLocalName)) {
 88  0
                                         isSuccess = false;
 89  
                                 } else {
 90  0
                                         throw new SAXParseException("Expected params or fault element, got "
 91  0
                                                                                                 + new QName(pURI, pLocalName),
 92  0
                                                                                                 getDocumentLocator());
 93  
                                 }
 94  0
                                 break;
 95  
                         case 2:
 96  280
                                 if (isSuccess) {
 97  280
                                         if (!"".equals(pURI)  ||  !"param".equals(pLocalName)) {
 98  0
                                                 throw new SAXParseException("Expected param element, got "
 99  0
                                                                                                         + new QName(pURI, pLocalName),
 100  0
                                                                                                         getDocumentLocator());
 101  
                                         }
 102  
                                 } else {
 103  0
                                         if ("".equals(pURI)  &&  "value".equals(pLocalName)) {
 104  0
                                                 startValueTag();
 105  
                                         } else {
 106  0
                                                 throw new SAXParseException("Expected value element, got "
 107  0
                                                                                                         + new QName(pURI, pLocalName),
 108  0
                                                                                                         getDocumentLocator());
 109  
                                         }
 110  
                                 }
 111  0
                                 break;
 112  
                         case 3:
 113  280
                                 if (isSuccess) {
 114  280
                                         if ("".equals(pURI)  &&  "value".equals(pLocalName)) {
 115  280
                                                 startValueTag();
 116  
                                         } else {
 117  0
                                                 throw new SAXParseException("Expected value element, got "
 118  0
                                                                 + new QName(pURI, pLocalName),
 119  0
                                                                 getDocumentLocator());
 120  
                                         }
 121  
                                 } else {
 122  0
                                         super.startElement(pURI, pLocalName, pQName, pAttrs);
 123  
                                 }
 124  0
                                 break;
 125  
                         default:
 126  624
                                 super.startElement(pURI, pLocalName, pQName, pAttrs);
 127  
                                 break;
 128  
                 }
 129  1744
         }
 130  
 
 131  
         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
 132  1744
                 switch (--level) {
 133  
                         case 0:
 134  280
                                 if (!"".equals(pURI)  ||  !"methodResponse".equals(pLocalName)) {
 135  0
                                         throw new SAXParseException("Expected /methodResponse element, got "
 136  0
                                                                                                 + new QName(pURI, pLocalName),
 137  0
                                                                                                 getDocumentLocator());
 138  
                                 }
 139  0
                                 break;
 140  
                         case 1:
 141  
                                 {
 142  
                                         String tag;
 143  280
                                         if (isSuccess) {
 144  280
                                                 tag = "params";
 145  
                                         } else {
 146  0
                                                 tag = "fault";
 147  
                                         }
 148  280
                                         if (!"".equals(pURI)  ||  !tag.equals(pLocalName)) {
 149  0
                                                 throw new SAXParseException("Expected /" + tag + " element, got "
 150  0
                                                                 + new QName(pURI, pLocalName),
 151  0
                                                                 getDocumentLocator());
 152  
                                         }
 153  0
                                         break;
 154  
                                 }
 155  
                         case 2:
 156  280
                                 if (isSuccess) {
 157  280
                                         if (!"".equals(pURI)  ||  !"param".equals(pLocalName)) {
 158  0
                                                 throw new SAXParseException("Expected /param, got "
 159  0
                                                                                                         + new QName(pURI, pLocalName),
 160  0
                                                                                                         getDocumentLocator());
 161  
                                         }
 162  
                                 } else {
 163  0
                                         if ("".equals(pURI)  &&  "value".equals(pLocalName)) {
 164  0
                                                 endValueTag();
 165  
                                         } else {
 166  0
                                                 throw new SAXParseException("Expected /value, got "
 167  0
                                                                 + new QName(pURI, pLocalName),
 168  0
                                                                 getDocumentLocator());
 169  
                                         }
 170  
                                 }
 171  0
                                 break;
 172  
                         case 3:
 173  280
                                 if (isSuccess) {
 174  280
                                         if ("".equals(pURI)  &&  "value".equals(pLocalName)) {
 175  280
                                                 endValueTag();
 176  
                                         } else {
 177  0
                                                 throw new SAXParseException("Expected /value, got "
 178  0
                                                                 + new QName(pURI, pLocalName),
 179  0
                                                                 getDocumentLocator());
 180  
                                         }
 181  
                                 } else {
 182  0
                                         super.endElement(pURI, pLocalName, pQName);
 183  
                                 }
 184  0
                                 break;
 185  
                         default:
 186  624
                                 super.endElement(pURI, pLocalName, pQName);
 187  
                                 break;
 188  
                 }
 189  1744
         }
 190  
 
 191  
         /** Returns whether the response returned success. If so, the
 192  
          * result object may be fetched using {@link #getResult()}.
 193  
          * Otherwise, you may use the methods
 194  
          * {@link #getErrorCode()} and {@link #getErrorMessage()} to
 195  
          * check for error reasons.
 196  
          * @return True, if the response indicated success, false otherwise.
 197  
          */
 198  280
         public boolean isSuccess() { return isSuccess; }
 199  
 
 200  
         /** If the response contained a fault, returns the error code.
 201  
          * @return The numeric error code.
 202  
          */
 203  0
         public int getErrorCode() { return errorCode; }
 204  
 
 205  
         /** If the response contained a fault, returns the error message.
 206  
          * @return The error message.
 207  
          */
 208  0
         public String getErrorMessage() { return errorMessage; }
 209  
 }