Coverage Report - org.apache.xmlrpc.parser.MapParser
 
Classes in this File Line Coverage Branch Coverage Complexity
MapParser
66% 
93% 
4,714
 
 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.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 
 23  
 import org.apache.ws.commons.util.NamespaceContextImpl;
 24  
 import org.apache.xmlrpc.common.TypeFactory;
 25  
 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
 26  
 import org.apache.xmlrpc.serializer.MapSerializer;
 27  
 import org.apache.xmlrpc.serializer.TypeSerializerImpl;
 28  
 import org.xml.sax.Attributes;
 29  
 import org.xml.sax.SAXException;
 30  
 import org.xml.sax.SAXParseException;
 31  
 
 32  
 
 33  
 /** {@link org.apache.xmlrpc.parser.TypeParser} implementation
 34  
  * for maps.
 35  
  */
 36  
 public class MapParser extends RecursiveTypeParserImpl {
 37  32
         private int level = 0;
 38  
         private String name;
 39  
         private Map map;
 40  
         private boolean inName, inValue, doneValue;
 41  
 
 42  
         /** Creates a new instance.
 43  
          * @param pConfig The request or response configuration.
 44  
          * @param pContext The namespace context.
 45  
          * @param pFactory The factory.
 46  
          */
 47  
         public MapParser(XmlRpcStreamConfig pConfig,
 48  
                                          NamespaceContextImpl pContext,
 49  
                                          TypeFactory pFactory) {
 50  32
                 super(pConfig, pContext, pFactory);
 51  32
         }
 52  
 
 53  
         protected void addResult(Object pResult) throws SAXException {
 54  96
                 if (name == null) {
 55  0
                         throw new SAXParseException("Invalid state: Expected name",
 56  0
                                                                                 getDocumentLocator());
 57  
                 } else {
 58  96
                         if (map.containsKey(name)) {
 59  0
                                 throw new SAXParseException("Duplicate name: " + name,
 60  0
                                                                                         getDocumentLocator());
 61  
                         } else {
 62  96
                                 map.put(name, pResult);
 63  
                         }
 64  
                 }
 65  96
         }
 66  
 
 67  
         public void startDocument() throws SAXException {
 68  32
                 super.startDocument();
 69  32
                 level = 0;
 70  32
                 map = new HashMap();
 71  32
                 inValue = inName = false;
 72  32
         }
 73  
 
 74  
         public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
 75  192
                 if (inName) {
 76  96
                         String s = new String(pChars, pOffset, pLength);
 77  96
                         name = name == null ? s : name + s;
 78  
                 } else {
 79  96
                         super.characters(pChars, pOffset, pLength);
 80  
                 }
 81  192
         }
 82  
 
 83  
         public void ignorableWhitespace(char[] pChars, int pOffset, int pLength) throws SAXException {
 84  0
                 if (inName) {
 85  0
                         characters(pChars, pOffset, pLength);
 86  
                 } else {
 87  0
                         super.ignorableWhitespace(pChars, pOffset, pLength);
 88  
                 }
 89  0
         }
 90  
 
 91  
         public void startElement(String pURI, String pLocalName, String pQName,
 92  
                                                          Attributes pAttrs) throws SAXException {
 93  416
                 switch (level++) {
 94  
                         case 0:
 95  32
                                 if (!"".equals(pURI)  ||  !MapSerializer.STRUCT_TAG.equals(pLocalName)) {
 96  0
                                         throw new SAXParseException("Expected " + MapSerializer.STRUCT_TAG + ", got "
 97  0
                                                                                                 + new QName(pURI, pLocalName),
 98  0
                                                                                                 getDocumentLocator());
 99  
                                 }
 100  0
                                 break;
 101  
                         case 1:
 102  96
                                 if (!"".equals(pURI)  ||  !MapSerializer.MEMBER_TAG.equals(pLocalName)) {
 103  0
                                         throw new SAXParseException("Expected " + MapSerializer.MEMBER_TAG + ", got "
 104  0
                                                                                                 + new QName(pURI, pLocalName),
 105  0
                                                                                                 getDocumentLocator());
 106  
                                 }
 107  96
                                 doneValue = inName = inValue = false;
 108  96
                                 name = null;
 109  96
                                 break;
 110  
                         case 2:
 111  192
                                 if (doneValue) {
 112  0
                                         throw new SAXParseException("Expected /" + MapSerializer.MEMBER_TAG
 113  0
                                                                                                 + ", got " + new QName(pURI, pLocalName),
 114  0
                                                                                                 getDocumentLocator());
 115  
                                 }
 116  192
                                 if ("".equals(pURI)  &&  MapSerializer.NAME_TAG.equals(pLocalName)) {
 117  96
                                         if (name == null) {
 118  96
                                                 inName = true;
 119  
                                         } else {
 120  0
                                                 throw new SAXParseException("Expected " + TypeSerializerImpl.VALUE_TAG
 121  0
                                                                                                         + ", got " + new QName(pURI, pLocalName),
 122  0
                                                                                                         getDocumentLocator());
 123  
                                         }
 124  96
                                 } else if ("".equals(pURI)  &&  TypeSerializerImpl.VALUE_TAG.equals(pLocalName)) {
 125  96
                                         if (name == null) {
 126  0
                                                 throw new SAXParseException("Expected " + MapSerializer.NAME_TAG
 127  0
                                                                                                         + ", got " + new QName(pURI, pLocalName),
 128  0
                                                                                                         getDocumentLocator());
 129  
                                         } else {
 130  96
                                                 inValue = true;
 131  96
                                                 startValueTag();
 132  
                                         }
 133  
                                         
 134  
                                 }
 135  96
                                 break;
 136  
                         default:
 137  96
                                 super.startElement(pURI, pLocalName, pQName, pAttrs);
 138  
                                 break;
 139  
                 }
 140  416
         }
 141  
 
 142  
         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
 143  416
                 switch (--level) {
 144  
                         case 0:
 145  32
                                 setResult(map);
 146  32
                                 break;
 147  
                         case 1:
 148  96
                                 break;
 149  
                         case 2:
 150  192
                                 if (inName) {
 151  96
                                         inName = false;
 152  96
                                 } else if (inValue) {
 153  96
                                         endValueTag();
 154  96
                                         doneValue = true;
 155  
                                 }
 156  96
                                 break;
 157  
                         default:
 158  96
                                 super.endElement(pURI, pLocalName, pQName);
 159  
                 }
 160  416
         }
 161  
 }