1 package org.kuali.common.util.spring.format;
2
3 import static org.kuali.common.util.base.Precondition.checkNotBlank;
4
5 import java.util.Locale;
6 import java.util.Properties;
7
8 import org.kuali.common.util.spring.convert.support.XmlStringToPropertiesConverter;
9 import org.kuali.common.util.spring.convert.support.PropertiesToXmlStringConverter;
10 import org.springframework.format.Formatter;
11
12 public final class XmlPropertiesFormatter implements Formatter<Properties> {
13
14 public XmlPropertiesFormatter(String encoding, String emptyPropertiesToken) {
15 this.encoding = checkNotBlank(encoding, "encoding");
16 this.emptyPropertiesToken = checkNotBlank(emptyPropertiesToken, "emptyPropertiesToken");
17 this.parser = new XmlStringToPropertiesConverter(encoding);
18 this.printer = new PropertiesToXmlStringConverter(encoding);
19 }
20
21 private final String encoding;
22 private final String emptyPropertiesToken;
23 private final XmlStringToPropertiesConverter parser;
24 private final PropertiesToXmlStringConverter printer;
25
26 @Override
27 public String print(Properties properties, Locale locale) {
28 if (properties.isEmpty()) {
29 return emptyPropertiesToken;
30 } else {
31 return printer.convert(properties);
32 }
33
34 }
35
36 @Override
37 public Properties parse(String string, Locale locale) {
38 if (emptyPropertiesToken.equals(string)) {
39 return new Properties();
40 } else {
41 return parser.convert(string);
42 }
43 }
44
45 public String getEncoding() {
46 return encoding;
47 }
48
49 public XmlStringToPropertiesConverter getParser() {
50 return parser;
51 }
52
53 public PropertiesToXmlStringConverter getPrinter() {
54 return printer;
55 }
56
57 public String getEmptyPropertiesToken() {
58 return emptyPropertiesToken;
59 }
60
61 }