Revision 46161
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/CSVFeatureWriter.java | ||
---|---|---|
19 | 19 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 | 20 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
21 | 21 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
22 |
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReaderSuperCSV; |
|
22 | 23 |
import org.gvsig.fmap.geom.Geometry; |
23 | 24 |
import org.gvsig.fmap.geom.GeometryLocator; |
24 | 25 |
import org.gvsig.fmap.geom.GeometryManager; |
... | ... | |
202 | 203 |
} |
203 | 204 |
} |
204 | 205 |
} |
205 |
if( values[i]!=null ) { |
|
206 |
values[i] = values[i].replaceAll("\\n","\\\\n"); |
|
207 |
values[i] = values[i].replaceAll("\\r","\\\\r"); |
|
208 |
} |
|
206 |
values[i] = CSVReaderSuperCSV.escapeCRLF(values[i]); |
|
209 | 207 |
} |
210 | 208 |
} |
211 | 209 |
try { |
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/CSVReaderSuperCSV.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.io.IOException; |
5 |
import java.io.InputStreamReader; |
|
6 | 5 |
import java.io.Reader; |
7 | 6 |
import java.util.List; |
8 | 7 |
import org.apache.commons.io.FilenameUtils; |
9 |
import org.apache.commons.io.IOUtils;
|
|
8 |
import org.apache.commons.text.StringEscapeUtils;
|
|
10 | 9 |
import org.apache.commons.lang3.StringUtils; |
11 | 10 |
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters; |
12 | 11 |
import org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileIndex; |
... | ... | |
116 | 115 |
if( line!=null ) { |
117 | 116 |
for (int i = 0; i < line.size(); i++) { |
118 | 117 |
String s = line.get(i); |
119 |
// System.out.println("CSVReaderSuperCSV.read(): bef s ='"+s+"'"); |
|
120 | 118 |
if( s!=null ) { |
121 |
s = s.replaceAll("\\\\n","\n"); |
|
122 |
s = s.replaceAll("\\\\r","\r"); |
|
123 |
line.set(i, s); |
|
124 |
// System.out.println("CSVReaderSuperCSV.read(): aft s ='"+s+"'"); |
|
119 |
line.set(i, unescapeCRLF(s)); |
|
125 | 120 |
} |
126 | 121 |
} |
127 | 122 |
} |
... | ... | |
234 | 229 |
} |
235 | 230 |
} |
236 | 231 |
|
232 |
public static String escapeCRLF(String s) { |
|
233 |
if( s==null ) { |
|
234 |
return s; |
|
235 |
} |
|
236 |
String s1 = s; |
|
237 |
s1 = StringUtils.replace(s1, "\\", "\\\\"); |
|
238 |
s1 = StringUtils.replace(s1, "\n", "\\n"); |
|
239 |
s1 = StringUtils.replace(s1, "\r", "\\r"); |
|
240 |
return s1; |
|
241 |
} |
|
242 |
|
|
243 |
public static String unescapeCRLF(String s) { |
|
244 |
if( s==null || s.indexOf('\\')==-1 ) { |
|
245 |
return s; |
|
246 |
} |
|
247 |
String s1 = s; |
|
248 |
s1 = s.replaceAll("(?:^\\\\n)|(?:([^\\\\])\\\\n)","$1\n"); |
|
249 |
s1 = s1.replaceAll("(?:^\\\\r)|(?:([^\\\\])\\\\n)","$1\r"); |
|
250 |
s1 = StringUtils.replace(s1, "\\\\", "\\"); |
|
251 |
return s1; |
|
252 |
} |
|
253 |
|
|
254 |
|
|
255 |
public static void main(String[] args) { |
|
256 |
String s0 = "\\n{\\n \"ANGULO\":\"0.000\",\\n \"\tEXTO\":\"RAVAL ROIG\\\\r\\\\n - \\\\r\\\\nVIRGEN DEL SOCORRO\",\\n \"LINK_POLIGONO\":\"HVCSGISCODE_ENT_11230100000000001\",\\n \"GEOMETRY\":\"00000000014125fe9b57b4a23441503411cb1c432d\"\\n}"; |
|
257 |
System.out.println("#"+s0+"#"); |
|
258 |
String s1 = s0.replaceAll("(?:^\\\\n)|(?:([^\\\\])\\\\n)","$1\n"); |
|
259 |
System.out.println("#"+s1+"#"); |
|
260 |
String s2 = s1.replaceAll("([^\\\\])\\\\r","$1{r}"); |
|
261 |
System.out.println("#"+s2+"#"); |
|
262 |
String s3 = StringEscapeUtils.unescapeCsv(s0); |
|
263 |
System.out.println("#"+s3+"#"); |
|
264 |
} |
|
237 | 265 |
} |
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 | ||
---|---|---|
797 | 797 |
if( line!=null ) { |
798 | 798 |
for (int i = 0; i < line.size(); i++) { |
799 | 799 |
String s = line.get(i); |
800 |
if( s!=null ) { |
|
801 |
s = s.replaceAll("\\\\n","\n"); |
|
802 |
s = s.replaceAll("\\\\r","\r"); |
|
803 |
line.set(i, s); |
|
804 |
} |
|
800 |
line.set(i, CSVReaderSuperCSV.unescapeCRLF(s)); |
|
805 | 801 |
} |
806 | 802 |
} |
807 | 803 |
return line; |
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/test/resources/org/gvsig/fmap/dal/store/csv/testCreateSource1.csv | ||
---|---|---|
18 | 18 |
17;85;T;8005;20190529121314;20190529;191314;T;Yo yo8;F; ;T;106.21;T;"POINT (-0.6439030698437881 40.89530766155764)" |
19 | 19 |
18;96;T;9006;20190429121314;20190429;201314;T;Yo yo9;F;12107.54321;T; ;T;"POINT (-1.3061826868199504 40.72372835570524)" |
20 | 20 |
19;97;T;1001;20191229121314;20191229;121314;T;Yo yo1;F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
21 |
20;97;T;1001;20191229121314;20191229;121314;T;Yo \nyo20;F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
|
22 |
21;97;T;1001;20191229121314;20191229;121314;T;"Yo \nyo21";F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
|
23 |
22;97;T;1001;20191229121314;20191229;121314;T;"\nYo yo22";F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
|
24 |
23;97;T;1001;20191229121314;20191229;121314;T;Yo "yo23";F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
|
25 |
24;97;T;1001;20191229121314;20191229;121314;T;"Yo ""yo24""";F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
|
26 |
25;97;T;1001;20191229121314;20191229;121314;T;"Yo\\n ""yo25""";F;12345.54321;T;123.21;T;"POINT (-1.4673843010413785 40.51985891271157)" |
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/test/java/org/gvsig/fmap/dal/store/csv/TestCreate.java | ||
---|---|---|
348 | 348 |
checkData(sourceStore); |
349 | 349 |
|
350 | 350 |
} |
351 |
|
|
352 |
public void testCRLFAndQuotes() throws Exception { |
|
353 |
try { |
|
354 |
FeatureStore sourceStore = openSourceStore1(); |
|
355 |
|
|
356 |
List<Feature> features = sourceStore.getFeatures(); |
|
357 |
Feature f20 = features.get(19); |
|
358 |
Feature f21 = features.get(20); |
|
359 |
Feature f22 = features.get(21); |
|
360 |
Feature f23 = features.get(22); |
|
361 |
Feature f24 = features.get(23); |
|
362 |
Feature f25 = features.get(24); |
|
363 |
|
|
364 |
assertEquals("f20", "Yo \nyo20", f20.getString("String")); |
|
365 |
assertEquals("f21", "Yo \nyo21", f21.getString("String")); |
|
366 |
assertEquals("f22", "\nYo yo22", f22.getString("String")); |
|
367 |
|
|
368 |
// Ojo, es lo que hace ahora el lector de CSV, en este caso |
|
369 |
// elimina las comillas dobles, aunque pensamos que deberia mantenerlas. |
|
370 |
// Si esto se corrige el test fallara y tendremos que evaluar el posible |
|
371 |
// impacto en otras partes del codigo. |
|
372 |
assertEquals("f23", "Yo yo23", f23.getString("String")); |
|
373 |
|
|
374 |
assertEquals("f24", "Yo \"yo24\"", f24.getString("String")); |
|
375 |
assertEquals("f25", "Yo\\n \"yo25\"", f25.getString("String")); |
|
376 |
} catch(Exception ex) { |
|
377 |
ex.printStackTrace(); |
|
378 |
throw ex; |
|
379 |
} |
|
380 |
} |
|
351 | 381 |
} |
Also available in: Unified diff