Coverage Report - org.apache.shindig.social.core.util.xstream.MapConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
MapConverter
68%
42/62
45%
9/20
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util.xstream;
 19  
 
 20  
 import com.thoughtworks.xstream.converters.MarshallingContext;
 21  
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 22  
 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
 23  
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 24  
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 25  
 import com.thoughtworks.xstream.mapper.Mapper;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 import java.util.Map;
 31  
 import java.util.Map.Entry;
 32  
 import java.util.concurrent.ConcurrentHashMap;
 33  
 
 34  
 /**
 35  
  * converts a map to and from the form <container>
 36  
  * &lt;key&gt;value&lt;/key&gt; &lt;key&gt;value&lt;/key&gt; <container>.
 37  
  */
 38  
 public class MapConverter extends AbstractCollectionConverter {
 39  
 
 40  
   /**
 41  
    * The logger.
 42  
    */
 43  1
   private static final Log log = LogFactory.getLog(MapConverter.class);
 44  
   /**
 45  
    * If true will use a short form of xml serialization.
 46  
    */
 47  222
   private boolean shortform = false;
 48  
 
 49  
   /**
 50  
    * Create a MapConverter that use use the supplied mapper.
 51  
    *
 52  
    * @param mapper
 53  
    *          the mapped to base the conversion on.
 54  
    */
 55  
   public MapConverter(Mapper mapper) {
 56  222
     super(mapper);
 57  222
   }
 58  
 
 59  
   /**
 60  
    * output the Map in the simplified form.
 61  
    *
 62  
    * @param source
 63  
    *          the object to be output
 64  
    * @param writer
 65  
    *          the writer to use to perform the output.
 66  
    * @param context
 67  
    *          the context in which to perform the output.
 68  
    *
 69  
    * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
 70  
    *      com.thoughtworks.xstream.io.HierarchicalStreamWriter,
 71  
    *      com.thoughtworks.xstream.converters.MarshallingContext)
 72  
    */
 73  
   public void marshal(Object source, HierarchicalStreamWriter writer,
 74  
       MarshallingContext context) {
 75  8
     Map<?, ?> map = (Map<?, ?>) source;
 76  8
     if (shortform) {
 77  0
       for (Entry<?, ?> e : map.entrySet()) {
 78  0
         writer.startNode(String.valueOf(e.getKey()));
 79  0
         context.convertAnother(e.getValue());
 80  0
         writer.endNode();
 81  0
       }
 82  0
     } else {
 83  8
       for (Entry<?, ?> e : map.entrySet()) {
 84  6
         writer.startNode("entry");
 85  6
         writer.startNode("key");
 86  6
         writer.setValue(String.valueOf(e.getKey()));
 87  6
         writer.endNode();
 88  6
         writer.startNode("value");
 89  6
         context.convertAnother(e.getValue());
 90  6
         writer.endNode();
 91  6
         writer.endNode();
 92  6
       }
 93  
 
 94  
     }
 95  8
   }
 96  
 
 97  
   /**
 98  
    * Convert a suitably positioned reader stream into a Map object.
 99  
    *
 100  
    * @param reader
 101  
    *          the stream reader positioned at the start of the object.
 102  
    * @param context
 103  
    *          the unmarshalling context.
 104  
    * @return the object representing the stream.
 105  
    * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
 106  
    *      .xstream.io.HierarchicalStreamReader,
 107  
    *      com.thoughtworks.xstream.converters.UnmarshallingContext)
 108  
    */
 109  
   public Object unmarshal(HierarchicalStreamReader reader,
 110  
       UnmarshallingContext context) {
 111  1
     Map<String, Object> m = new ConcurrentHashMap<String, Object>();
 112  1
     reader.moveDown();
 113  2
     while (reader.hasMoreChildren()) {
 114  1
       String key = reader.getNodeName();
 115  1
       if ("entry".equals(key)) {
 116  1
         Object value = null;
 117  1
         reader.moveDown();
 118  1
         String type = reader.getNodeName();
 119  1
         if ("key".equals(type)) {
 120  1
           key = reader.getValue();
 121  1
         } else {
 122  0
           if (reader.hasMoreChildren()) {
 123  0
             value = readItem(reader, context, m);
 124  0
           } else {
 125  0
             value = reader.getValue();
 126  
           }
 127  
         }
 128  1
         reader.moveUp();
 129  1
         reader.moveDown();
 130  1
         type = reader.getNodeName();
 131  1
         if ("key".equals(type)) {
 132  0
           key = reader.getValue();
 133  0
         } else {
 134  1
           if (reader.hasMoreChildren()) {
 135  0
             value = readItem(reader, context, m);
 136  0
           } else {
 137  1
             value = reader.getValue();
 138  
           }
 139  
         }
 140  1
         m.put(key, value);
 141  1
         reader.moveUp();
 142  1
       } else {
 143  0
         reader.moveDown();
 144  0
         if (reader.hasMoreChildren()) {
 145  0
           m.put(key, readItem(reader, context, m));
 146  0
         } else {
 147  0
           m.put(key, reader.getValue());
 148  
         }
 149  0
         reader.moveUp();
 150  
       }
 151  1
     }
 152  1
     reader.moveUp();
 153  1
     return m;
 154  
   }
 155  
 
 156  
   /**
 157  
    * Can this Converter convert the type supplied.
 158  
    *
 159  
    * @param clazz
 160  
    *          the type being converted.
 161  
    * @return true if the type supplied is a form of Map.
 162  
    * @see com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java.lang
 163  
    *      .Class)
 164  
    */
 165  
   @SuppressWarnings("unchecked")
 166  
   // API is not generic
 167  
   public boolean canConvert(Class clazz) {
 168  302
     boolean convert = (Map.class.isAssignableFrom(clazz));
 169  302
     return convert;
 170  
   }
 171  
 
 172  
 }