42
42
*/
43
43
public class Csv2xml {
44
44
45
- private DocumentBuilderFactory domFactory = null ;
46
- private DocumentBuilder domBuilder = null ;
45
+ private DocumentBuilder domBuilder = null ;
47
46
48
47
private Document document ;
49
48
@@ -55,7 +54,7 @@ public class Csv2xml {
55
54
56
55
public Csv2xml () {
57
56
try {
58
- domFactory = DocumentBuilderFactory .newInstance ();
57
+ DocumentBuilderFactory domFactory = DocumentBuilderFactory .newInstance ();
59
58
domBuilder = domFactory .newDocumentBuilder ();
60
59
} catch (FactoryConfigurationError exp ) {
61
60
System .err .println (exp .toString ());
@@ -80,7 +79,7 @@ public void createNewDocument(String node) {
80
79
Element element = document .createElement (node );
81
80
document .appendChild (element );
82
81
83
- currentElement = ( Node ) element ;
82
+ currentElement = element ;
84
83
85
84
}
86
85
@@ -107,7 +106,7 @@ public void addNode(String node) {
107
106
Element element = document .createElement (node );
108
107
currentElement .appendChild (element );
109
108
110
- currentElement = ( Node ) element ;
109
+ currentElement = element ;
111
110
}
112
111
113
112
/**
@@ -136,7 +135,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
136
135
List <String > headers = new ArrayList <String >();
137
136
138
137
{ // Header row
139
- String text = null ;
138
+ String text ;
140
139
141
140
// Header row
142
141
if ( (text = csvReader .readLine ()) != null ) {
@@ -145,9 +144,8 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
145
144
}
146
145
}
147
146
148
-
149
147
{ // Data rows
150
- List <String > rowValues = null ;
148
+ List <String > rowValues ;
151
149
while ( (rowValues = split (csvReader , delimiter , headers .size ())) != null ) {
152
150
153
151
Element rowElement = document .createElement (nodeRow );
@@ -162,7 +160,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
162
160
value = rowValues .get (col );
163
161
}
164
162
165
- Element curElement = null ;
163
+ Element curElement ;
166
164
167
165
try
168
166
{
@@ -178,9 +176,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
178
176
throw e ;
179
177
}
180
178
181
-
182
-
183
- curElement .appendChild (document .createTextNode (value ));
179
+ curElement .appendChild (document .createTextNode (value .replaceAll ("\" \" " , "\" " )));
184
180
rowElement .appendChild (curElement );
185
181
}
186
182
@@ -283,32 +279,32 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
283
279
// find a complex field with delimiter character or multiline
284
280
if (!field .equals ("" )
285
281
&& (field .charAt (0 ) == '"' || fieldOpened )
286
- && (field .charAt (field .length () - 1 ) != '"' ||
287
- field .equals ("\" " ) == true )) {
282
+ && (field .charAt (field .length () - 1 ) != '"' )) {
288
283
289
- if (!fieldOpened ) {
290
- // delete the " unnessaisery
284
+ if (!fieldOpened && field . length () > 1 && field . charAt ( 1 ) != '"' ) {
285
+ // delete the " unnecessary
291
286
field = field .substring (1 );
287
+ fieldOpened = true ;
292
288
}
293
289
294
- fieldOpened = true ;
290
+ if (fieldOpened ) {
291
+ ++j ;
292
+ if (j < splited .length ) {
293
+ while (j < splited .length
294
+ && (splited [j ].equals ("" ) || splited [j ].charAt (splited [j ].length () - 1 ) != '"' )
295
+ ) {
296
+ field += delimiter + splited [j ];
297
+ ++j ;
298
+ }
299
+ }
295
300
296
- ++j ;
297
- if (j < splited .length ) {
298
- while ( j < splited .length
299
- && (splited [j ].equals ("" ) || splited [j ].charAt (splited [j ].length () - 1 ) != '"' )
300
- ) {
301
+ // we find the end field
302
+ if (j < splited .length ) {
301
303
field += delimiter + splited [j ];
302
- ++j ;
304
+ field = field .substring (0 , field .length () - 2 );
305
+ fieldOpened = false ;
303
306
}
304
307
}
305
-
306
- // we find the end field
307
- if (j < splited .length ) {
308
- field += delimiter + splited [j ];
309
- field = field .substring (0 , field .length () - 2 );
310
- fieldOpened = false ;
311
- }
312
308
}
313
309
314
310
// we find a quote field
@@ -317,7 +313,7 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
317
313
&& field .charAt (field .length ()-1 ) == '"' ) {
318
314
319
315
int startIndex = (fieldOpened ) ? 0 : 1 ;
320
- result .add (field .substring (startIndex , field .length () - 1 ));
316
+ result .add (field .substring (startIndex , Math . max ( field .length () - 1 , 1 ) ));
321
317
fieldOpened = false ;
322
318
}
323
319
else {
@@ -326,11 +322,10 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
326
322
i = j +1 ;
327
323
}
328
324
329
-
330
325
// complete line who field contain '\n'
331
326
if ( result .size () < limit ) {
332
- List <String > extendsRowValues = null ;
333
- if ((extendsRowValues = split (reader , delimiter , limit - result .size (), fieldOpened )) != null ) {
327
+ List <String > extendsRowValues ;
328
+ if ((extendsRowValues = split (reader , delimiter , limit - result .size ()+ 1 , fieldOpened )) != null ) {
334
329
335
330
int rowValuesLastIndex = result .size () - 1 ;
336
331
@@ -372,7 +367,7 @@ public void setIndentSize(int indentSize) {
372
367
* @throws java.io.IOException if a error in read the input
373
368
*/
374
369
public static InputStream getInputStream (String inputName ) throws IOException {
375
- InputStream inputStream = null ;
370
+ InputStream inputStream ;
376
371
377
372
try {
378
373
URL url = new URL (inputName );
@@ -429,8 +424,6 @@ public static void main (String[] args) {
429
424
e .printStackTrace ();
430
425
}
431
426
432
-
433
-
434
427
}
435
428
}
436
429
0 commit comments