Coverage Report - org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
MapStringStringAdapter
0%
0/15
0%
0/10
1.9
MapStringStringAdapter$StringMapEntry
0%
0/14
N/A
1.9
MapStringStringAdapter$StringMapEntryList
0%
0/10
0%
0/2
1.9
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 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.kuali.rice.core.api.util.jaxb;
 17  
 
 18  
 import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
 19  
 import org.w3c.dom.Element;
 20  
 
 21  
 import javax.xml.bind.annotation.XmlAccessType;
 22  
 import javax.xml.bind.annotation.XmlAccessorType;
 23  
 import javax.xml.bind.annotation.XmlAnyElement;
 24  
 import javax.xml.bind.annotation.XmlAttribute;
 25  
 import javax.xml.bind.annotation.XmlElement;
 26  
 import javax.xml.bind.annotation.XmlType;
 27  
 import javax.xml.bind.annotation.XmlValue;
 28  
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 29  
 import java.io.Serializable;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Collection;
 32  
 import java.util.Collections;
 33  
 import java.util.HashMap;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 
 37  
 /**
 38  
  * Do JAXB mapping of Map<String, String> to a format like the following for a
 39  
  * map containing { key1:value1, key2:value2 }:
 40  
  * 
 41  
  * <pre>
 42  
  * {@code
 43  
  * <...>
 44  
  *   <entry key="key1">value1</entry>
 45  
  *   <entry key="key2">value2</entry>
 46  
  * </...>
 47  
  * }
 48  
  * 
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  *
 51  
  */
 52  0
 public class MapStringStringAdapter extends XmlAdapter<MapStringStringAdapter.StringMapEntryList, Map<String, String>> {
 53  
 
 54  
         @Override
 55  
         public StringMapEntryList marshal(Map<String, String> map) throws Exception {
 56  0
                 if (map == null) {
 57  0
                         return null;
 58  
                 }
 59  0
                 List<StringMapEntry> entries = new ArrayList<StringMapEntry>();
 60  0
                 for (Map.Entry<String, String> entry : map.entrySet()) {
 61  0
                         entries.add(new StringMapEntry(entry));
 62  
                 }
 63  0
                 return new StringMapEntryList(entries);
 64  
         }
 65  
 
 66  
         @Override
 67  
         public Map<String, String> unmarshal(StringMapEntryList entryList) throws Exception {
 68  0
                 if (entryList == null || entryList.getEntries() == null) {
 69  0
                         return null;
 70  
                 }
 71  0
                 List<StringMapEntry> entries = entryList.getEntries();
 72  0
                 Map<String, String> resultMap = new HashMap<String, String>(entries.size());
 73  0
                 for (StringMapEntry entry : entries) {
 74  0
                         resultMap.put(entry.getKey(), entry.getValue());
 75  
                 }
 76  0
                 return Collections.unmodifiableMap(resultMap);
 77  
         }
 78  
 
 79  
     /**
 80  
      * Single String-String key-value pair for
 81  
      * marshalling/unmarshalling. Need this rather than
 82  
      * general Map.Entry<String, String> to specify
 83  
      * cardinality in resulting wsdl's.
 84  
      *
 85  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 86  
      *
 87  
      */
 88  
     @XmlAccessorType(XmlAccessType.NONE)
 89  
     @XmlType(name = "StringMapEntryType")
 90  
     public static final class StringMapEntry implements Serializable {
 91  
 
 92  
         private static final long serialVersionUID = -9609663434312103L;
 93  
 
 94  
         @XmlAttribute(name = "key")
 95  
         private final String key;
 96  
 
 97  
         @XmlValue
 98  
         private final String value;
 99  
 
 100  
         /**
 101  
          * Used only by JAXB.
 102  
          */
 103  
         @SuppressWarnings("unused")
 104  0
         private StringMapEntry() {
 105  0
             this.key = null;
 106  0
             this.value = null;
 107  0
         }
 108  
 
 109  0
         public StringMapEntry(String key, String value) {
 110  0
             this.key = key;
 111  0
             this.value = value;
 112  0
         }
 113  
 
 114  0
         public StringMapEntry(Map.Entry<String, String> e) {
 115  0
             this.key = e.getKey();
 116  0
             this.value = e.getValue();
 117  0
         }
 118  
 
 119  
         public String getKey() {
 120  0
             return this.key;
 121  
         }
 122  
 
 123  
         public String getValue() {
 124  0
             return this.value;
 125  
         }
 126  
 
 127  
     }
 128  
 
 129  0
     @XmlAccessorType(XmlAccessType.FIELD)
 130  
     @XmlType(name = "StringMapEntryListType")
 131  
     public static class StringMapEntryList extends AbstractDataTransferObject {
 132  
 
 133  
         private static final long serialVersionUID = 1L;
 134  
 
 135  
         @XmlElement(name = "entry")
 136  
         private final List<StringMapEntry> entries;
 137  
 
 138  0
         @SuppressWarnings("unused")
 139  
         @XmlAnyElement
 140  
         private final Collection<Element> _futureElements = null;
 141  
 
 142  
         @SuppressWarnings("unused")
 143  0
         private StringMapEntryList() {
 144  0
             this.entries = null;
 145  0
         }
 146  
 
 147  0
         public StringMapEntryList(List<StringMapEntry> entries) {
 148  0
             this.entries = new ArrayList<StringMapEntry>(entries);
 149  0
         }
 150  
 
 151  
         /**
 152  
          * @return the attribute
 153  
          */
 154  
         public List<StringMapEntry> getEntries() {
 155  0
             if (this.entries == null) {
 156  0
                 return Collections.emptyList();
 157  
             }
 158  0
             return Collections.unmodifiableList(entries);
 159  
         }
 160  
     }
 161  
 }