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 @ 47636
History | View | Annotate | Download (4.71 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.Reader; |
10 |
import java.util.ArrayList; |
11 |
import java.util.List; |
12 |
import javax.json.JsonArray; |
13 |
import javax.json.JsonObject; |
14 |
import javax.json.JsonString; |
15 |
import javax.json.JsonValue; |
16 |
import org.apache.commons.io.IOUtils; |
17 |
import org.apache.commons.lang3.StringUtils; |
18 |
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters; |
19 |
import static org.gvsig.fmap.dal.store.csv.CSVStoreParameters.FIRST_LINE_HEADER; |
20 |
import static org.gvsig.fmap.dal.store.csv.CSVStoreParameters.HEADER; |
21 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReader; |
22 |
import org.gvsig.json.Json; |
23 |
import org.slf4j.Logger; |
24 |
import org.slf4j.LoggerFactory; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author jovivas
|
29 |
*/
|
30 |
public class JSonReader implements SimpleReader { |
31 |
|
32 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVReaderSuperCSV.class); |
33 |
|
34 |
private JsonArray jsonData;
|
35 |
// private final CSVStoreParameters parameters;
|
36 |
// private List<String> nextLine;
|
37 |
private int columns; |
38 |
private String[] header; |
39 |
private int currentElement = 0; |
40 |
private final CSVStoreParameters parameters; |
41 |
|
42 |
public JSonReader(Reader reader, CSVStoreParameters csvParameters) throws IOException { |
43 |
String input = IOUtils.toString(reader);
|
44 |
this.jsonData = Json.createArray(input);
|
45 |
this.columns = -1; |
46 |
this.header = null; |
47 |
this.parameters = csvParameters;
|
48 |
this.parameters.setDynValue(FIRST_LINE_HEADER, false); |
49 |
if (StringUtils.isBlank((CharSequence) this.parameters.getDynValue(HEADER))){ |
50 |
this.parameters.setDynValue(HEADER, StringUtils.join(this.getHeader(), ',')); |
51 |
} |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public String[] getHeader() throws IOException { |
56 |
if (this.header == null) { |
57 |
JsonObject firstItem = this.jsonData.getJsonObject(0); |
58 |
String[] theHeader = new String[firstItem.size()]; |
59 |
int i = 0; |
60 |
for (String item : firstItem.keySet()) { |
61 |
theHeader[i++] = item; |
62 |
} |
63 |
this.header = theHeader;
|
64 |
} |
65 |
return this.header; |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public int getColumnsCount() throws IOException { |
70 |
if (this.columns <= 0) { |
71 |
this.columns = this.getHeader().length; |
72 |
} |
73 |
return this.columns; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public List<String> read() throws IOException { |
78 |
List<String> values = this.read(currentElement); |
79 |
if (values == null) { |
80 |
return null; |
81 |
} |
82 |
currentElement += 1;
|
83 |
return values;
|
84 |
} |
85 |
|
86 |
public List<String> read(int rowNumber) throws IOException { |
87 |
List<String> values = new ArrayList<>(); |
88 |
if (rowNumber < jsonData.size()) {
|
89 |
JsonObject feature = jsonData.getJsonObject(rowNumber); |
90 |
for (String columName : this.getHeader()) { |
91 |
if (feature.isNull(columName)) {
|
92 |
values.add("");
|
93 |
} else {
|
94 |
JsonValue value = feature.get(columName); |
95 |
switch (value.getValueType()) {
|
96 |
case FALSE:
|
97 |
values.add("false");
|
98 |
break;
|
99 |
case STRING:
|
100 |
values.add(((JsonString) value).getString()); |
101 |
break;
|
102 |
case TRUE:
|
103 |
values.add("true");
|
104 |
break;
|
105 |
case NULL:
|
106 |
values.add("");
|
107 |
break;
|
108 |
case NUMBER:
|
109 |
case ARRAY:
|
110 |
case OBJECT:
|
111 |
default:
|
112 |
values.add(value.toString()); |
113 |
break;
|
114 |
} |
115 |
} |
116 |
} |
117 |
return values;
|
118 |
} |
119 |
return null; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public void close() throws IOException { |
124 |
this.jsonData = null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public List<String> skip(int lines) throws IOException { |
129 |
this.currentElement += lines;
|
130 |
if (this.currentElement > this.jsonData.size()) { |
131 |
return null; |
132 |
} |
133 |
return read(this.currentElement); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public int getLine() { |
138 |
if (this.jsonData == null) { |
139 |
return 0; |
140 |
} |
141 |
return this.currentElement; |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public List<String> nextRowValues() { |
146 |
try {
|
147 |
return this.read(); |
148 |
} catch (IOException ex) { |
149 |
throw new RuntimeException(ex); |
150 |
} |
151 |
} |
152 |
} |