Revision 45563
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/CSVStoreProvider.java | ||
---|---|---|
80 | 80 |
import org.gvsig.fmap.dal.store.csv.AutomaticDetectionOfTypes.DetectedValue; |
81 | 81 |
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReader; |
82 | 82 |
import org.gvsig.fmap.dal.store.csv.simplereaders.FixedLenReader; |
83 |
import org.gvsig.fmap.dal.store.csv.simplereaders.JSonReader; |
|
83 | 84 |
import org.gvsig.fmap.dal.store.csv.simplereaders.SimpleReader; |
84 | 85 |
import org.gvsig.fmap.geom.Geometry; |
85 | 86 |
import org.gvsig.fmap.geom.GeometryLocator; |
... | ... | |
943 | 944 |
|
944 | 945 |
} |
945 | 946 |
|
946 |
private SimpleReader getSimpleReader(InputStreamReader in) { |
|
947 |
private SimpleReader getSimpleReader(InputStreamReader in) throws IOException {
|
|
947 | 948 |
SimpleReader reader; |
948 |
if (CSVStoreParameters.getRawFieldsDefinition(getCSVParameters()) != null) { |
|
949 |
String filename = CSVStoreParameters.getFileName(getCSVParameters()); |
|
950 |
if (FilenameUtils.isExtension(filename, "json")){ |
|
951 |
reader= new JSonReader(in); |
|
952 |
// reader= new JSonReader(in,getCSVParameters()); |
|
953 |
} else if (CSVStoreParameters.getRawFieldsDefinition(getCSVParameters()) != null) { |
|
949 | 954 |
reader = new FixedLenReader(in, getCSVParameters()); |
950 | 955 |
} else { |
951 | 956 |
reader = new CSVReader(in, getCSVParameters()); |
... | ... | |
1194 | 1199 |
AutomaticDetectionOfTypes x = new AutomaticDetectionOfTypes( |
1195 | 1200 |
this.getFullFileName() |
1196 | 1201 |
); |
1197 |
types = x.detect( |
|
1198 |
headers.length, |
|
1199 |
reader, |
|
1200 |
CSVStoreParameters.isFirstLineHeader(getCSVParameters()), |
|
1201 |
CSVStoreParameters.getLocale(getCSVParameters()) |
|
1202 |
); |
|
1202 |
String filename = CSVStoreParameters.getFileName(getCSVParameters()); |
|
1203 |
if (FilenameUtils.isExtension(filename, "csv")){ |
|
1204 |
types = x.detect( |
|
1205 |
headers.length, |
|
1206 |
reader, |
|
1207 |
CSVStoreParameters.isFirstLineHeader(getCSVParameters()), |
|
1208 |
CSVStoreParameters.getLocale(getCSVParameters()) |
|
1209 |
); |
|
1210 |
} else if (FilenameUtils.isExtension(filename, "json")) { |
|
1211 |
types = x.detect( |
|
1212 |
headers.length, |
|
1213 |
reader, |
|
1214 |
false, |
|
1215 |
CSVStoreParameters.getLocale(getCSVParameters()) |
|
1216 |
); |
|
1217 |
} |
|
1203 | 1218 |
} catch (Exception ex) { |
1204 | 1219 |
int lineno = 0; |
1205 | 1220 |
if (reader != null) { |
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/AutomaticDetectionOfTypes.java | ||
---|---|---|
69 | 69 |
public boolean possibleLong = true; |
70 | 70 |
public boolean possibleURL = true; |
71 | 71 |
public boolean possibleDate = true; |
72 |
public boolean possibleTime = true; |
|
73 |
public boolean possibleTimestamp = true; |
|
72 | 74 |
public boolean possibleGeometry = true; |
73 | 75 |
} |
74 | 76 |
|
... | ... | |
87 | 89 |
} |
88 | 90 |
|
89 | 91 |
@SuppressWarnings({"UseSpecificCatch", "ResultOfObjectAllocationIgnored"}) |
90 |
public DetectedValue[] detect(int columns, Rows rows, boolean isFirstLineHeader, Locale locale) throws IOException { |
|
92 |
public DetectedValue[] detect(int columns, |
|
93 |
Rows rows, |
|
94 |
boolean isFirstLineHeader, |
|
95 |
Locale locale |
|
96 |
) throws IOException { |
|
91 | 97 |
List<PossibleDataType> possibleDataTypes; |
92 | 98 |
DetectedValueImpl[] detectedValues = new DetectedValueImpl [columns]; |
93 | 99 |
|
... | ... | |
110 | 116 |
Coercion toDouble = typeManager.getCoercion(DataTypes.DOUBLE); |
111 | 117 |
Coercion toFloat = typeManager.getCoercion(DataTypes.FLOAT); |
112 | 118 |
Coercion toDate = typeManager.getCoercion(DataTypes.DATE); |
119 |
Coercion toTime = typeManager.getCoercion(DataTypes.TIME); |
|
120 |
Coercion toTimestamp = typeManager.getCoercion(DataTypes.TIMESTAMP); |
|
113 | 121 |
Coercion toInt = typeManager.getCoercion(DataTypes.INT); |
114 | 122 |
Coercion toLong = typeManager.getCoercion(DataTypes.LONG); |
115 | 123 |
Coercion toGeom = typeManager.getCoercion(DataTypes.GEOMETRY); |
... | ... | |
178 | 186 |
possibleDataType.possibleDate = false; |
179 | 187 |
} |
180 | 188 |
} |
189 |
if (possibleDataType.possibleTime) { |
|
190 |
try { |
|
191 |
toTime.coerce(rawvalue, coercionContext); |
|
192 |
possibleDataType.possibleTime = true; |
|
193 |
} catch (Exception ex) { |
|
194 |
possibleDataType.possibleTime = false; |
|
195 |
} |
|
196 |
} |
|
197 |
if (possibleDataType.possibleTimestamp) { |
|
198 |
try { |
|
199 |
toTimestamp.coerce(rawvalue, coercionContext); |
|
200 |
possibleDataType.possibleTimestamp = true; |
|
201 |
} catch (Exception ex) { |
|
202 |
possibleDataType.possibleTimestamp = false; |
|
203 |
} |
|
204 |
} |
|
181 | 205 |
if (possibleDataType.possibleURL) { |
182 | 206 |
try { |
183 | 207 |
new URL((String) rawvalue); |
... | ... | |
186 | 210 |
possibleDataType.possibleURL = false; |
187 | 211 |
} |
188 | 212 |
} |
213 |
|
|
189 | 214 |
if (possibleDataType.possibleGeometry) { |
190 | 215 |
try { |
191 | 216 |
toGeom.coerce((String) rawvalue); |
... | ... | |
226 | 251 |
detectedValues[n++].type = DataTypes.URL; |
227 | 252 |
continue; |
228 | 253 |
} |
254 |
if (possibleDataType.possibleTime) { |
|
255 |
detectedValues[n++].type = DataTypes.TIME; |
|
256 |
continue; |
|
257 |
} |
|
229 | 258 |
if (possibleDataType.possibleDate) { |
230 | 259 |
detectedValues[n++].type = DataTypes.DATE; |
231 | 260 |
continue; |
232 | 261 |
} |
262 |
if (possibleDataType.possibleTimestamp) { |
|
263 |
detectedValues[n++].type = DataTypes.TIMESTAMP; |
|
264 |
continue; |
|
265 |
} |
|
233 | 266 |
if (possibleDataType.possibleGeometry) { |
234 | 267 |
detectedValues[n++].type = DataTypes.GEOMETRY; |
235 | 268 |
continue; |
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 | ||
---|---|---|
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.JsonValue; |
|
16 |
import org.apache.commons.io.IOUtils; |
|
17 |
import org.gvsig.json.Json; |
|
18 |
import org.slf4j.Logger; |
|
19 |
import org.slf4j.LoggerFactory; |
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
* @author jovivas |
|
24 |
*/ |
|
25 |
public class JSonReader implements SimpleReader { |
|
26 |
|
|
27 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVReader.class); |
|
28 |
|
|
29 |
private JsonArray jsonData; |
|
30 |
// private final CSVStoreParameters parameters; |
|
31 |
// private List<String> nextLine; |
|
32 |
private int columns; |
|
33 |
private String[] header; |
|
34 |
private int currentElement = 0; |
|
35 |
|
|
36 |
public JSonReader(InputStreamReader reader) throws IOException { |
|
37 |
String input= IOUtils.toString(reader); |
|
38 |
this.jsonData = Json.createArray(input); |
|
39 |
this.columns=-1; |
|
40 |
this.header=null; |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public String[] getHeader() throws IOException { |
|
45 |
if (this.header == null){ |
|
46 |
JsonObject firstItem = this.jsonData.getJsonObject(0); |
|
47 |
String[] theHeader = new String [firstItem.size()]; |
|
48 |
int i = 0; |
|
49 |
for (String item : firstItem.keySet()) { |
|
50 |
theHeader[i++]=item; |
|
51 |
} |
|
52 |
this.header = theHeader; |
|
53 |
} |
|
54 |
return this.header; |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public int getColumnsCount() throws IOException { |
|
59 |
if( this.columns <= 0 ) { |
|
60 |
this.columns = this.getHeader().length; |
|
61 |
} |
|
62 |
return this.columns; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public List<String> read() throws IOException { |
|
67 |
List<String> values = this.read(currentElement); |
|
68 |
if (values == null){ |
|
69 |
return null; |
|
70 |
} |
|
71 |
currentElement+=1; |
|
72 |
return values; |
|
73 |
} |
|
74 |
|
|
75 |
public List<String> read(int rowNumber) throws IOException { |
|
76 |
List<String> values = new ArrayList<>(); |
|
77 |
if (rowNumber < jsonData.size()){ |
|
78 |
JsonObject feature = jsonData.getJsonObject(rowNumber); |
|
79 |
for (String columName : this.getHeader()) { |
|
80 |
String value = feature.getString(columName,""); |
|
81 |
values.add(value); |
|
82 |
} |
|
83 |
return values; |
|
84 |
} |
|
85 |
return null; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public void close() throws IOException { |
|
90 |
this.jsonData=null; |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public List<String> skip(int lines) throws IOException { |
|
95 |
this.currentElement += lines; |
|
96 |
if (this.currentElement > this.jsonData.size()){ |
|
97 |
return null; |
|
98 |
} |
|
99 |
return read(this.currentElement); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public int getLine() { |
|
104 |
if ( this.jsonData==null){ |
|
105 |
return 0; |
|
106 |
} |
|
107 |
return this.currentElement; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public List<String> nextRowValues() { |
|
112 |
try { |
|
113 |
return this.read(); |
|
114 |
} catch (IOException ex) { |
|
115 |
throw new RuntimeException(ex); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
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/CSVFilesystemServerProvider.java | ||
---|---|---|
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
@Override |
74 |
public boolean accept(File pathname) { |
|
75 |
return (pathname.getName().toLowerCase().endsWith(".csv")); |
|
74 |
public boolean accept(File file) { |
|
75 |
String pathname = file.getName().toLowerCase(); |
|
76 |
if ( pathname.endsWith(".csv") || pathname.endsWith(".json")){ |
|
77 |
return true; |
|
78 |
} |
|
79 |
return false; |
|
76 | 80 |
} |
77 | 81 |
|
78 | 82 |
@Override |
Also available in: Unified diff