Coverage Report - org.apache.shindig.social.core.util.xstream.DataCollectionConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
DataCollectionConverter
57%
47/83
38%
13/34
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.google.common.collect.Maps;
 21  
 
 22  
 import com.thoughtworks.xstream.converters.MarshallingContext;
 23  
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 24  
 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
 25  
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 26  
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 27  
 import com.thoughtworks.xstream.mapper.Mapper;
 28  
 
 29  
 import org.apache.shindig.social.opensocial.spi.DataCollection;
 30  
 
 31  
 import java.util.HashMap;
 32  
 import java.util.Map;
 33  
 import java.util.Map.Entry;
 34  
 
 35  
 /**
 36  
  * This converter changes the way in which a collection is serialized
 37  
  * 
 38  
  */
 39  
 public class DataCollectionConverter extends AbstractCollectionConverter {
 40  
 
 41  
   /**
 42  
    * @param mapper
 43  
    */
 44  
   public DataCollectionConverter(Mapper mapper) {
 45  222
     super(mapper);
 46  222
   }
 47  
 
 48  
   /**
 49  
    * {@inheritDoc}
 50  
    * 
 51  
    * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#canConvert(java.lang.Class)
 52  
    */
 53  
   @Override
 54  
   public boolean canConvert(Class clazz) {
 55  314
     boolean convert = (DataCollection.class.isAssignableFrom(clazz));
 56  314
     return convert;
 57  
   }
 58  
 
 59  
   /**
 60  
    * {@inheritDoc}
 61  
    * 
 62  
    * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#marshal(java.lang.Object,
 63  
    *      com.thoughtworks.xstream.io.HierarchicalStreamWriter,
 64  
    *      com.thoughtworks.xstream.converters.MarshallingContext)
 65  
    */
 66  
   @Override
 67  
   public void marshal(Object source, HierarchicalStreamWriter writer,
 68  
       MarshallingContext context) {
 69  
 
 70  9
     DataCollection collection = (DataCollection) source;
 71  9
     Map<String, Map<String, String>> internalMap = collection.getEntry();
 72  
 
 73  
     if (false) {
 74  
       Map<String, String> m = internalMap.values().iterator().next();
 75  
       for (Entry<String, String> e : m.entrySet()) {
 76  
         writer.startNode("entry");
 77  
         writer.startNode("key");
 78  
         writer.setValue(e.getKey());
 79  
         writer.endNode();
 80  
         writer.startNode("value");
 81  
         writer.setValue(e.getValue());
 82  
         writer.endNode();
 83  
         writer.endNode();
 84  
       }
 85  
     } else {
 86  9
       for (Entry<String, Map<String, String>> eo : internalMap.entrySet()) {
 87  11
         writer.startNode("entry");
 88  11
         writer.startNode("key");
 89  11
         writer.setValue(eo.getKey());
 90  11
         writer.endNode();
 91  11
         writer.startNode("value");
 92  11
         for (Entry<String, String> ei : eo.getValue().entrySet()) {
 93  8
           writer.startNode("entry");
 94  8
           writer.startNode("key");
 95  8
           writer.setValue(ei.getKey());
 96  8
           writer.endNode();
 97  8
           writer.startNode("value");
 98  8
           writer.setValue(ei.getValue());
 99  8
           writer.endNode();
 100  8
           writer.endNode();
 101  8
         }
 102  
 
 103  11
         writer.endNode();
 104  11
         writer.endNode();
 105  11
       }
 106  
     }
 107  9
   }
 108  
 
 109  
   /**
 110  
    * {@inheritDoc}
 111  
    * 
 112  
    * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
 113  
    *      com.thoughtworks.xstream.converters.UnmarshallingContext)
 114  
    */
 115  
   @SuppressWarnings("unchecked")
 116  
   @Override
 117  
   public Object unmarshal(HierarchicalStreamReader reader,
 118  
       UnmarshallingContext context) {
 119  1
     reader.moveDown();
 120  1
     Map<String, Object> m = Maps.newHashMap();
 121  3
     while (reader.hasMoreChildren()) {
 122  2
       reader.moveDown(); // entry
 123  2
       String ok = null;
 124  2
       Object ov = null;
 125  2
       while (reader.hasMoreChildren()) {
 126  0
         reader.moveDown(); // key or value
 127  0
         String elname = reader.getNodeName();
 128  0
         if ("key".equals(elname)) {
 129  0
           ok = reader.getValue();
 130  0
         } else if ("value".equals(elname)) {
 131  0
           ov = reader.getValue();
 132  0
           if (reader.hasMoreChildren()) {
 133  0
             Map<String, String> innerMap = Maps.newHashMap();
 134  0
             while (reader.hasMoreChildren()) {
 135  0
               reader.moveDown();// entry
 136  0
               String k = null;
 137  0
               String v = null;
 138  0
               while (reader.hasMoreChildren()) {
 139  0
                 reader.moveDown(); // key or value
 140  0
                 if ("key".equals(elname)) {
 141  0
                   k = reader.getValue();
 142  0
                 } else if ("value".equals(elname)) {
 143  0
                   v = reader.getValue();
 144  
                 }
 145  0
                 reader.moveUp();
 146  0
               }
 147  0
               innerMap.put(k, v);
 148  0
               reader.moveUp();
 149  0
             }
 150  0
             ov = innerMap;
 151  
           } else {
 152  
           }
 153  
         }
 154  0
         reader.moveUp();
 155  0
       }
 156  2
       reader.moveUp();
 157  2
       m.put(ok, ov);
 158  2
     }
 159  1
     reader.moveUp();
 160  
     // scan the map, if there are any maps, then everything should be in maps.
 161  1
     boolean nonmap = false;
 162  1
     for (Entry<String, Object> e : m.entrySet()) {
 163  1
       if (e.getValue() instanceof String) {
 164  0
         nonmap = true;
 165  
       }
 166  1
     }
 167  1
     Map<String, Map<String, String>> fm = Maps.newHashMap();
 168  1
     if (nonmap) {
 169  0
       for (Entry<String, Object> e : m.entrySet()) {
 170  0
         if (e.getValue() instanceof Map) {
 171  0
           fm.put(e.getKey(), (Map<String, String>) e.getValue());
 172  0
         } else {
 173  
           // not certain that this makes sense, but can't see how else.
 174  0
           Map<String, String> mv = Maps.newHashMap();
 175  0
           mv.put(e.getKey(), (String) e.getValue());
 176  0
           fm.put(e.getKey(), mv);
 177  
         }
 178  0
       }
 179  
 
 180  0
     } else {
 181  1
       for (Entry<String, Object> e : m.entrySet()) {
 182  1
         fm.put(e.getKey(), (Map<String, String>) e.getValue());
 183  1
       }
 184  
     }
 185  1
     return new DataCollection(fm);
 186  
   }
 187  
 
 188  
 }