Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / simplereaders / JSonReader.java @ 45567

History | View | Annotate | Download (4.72 KB)

1
/*
2
 * To change this license theHeader, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.csv.simplereaders;
7

    
8
import java.io.IOException;
9
import java.io.InputStreamReader;
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.Set;
13
import javax.json.JsonArray;
14
import javax.json.JsonObject;
15
import javax.json.JsonString;
16
import javax.json.JsonStructure;
17
import javax.json.JsonValue;
18
import org.apache.commons.io.IOUtils;
19
import org.apache.commons.lang3.StringUtils;
20
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters;
21
import static org.gvsig.fmap.dal.store.csv.CSVStoreParameters.FIRST_LINE_HEADER;
22
import static org.gvsig.fmap.dal.store.csv.CSVStoreParameters.HEADER;
23
import org.gvsig.json.Json;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
/**
28
 *
29
 * @author jovivas
30
 */
31
public class JSonReader implements SimpleReader {
32

    
33
    private static final Logger LOGGER = LoggerFactory.getLogger(CSVReader.class);
34

    
35
    private JsonArray jsonData;
36
//    private final CSVStoreParameters parameters;
37
//    private List<String>  nextLine;
38
    private int columns;
39
    private String[] header;
40
    private int currentElement = 0;
41
    private final CSVStoreParameters parameters;
42

    
43
    public JSonReader(InputStreamReader reader, CSVStoreParameters csvParameters) throws IOException {
44
        String input = IOUtils.toString(reader);
45
        this.jsonData = Json.createArray(input);
46
        this.columns = -1;
47
        this.header = null;
48
        this.parameters = csvParameters;
49
        this.parameters.setDynValue(FIRST_LINE_HEADER, false);
50
        if (StringUtils.isBlank((CharSequence) this.parameters.getDynValue(HEADER))){
51
            this.parameters.setDynValue(HEADER, StringUtils.join(this.getHeader(), ','));
52
        }
53
    }
54

    
55
    @Override
56
    public String[] getHeader() throws IOException {
57
        if (this.header == null) {
58
            JsonObject firstItem = this.jsonData.getJsonObject(0);
59
            String[] theHeader = new String[firstItem.size()];
60
            int i = 0;
61
            for (String item : firstItem.keySet()) {
62
                theHeader[i++] = item;
63
            }
64
            this.header = theHeader;
65
        }
66
        return this.header;
67
    }
68

    
69
    @Override
70
    public int getColumnsCount() throws IOException {
71
        if (this.columns <= 0) {
72
            this.columns = this.getHeader().length;
73
        }
74
        return this.columns;
75
    }
76

    
77
    @Override
78
    public List<String> read() throws IOException {
79
        List<String> values = this.read(currentElement);
80
        if (values == null) {
81
            return null;
82
        }
83
        currentElement += 1;
84
        return values;
85
    }
86

    
87
    public List<String> read(int rowNumber) throws IOException {
88
        List<String> values = new ArrayList<>();
89
        if (rowNumber < jsonData.size()) {
90
            JsonObject feature = jsonData.getJsonObject(rowNumber);
91
            for (String columName : this.getHeader()) {
92
                if (feature.isNull(columName)) {
93
                    values.add("");
94
                } else {
95
                    JsonValue value = feature.get(columName);
96
                    switch (value.getValueType()) {
97
                        case FALSE:
98
                            values.add("false");
99
                            break;
100
                        case STRING:
101
                            values.add(((JsonString) value).getString());
102
                            break;
103
                        case TRUE:
104
                            values.add("true");
105
                            break;
106
                        case NULL:
107
                            values.add("");
108
                            break;
109
                        case NUMBER:
110
                        case ARRAY:
111
                        case OBJECT:
112
                        default:
113
                            values.add(value.toString());
114
                            break;
115
                    }
116
                }
117
            }
118
            return values;
119
        }
120
        return null;
121
    }
122

    
123
    @Override
124
    public void close() throws IOException {
125
        this.jsonData = null;
126
    }
127

    
128
    @Override
129
    public List<String> skip(int lines) throws IOException {
130
        this.currentElement += lines;
131
        if (this.currentElement > this.jsonData.size()) {
132
            return null;
133
        }
134
        return read(this.currentElement);
135
    }
136

    
137
    @Override
138
    public int getLine() {
139
        if (this.jsonData == null) {
140
            return 0;
141
        }
142
        return this.currentElement;
143
    }
144

    
145
    @Override
146
    public List<String> nextRowValues() {
147
        try {
148
            return this.read();
149
        } catch (IOException ex) {
150
            throw new RuntimeException(ex);
151
        }
152
    }
153
}