1 package org.kuali.common.util.xml.jaxb.adapter;
2
3 import javax.xml.bind.annotation.adapters.XmlAdapter;
4
5 import org.kuali.common.util.Assert;
6 import org.kuali.common.util.Str;
7
8 import com.google.common.base.Optional;
9
10 public final class FlattenOptionalStringAdapter extends XmlAdapter<String, Optional<String>> {
11
12 public FlattenOptionalStringAdapter() {
13 this(FlattenConstants.DEFAULT_CR_REPLACEMENT, FlattenConstants.DEFAULT_LF_REPLACEMENT);
14 }
15
16 public FlattenOptionalStringAdapter(String carriageReturnReplacement, String linefeedReplacement) {
17
18 Assert.noBlanks(carriageReturnReplacement, linefeedReplacement);
19 this.carriageReturnReplacement = carriageReturnReplacement;
20 this.linefeedReplacement = linefeedReplacement;
21 }
22
23 private final String carriageReturnReplacement;
24 private final String linefeedReplacement;
25
26 @Override
27 public String marshal(Optional<String> optional) {
28 if (optional.isPresent()) {
29 return Str.flatten(optional.get(), carriageReturnReplacement, linefeedReplacement);
30 } else {
31 return null;
32 }
33 }
34
35 @Override
36 public Optional<String> unmarshal(String value) {
37 if (value == null) {
38 return Optional.<String> absent();
39 } else {
40 return Optional.<String> of(Str.inflate(value, carriageReturnReplacement, linefeedReplacement));
41 }
42 }
43
44 public String getCarriageReturnReplacement() {
45 return carriageReturnReplacement;
46 }
47
48 public String getLinefeedReplacement() {
49 return linefeedReplacement;
50 }
51
52 }