Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QualificationListAdapter |
|
| 4.5;4.5 |
1 | /* | |
2 | * Copyright 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/ecl1.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.kim.api.jaxb; | |
17 | ||
18 | import org.apache.commons.lang.StringUtils; | |
19 | import org.kuali.rice.core.util.jaxb.StringMapEntry; | |
20 | ||
21 | import javax.xml.bind.UnmarshalException; | |
22 | import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; | |
23 | import javax.xml.bind.annotation.adapters.XmlAdapter; | |
24 | import java.util.HashMap; | |
25 | import java.util.Map; | |
26 | ||
27 | /** | |
28 | * An XML adapter that converts between QualificationList objects and Map<String, String> objects. | |
29 | * Unmarshalled keys and values will automatically be trimmed if non-null. | |
30 | * | |
31 | * <p>This adapter will throw an exception during unmarshalling if blank or duplicate keys are encountered. | |
32 | * | |
33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
34 | */ | |
35 | 0 | public class QualificationListAdapter extends XmlAdapter<QualificationList,Map<String, String>> { |
36 | ||
37 | /** | |
38 | * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) | |
39 | */ | |
40 | @Override | |
41 | public Map<String, String> unmarshal(QualificationList v) throws Exception { | |
42 | 0 | if (v != null) { |
43 | 0 | NormalizedStringAdapter normalizedStringAdapter = new NormalizedStringAdapter(); |
44 | 0 | Map<String, String> map = new HashMap<String, String>(); |
45 | 0 | for (StringMapEntry stringMapEntry : v.getQualifications()) { |
46 | 0 | String tempKey = normalizedStringAdapter.unmarshal(stringMapEntry.getKey()); |
47 | 0 | if (StringUtils.isBlank(tempKey)) { |
48 | 0 | throw new UnmarshalException("Cannot create a qualification entry with a blank key"); |
49 | 0 | } else if (map.containsKey(tempKey)) { |
50 | 0 | throw new UnmarshalException("Cannot create more than one qualification entry with a key of \"" + tempKey + "\""); |
51 | } | |
52 | 0 | map.put(tempKey, normalizedStringAdapter.unmarshal(stringMapEntry.getValue())); |
53 | 0 | } |
54 | } | |
55 | 0 | return null; |
56 | } | |
57 | ||
58 | /** | |
59 | * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) | |
60 | */ | |
61 | @Override | |
62 | public QualificationList marshal(Map<String, String> v) throws Exception { | |
63 | 0 | return (v != null) ? new QualificationList(v) : null; |
64 | } | |
65 | ||
66 | } |