blob: 1a16b909bd2273a5598a7dd206827f761889f152 (
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
|
package design.persistence.importexport;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XMLHandler implements DataHandler {
private final DataSource dataSource;
private final XmlMapper xmlMapper = new XmlMapper();
public XMLHandler(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void importData(File file) throws IOException {
JsonNode tree = xmlMapper.readTree(file);
JsonNode unwrapped = tree.get("items");
dataSource.importData(unwrapped);
}
@Override
public void exportData(File file) throws IOException {
JsonNode tree = dataSource.exportData();
ObjectNode wrapper = xmlMapper.createObjectNode();
wrapper.set("items", tree);
xmlMapper.writerWithDefaultPrettyPrinter().withRootName("export").writeValue(file, wrapper);
}
}
|