Revision 45567 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

View differences:

JSonReader.java
12 12
import java.util.Set;
13 13
import javax.json.JsonArray;
14 14
import javax.json.JsonObject;
15
import javax.json.JsonString;
16
import javax.json.JsonStructure;
15 17
import javax.json.JsonValue;
16 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;
17 23
import org.gvsig.json.Json;
18 24
import org.slf4j.Logger;
19 25
import org.slf4j.LoggerFactory;
......
23 29
 * @author jovivas
24 30
 */
25 31
public class JSonReader implements SimpleReader {
26
    
32

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

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

  
36
    public JSonReader(InputStreamReader reader) throws IOException {
37
        String input= IOUtils.toString(reader);
43
    public JSonReader(InputStreamReader reader, CSVStoreParameters csvParameters) throws IOException {
44
        String input = IOUtils.toString(reader);
38 45
        this.jsonData = Json.createArray(input);
39
        this.columns=-1;
40
        this.header=null;
41
    }    
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
    }
42 54

  
43 55
    @Override
44 56
    public String[] getHeader() throws IOException {
45
        if (this.header == null){
57
        if (this.header == null) {
46 58
            JsonObject firstItem = this.jsonData.getJsonObject(0);
47
            String[] theHeader = new String [firstItem.size()];
59
            String[] theHeader = new String[firstItem.size()];
48 60
            int i = 0;
49 61
            for (String item : firstItem.keySet()) {
50
                theHeader[i++]=item;
62
                theHeader[i++] = item;
51 63
            }
52 64
            this.header = theHeader;
53 65
        }
......
56 68

  
57 69
    @Override
58 70
    public int getColumnsCount() throws IOException {
59
        if( this.columns <= 0 ) {
60
            this.columns =  this.getHeader().length;
71
        if (this.columns <= 0) {
72
            this.columns = this.getHeader().length;
61 73
        }
62 74
        return this.columns;
63 75
    }
......
65 77
    @Override
66 78
    public List<String> read() throws IOException {
67 79
        List<String> values = this.read(currentElement);
68
        if (values == null){
80
        if (values == null) {
69 81
            return null;
70 82
        }
71
        currentElement+=1;
83
        currentElement += 1;
72 84
        return values;
73 85
    }
74 86

  
75 87
    public List<String> read(int rowNumber) throws IOException {
76 88
        List<String> values = new ArrayList<>();
77
        if (rowNumber < jsonData.size()){
89
        if (rowNumber < jsonData.size()) {
78 90
            JsonObject feature = jsonData.getJsonObject(rowNumber);
79 91
            for (String columName : this.getHeader()) {
80
                String value = feature.getString(columName,"");
81
                values.add(value);         
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
                }
82 117
            }
83 118
            return values;
84 119
        }
85 120
        return null;
86 121
    }
87
        
122

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

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

  
102 137
    @Override
103 138
    public int getLine() {
104
        if ( this.jsonData==null){
139
        if (this.jsonData == null) {
105 140
            return 0;
106
        } 
141
        }
107 142
        return this.currentElement;
108 143
    }
109 144

  
......
114 149
        } catch (IOException ex) {
115 150
            throw new RuntimeException(ex);
116 151
        }
117
    } 
152
    }
118 153
}

Also available in: Unified diff