summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/XMLHandler.java
blob: 174407af054d1b7fd969c515937146b2e77a4c8a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package design.persistence;
import java.io.File;
import java.io.IOException;
import design.model.DataHandler;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import design.model.Golfer;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;



public class XMLHandler implements DataHandler 
{    
    private final ObjectMapper jsonMapper = new ObjectMapper();
    private final XmlMapper xmlMapper = new XmlMapper();

    public void exportPersonalData(File file) throws IOException {
        // get our json data
        File jsonData = JSONPersonalDatabase.instance().exportData(file);
        // read the top tree node (the array)
        JsonNode golfersNode = jsonMapper.readTree(jsonData);
        // map it to an object node
        xmlMapper.writerWithDefaultPrettyPrinter().writeValue(file, golfersNode);
    }

    public void importPersonalData(File file) throws IOException {
        JsonNode root = xmlMapper.readTree(file); 

        System.out.println(root.toPrettyString());
        Golfer[] golfers = xmlMapper.treeToValue(root, Golfer[].class); 
        File tempJson = File.createTempFile("imported", ".json"); 
        jsonMapper.writerWithDefaultPrettyPrinter().writeValue(tempJson, golfers); 
        JSONPersonalDatabase.instance().importData(tempJson); 
        tempJson.delete();
    }

    

    public void exportLeagueData(File file) throws IOException
    {
        // TO DO: exporting league data
    }

    public void importLeagueData(File file) throws IOException
    {
        // TO DO: importing league data
    }
}