Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit cdb2daa

Browse files
committed
Merge branch 'release/0.2.0'
2 parents cdeab0c + 9668165 commit cdb2daa

26 files changed

+869
-48
lines changed

pom.xml

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5+
<parent>
6+
<groupId>fr.dralagen</groupId>
7+
<artifactId>pom</artifactId>
8+
<version>1.1.0</version>
9+
</parent>
10+
511
<groupId>fr.dralagen</groupId>
612
<artifactId>csv2xml</artifactId>
7-
<version>0.2.0-beta-1</version>
13+
<version>0.2.0</version>
814
<packaging>jar</packaging>
915

1016
<name>csv2xml</name>
@@ -42,6 +48,25 @@
4248
<url>https://github.com/dralagen/csv2xml</url>
4349
</scm>
4450

51+
<properties>
52+
<java.mainClass>fr.dralagen.Csv2xml</java.mainClass>
53+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
54+
</properties>
55+
56+
<dependencies>
57+
<dependency>
58+
<groupId>junit</groupId>
59+
<artifactId>junit</artifactId>
60+
<version>4.12</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>commons-io</groupId>
64+
<artifactId>commons-io</artifactId>
65+
<version>2.4</version>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
4570
<distributionManagement>
4671
<snapshotRepository>
4772
<id>ossrh</id>
@@ -140,24 +165,14 @@
140165
<plugin>
141166
<groupId>external.atlassian.jgitflow</groupId>
142167
<artifactId>jgitflow-maven-plugin</artifactId>
143-
<version>1.0-m4.3</version>
144168
<configuration>
145169
<flowInitContext>
146-
<masterBranchName>master</masterBranchName>
147-
<developBranchName>develop</developBranchName>
148170
<versionTagPrefix />
149171
</flowInitContext>
150-
<autoVersionSubmodules>true</autoVersionSubmodules>
151-
<pushReleases>false</pushReleases>
152-
<allowUntracked>true</allowUntracked>
153172
</configuration>
154173
</plugin>
155174

156175
</plugins>
157176
</build>
158-
159-
<properties>
160-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
161-
</properties>
162177
</project>
163178

