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

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dralagen committed Nov 22, 2015
2 parents cdeab0c + 9668165 commit cdb2daa
Show file tree
Hide file tree
Showing 26 changed files with 869 additions and 48 deletions.
37 changes: 26 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>fr.dralagen</groupId>
<artifactId>pom</artifactId>
<version>1.1.0</version>
</parent>

<groupId>fr.dralagen</groupId>
<artifactId>csv2xml</artifactId>
<version>0.2.0-beta-1</version>
<version>0.2.0</version>
<packaging>jar</packaging>

<name>csv2xml</name>
Expand Down Expand Up @@ -42,6 +48,25 @@
<url>https://github.com/dralagen/csv2xml</url>
</scm>

<properties>
<java.mainClass>fr.dralagen.Csv2xml</java.mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down Expand Up @@ -140,24 +165,14 @@
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m4.3</version>
<configuration>
<flowInitContext>
<masterBranchName>master</masterBranchName>
<developBranchName>develop</developBranchName>
<versionTagPrefix />
</flowInitContext>
<autoVersionSubmodules>true</autoVersionSubmodules>
<pushReleases>false</pushReleases>
<allowUntracked>true</allowUntracked>
</configuration>
</plugin>

</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

67 changes: 30 additions & 37 deletions src/main/java/fr/dralagen/Csv2xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
*/
public class Csv2xml {

private DocumentBuilderFactory domFactory = null;
private DocumentBuilder domBuilder = null;
private DocumentBuilder domBuilder = null;

private Document document;

Expand All @@ -55,7 +54,7 @@ public class Csv2xml {

public Csv2xml() {
try {
domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
} catch (FactoryConfigurationError exp) {
System.err.println(exp.toString());
Expand All @@ -80,7 +79,7 @@ public void createNewDocument(String node) {
Element element = document.createElement(node);
document.appendChild(element);

currentElement = (Node) element;
currentElement = element;

}

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

currentElement = (Node) element;
currentElement = element;
}

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

{ // Header row
String text = null;
String text;

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


{ // Data rows
List<String> rowValues = null;
List<String> rowValues;
while ( (rowValues = split(csvReader, delimiter, headers.size())) != null ) {

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

Element curElement = null;
Element curElement;

try
{
Expand All @@ -178,9 +176,7 @@ public int convert(InputStream csv, String delimiter, String nodeRow) {
throw e;
}



curElement.appendChild(document.createTextNode(value));
curElement.appendChild(document.createTextNode(value.replaceAll("\"\"", "\"")));
rowElement.appendChild(curElement);
}

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

if (!fieldOpened) {
// delete the " unnessaisery
if (!fieldOpened && field.length() > 1 && field.charAt(1) != '"') {
// delete the " unnecessary
field = field.substring(1);
fieldOpened = true;
}

fieldOpened = true;
if (fieldOpened) {
++j;
if (j < splited.length) {
while (j < splited.length
&& (splited[j].equals("") || splited[j].charAt(splited[j].length() - 1) != '"')
) {
field += delimiter + splited[j];
++j;
}
}

++j;
if (j < splited.length) {
while ( j < splited.length
&& (splited[j].equals("") || splited[j].charAt(splited[j].length() - 1) != '"')
) {
// we find the end field
if (j < splited.length) {
field += delimiter + splited[j];
++j;
field = field.substring(0, field.length() - 2);
fieldOpened = false;
}
}

// we find the end field
if (j < splited.length) {
field += delimiter + splited[j];
field = field.substring(0, field.length() - 2);
fieldOpened = false;
}
}

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

int startIndex = (fieldOpened) ? 0 : 1;
result.add(field.substring(startIndex, field.length() - 1));
result.add(field.substring(startIndex, Math.max(field.length() - 1, 1)));
fieldOpened = false;
}
else {
Expand All @@ -326,11 +322,10 @@ private List<String> split(LineNumberReader reader, String delimiter, int limit,
i = j+1;
}


// complete line who field contain '\n'
if ( result.size() < limit ) {
List<String> extendsRowValues = null;
if ((extendsRowValues = split(reader, delimiter, limit - result.size(), fieldOpened)) != null) {
List<String> extendsRowValues;
if ((extendsRowValues = split(reader, delimiter, limit - result.size()+1, fieldOpened)) != null) {

int rowValuesLastIndex = result.size() - 1;

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

try {
URL url = new URL(inputName);
Expand Down Expand Up @@ -429,8 +424,6 @@ public static void main (String[] args) {
e.printStackTrace();
}



}
}

82 changes: 82 additions & 0 deletions src/test/java/fr/dralagen/ComaDelimiterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fr.dralagen;

/*
* csv2xml
*
* Copyright (C) 2015 dralagen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import org.apache.commons.io.IOUtils;
import org.junit.Assert;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by dralagen on 11/22/15.
*/
public class ComaDelimiterTest {

private Csv2xml parser;

private InputStream csvInput;

@org.junit.Before
public void setUp() throws Exception {
parser = new Csv2xml();

parser.createNewDocument("test");
parser.setIndentSize(2);

csvInput = SimpleParseTest.class.getResourceAsStream("/csv/comaDelimiter.csv");
}

@org.junit.Test
public void simpleTest() {
parser.convert(csvInput, ",", "field");

ByteArrayOutputStream result = new ByteArrayOutputStream();
parser.writeTo(result);

InputStream expected = SimpleParseTest.class.getResourceAsStream("/xml/simple.xml");

try {
Assert.assertEquals("Simple csv parsing", IOUtils.toString(expected), IOUtils.toString(result.toByteArray(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}

@org.junit.Test
public void simpleCompactTest() {
parser.setCompact(true);
parser.convert(csvInput, ",", "field");

ByteArrayOutputStream result = new ByteArrayOutputStream();
parser.writeTo(result);

InputStream expected = SimpleParseTest.class.getResourceAsStream("/xml/simpleCompact.xml");

try {
Assert.assertEquals("Simple csv parsing on compact mode", IOUtils.toString(expected), IOUtils.toString(result.toByteArray(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}


}
Loading

0 comments on commit cdb2daa

Please sign in to comment.