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