blob: 0a07d6e189af1025f6f3c3e1cfc9ef3a90c4f6af (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package design.persistence.importexport;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import design.persistence.Serializers;
public class XMLHandler implements DataHandler {
private final DataSource dataSource;
private final XmlMapper xmlMapper = new XmlMapper();
public XMLHandler(DataSource dataSource) {
this.dataSource = dataSource;
Serializers.configureMapper(xmlMapper);
SimpleModule module = dataSource.getJacksonModule();
module.addSerializer(LocalDateTime.class, new Serializers.DateTimeStringSerializer());
xmlMapper.registerModule(module);
}
@Override
public void importData(File file) throws IOException {
Object data = xmlMapper.readValue(file, dataSource.getTargetClass());
dataSource.importData(data);
}
@Override
public void exportData(File file) throws IOException {
Object data = dataSource.exportData();
xmlMapper.writerWithDefaultPrettyPrinter()
.writeValue(file, data);
}
}
|