src/main/java/fr/dralagen/Csv2xml.java

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
*/
4343
public class Csv2xml {
4444

45-
private DocumentBuilderFactory domFactory = null;
46-
private DocumentBuilder domBuilder = null;
45+
private DocumentBuilder domBuilder = null;
4746

4847
private Document document;
4948

@@ -55,7 +54,7 @@ public class Csv2xml {
5554

5655
public Csv2xml() {
5756
try {
58-
domFactory = DocumentBuilderFactory.newInstance();
57+
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
5958
domBuilder = domFactory.newDocumentBuilder();
6059
} catch (FactoryConfigurationError exp) {
6160
System.err.println(exp.toString());
@@ -80,7 +79,7 @@ public void createNewDocument(String node) {
8079
Element element = document.createElement(node);
8180
document.appendChild(element);
8281

83-
currentElement = (Node) element;
82+
currentElement = element;
8483

8584
}
8685

@@ -107,7 +106,7 @@ public void addNode(String node) {
107106
Element element = document.createElement(node);
108107
currentElement.appendChild(element);
109108

110-
currentElement = (Node) element;
109+
currentElement = element;
111110
}
112111

113112
/**
@@ -136,7 +135,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
136135
List<String> headers = new ArrayList<String>();
137136

138137
{ // Header row
139-
String text = null;
138+
String text;
140139

141140
// Header row
142141
if ( (text = csvReader.readLine()) != null ) {
@@ -145,9 +144,8 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
145144
}
146145
}
147146

148-
149147
{ // Data rows
150-
List<String> rowValues = null;
148+
List<String> rowValues;
151149
while ( (rowValues = split(csvReader, delimiter, headers.size())) != null ) {
152150

153151
Element rowElement = document.createElement(nodeRow);
@@ -162,7 +160,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
162160
value = rowValues.get(col);
163161
}
164162

165-
Element curElement = null;
163+
Element curElement;
166164

167165
try
168166
{
@@ -178,9 +176,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
178176
throw e;
179177
}
180178

181-
182-
183-
curElement.appendChild(document.createTextNode(value));
179+
curElement.appendChild(document.createTextNode(value.replaceAll("\"\"", "\"")));
184180
rowElement.appendChild(curElement);
185181
}
186182

@@ -283,32 +279,32 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
283279
// find a complex field with delimiter character or multiline
284280
if (!field.equals("")
285281
&& (field.charAt(0) == '"' || fieldOpened)
286-
&& (field.charAt(field.length() - 1) != '"' ||
287-
field.equals("\"") == true)) {
282+
&& (field.charAt(field.length() - 1) != '"')) {
288283

289-
if (!fieldOpened) {
290-
// delete the " unnessaisery
284+
if (!fieldOpened && field.length() > 1 && field.charAt(1) != '"') {
285+
// delete the " unnecessary
291286
field = field.substring(1);
287+
fieldOpened = true;
292288
}
293289

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+
}
295300

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) {
301303
field += delimiter + splited[j];
302-
++j;
304+
field = field.substring(0, field.length() - 2);
305+
fieldOpened = false;
303306
}
304307
}
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-
}
312308
}
313309

314310
// we find a quote field
@@ -317,7 +313,7 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
317313
&& field.charAt(field.length()-1) == '"') {
318314

319315
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)));
321317
fieldOpened = false;
322318
}
323319
else {
@@ -326,11 +322,10 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
326322
i = j+1;
327323
}
328324

329-
330325
// complete line who field contain '\n'
331326
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) {
334329

335330
int rowValuesLastIndex = result.size() - 1;
336331

@@ -372,7 +367,7 @@ public void setIndentSize(int indentSize) {
372367
* @throws java.io.IOException if a error in read the input
373368
*/
374369
public static InputStream getInputStream(String inputName) throws IOException {
375-
InputStream inputStream = null;
370+
InputStream inputStream;
376371

377372
try {
378373
URL url = new URL(inputName);
@@ -429,8 +424,6 @@ public static void main (String[] args) {
429424
e.printStackTrace();
430425
}
431426

432-
433-
434427
}
435428
}
436429

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package fr.dralagen;
2+
3+
/*
4+
* csv2xml
5+
*
6+
* Copyright (C) 2015 dralagen
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
import org.apache.commons.io.IOUtils;
23+
import org.junit.Assert;
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
29+
/**
30+
* Created by dralagen on 11/22/15.
31+
*/
32+
public class ComaDelimiterTest {
33+
34+
private Csv2xml parser;
35+
36+
private InputStream csvInput;
37+
38+
@org.junit.Before
39+
public void setUp() throws Exception {
40+
parser = new Csv2xml();
41+
42+
parser.createNewDocument("test");
43+
parser.setIndentSize(2);
44+
45+
csvInput = SimpleParseTest.class.getResourceAsStream("/csv/comaDelimiter.csv");
46+
}
47+
48+
@org.junit.Test
49+
public void simpleTest() {
50+
parser.convert(csvInput, ",", "field");
51+
52+
ByteArrayOutputStream result = new ByteArrayOutputStream();
53+
parser.writeTo(result);
54+
55+
InputStream expected = SimpleParseTest.class.getResourceAsStream("/xml/simple.xml");
56+
57+
try {
58+
Assert.assertEquals("Simple csv parsing", IOUtils.toString(expected), IOUtils.toString(result.toByteArray(), "UTF-8"));
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
@org.junit.Test
65+
public void simpleCompactTest() {
66+
parser.setCompact(true);
67+
parser.convert(csvInput, ",", "field");
68+
69+
ByteArrayOutputStream result = new ByteArrayOutputStream();
70+
parser.writeTo(result);
71+
72+
InputStream expected = SimpleParseTest.class.getResourceAsStream("/xml/simpleCompact.xml");
73+
74+
try {
75+
Assert.assertEquals("Simple csv parsing on compact mode", IOUtils.toString(expected), IOUtils.toString(result.toByteArray(), "UTF-8"));
76+
} catch (IOException e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
81+
82+
}

0 commit comments

Comments
 (0)