1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.xml.jaxb.adapter;
17
18 import java.util.List;
19
20 import javax.xml.bind.annotation.adapters.XmlAdapter;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.kuali.common.util.Assert;
24 import org.kuali.common.util.CollectionUtils;
25
26 import com.google.common.collect.ImmutableList;
27
28
29
30
31
32
33
34
35
36
37
38 public class TrimmingCSVStringAdapter extends XmlAdapter<String, List<String>> {
39
40 private static final String DELIMITER = ",";
41
42 @Override
43 public final String marshal(List<String> strings) {
44 if (strings.size() == 0) {
45 return null;
46 }
47 StringBuilder sb = new StringBuilder();
48 for (int i = 0; i < strings.size(); i++) {
49 if (i != 0) {
50 sb.append(DELIMITER);
51 }
52 String trimmed = strings.get(i).trim();
53 Assert.isFalse(StringUtils.contains(trimmed, DELIMITER), "[" + trimmed + "] contains '" + DELIMITER + "'");
54 sb.append(trimmed);
55 }
56 return sb.toString();
57 }
58
59 @Override
60 public final List<String> unmarshal(String string) {
61 if (string == null) {
62 return ImmutableList.of();
63 } else {
64 return ImmutableList.copyOf(CollectionUtils.getTrimmedListFromCSV(string));
65 }
66 }
67
68 }