Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MapStringStringAdapter |
|
| 4.5;4.5 |
1 | /* | |
2 | * Copyright 2007-2010 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.util.jaxb; | |
17 | ||
18 | import java.util.ArrayList; | |
19 | import java.util.HashMap; | |
20 | import java.util.List; | |
21 | import java.util.Map; | |
22 | ||
23 | import javax.xml.bind.annotation.adapters.XmlAdapter; | |
24 | ||
25 | /** | |
26 | * Do JAXB mapping of Map<String, String> to a format like the following for a | |
27 | * map containing { key1:value1, key2:value2 }: | |
28 | * | |
29 | * <pre> | |
30 | * {@code | |
31 | * <...> | |
32 | * <entry key="key1">value1</entry> | |
33 | * <entry key="key2">value2</entry> | |
34 | * </...> | |
35 | * } | |
36 | * | |
37 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
38 | * | |
39 | */ | |
40 | 0 | public class MapStringStringAdapter extends XmlAdapter<StringMapEntryList, Map<String, String>> { |
41 | ||
42 | @Override | |
43 | public StringMapEntryList marshal(Map<String, String> map) throws Exception { | |
44 | 0 | if (map == null) { |
45 | 0 | return null; |
46 | } | |
47 | 0 | List<StringMapEntry> entries = new ArrayList<StringMapEntry>(); |
48 | 0 | for (Map.Entry<String, String> entry : map.entrySet()) { |
49 | 0 | entries.add(new StringMapEntry(entry)); |
50 | } | |
51 | 0 | return new StringMapEntryList(entries); |
52 | } | |
53 | ||
54 | @Override | |
55 | public Map<String, String> unmarshal(StringMapEntryList entryList) throws Exception { | |
56 | 0 | if (entryList == null || entryList.getEntries() == null) { |
57 | 0 | return null; |
58 | } | |
59 | 0 | List<StringMapEntry> entries = entryList.getEntries(); |
60 | 0 | Map<String, String> resultMap = new HashMap<String, String>(entries.size()); |
61 | 0 | for (StringMapEntry entry : entries) { |
62 | 0 | resultMap.put(entry.getKey(), entry.getValue()); |
63 | } | |
64 | 0 | return resultMap; |
65 | } | |
66 | } |