Coverage Report - org.apache.xmlrpc.serializer.MapSerializer
 
Classes in this File Line Coverage Branch Coverage Complexity
MapSerializer
96% 
100% 
1,75
 
 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.serializer;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.xmlrpc.common.TypeFactory;
 22  
 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
 23  
 import org.xml.sax.ContentHandler;
 24  
 import org.xml.sax.SAXException;
 25  
 
 26  
 
 27  
 /** A {@link TypeSerializer} for maps.
 28  
  */
 29  
 public class MapSerializer extends TypeSerializerImpl {
 30  
         private final XmlRpcStreamConfig config;
 31  
         private final TypeFactory typeFactory;
 32  
         /** Tag name of a maps struct tag.
 33  
          */
 34  
         public static final String STRUCT_TAG = "struct";
 35  
         /** Tag name of a maps member tag.
 36  
          */
 37  
         public static final String MEMBER_TAG = "member";
 38  
         /** Tag name of a maps members name tag.
 39  
          */
 40  
         public static final String NAME_TAG = "name";
 41  
         /** Creates a new instance.
 42  
          * @param pTypeFactory The factory being used for creating serializers.
 43  
          * @param pConfig The configuration being used for creating serializers.
 44  
          */
 45  33
         public MapSerializer(TypeFactory pTypeFactory, XmlRpcStreamConfig pConfig) {
 46  33
                 typeFactory = pTypeFactory;
 47  33
                 config = pConfig;
 48  33
         }
 49  
         protected void writeEntry(ContentHandler pHandler, String pKey, Object pValue) throws SAXException {
 50  98
                 pHandler.startElement("", MEMBER_TAG, MEMBER_TAG, ZERO_ATTRIBUTES);
 51  98
                 pHandler.startElement("", NAME_TAG, NAME_TAG, ZERO_ATTRIBUTES);
 52  98
                 pHandler.characters(pKey.toCharArray(), 0, pKey.length());
 53  98
                 pHandler.endElement("", NAME_TAG, NAME_TAG);
 54  98
                 TypeSerializer ts = typeFactory.getSerializer(config, pValue);
 55  98
                 if (ts == null) {
 56  0
                         throw new SAXException("Unsupported Java type: " + pValue.getClass().getName());
 57  
                 }
 58  98
                 ts.write(pHandler, pValue);
 59  98
                 pHandler.endElement("", MEMBER_TAG, MEMBER_TAG);
 60  98
         }
 61  
         protected void writeData(ContentHandler pHandler, Object pData) throws SAXException {
 62  33
                 Map map = (Map) pData;
 63  164
                 for (Iterator iter = map.entrySet().iterator();  iter.hasNext();  ) {
 64  98
                         Map.Entry entry = (Map.Entry) iter.next();
 65  98
                         writeEntry(pHandler, entry.getKey().toString(), entry.getValue());
 66  
                 }
 67  33
         }
 68  
         public void write(final ContentHandler pHandler, Object pObject) throws SAXException {
 69  33
                 pHandler.startElement("", VALUE_TAG, VALUE_TAG, ZERO_ATTRIBUTES);
 70  33
                 pHandler.startElement("", STRUCT_TAG, STRUCT_TAG, ZERO_ATTRIBUTES);
 71  33
                 writeData(pHandler, pObject);
 72  33
                 pHandler.endElement("", STRUCT_TAG, STRUCT_TAG);
 73  33
                 pHandler.endElement("", VALUE_TAG, VALUE_TAG);
 74  33
         }
 75  
 }