8000 Main update -> Updated arg handling · codepressed/Java-CSV2XML@5a64d99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a64d99

Browse files
committed
Main update -> Updated arg handling
1 parent 6f8a16f commit 5a64d99

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* It is a console application with var args
33
* Uses the traditional methodology to generate the xml (without a library), through
44
of nodes and tree structure.
5-
* Can read ANY CSV File
6-
* Args are:
5+
* Can read ANY CSV File (comma-separated & semicolon-separated)
6+
7+
Args are:
78
- [0] Input file
89
- [1] Output file
910
- [2] Element node name and optionally,

src/main/java/com/codepressed/CSVtoXML/Main.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33

44
import org.w3c.dom.Document;
5-
import javax.xml.parsers.ParserConfigurationException;
6-
import javax.xml.transform.TransformerException;
7-
import java.io.IOException;
8-
import java.util.ArrayList;
95
import java.util.List;
106
import java.util.logging.Level;
117
import java.util.logging.Logger;
@@ -23,35 +19,20 @@ public class Main {
2319
public static void main(String[] args) {
2420

2521
//Arg validator
26-
if (args.length == 0) {
22+
if (args.length <2) {
2723
logger.log(Level.SEVERE, "No args were specified.");
28-
System.exit(0);
24+
System.exit(1);
2925
}
3026
//Vars Initialization
3127
String csvFile = args[0];
3228
String xmlFile = args[1];
33-
String elementName;
34-
String csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
35-
36-
try {
37-
elementName = args[2];
38-
} catch (ArrayIndexOutOfBoundsException e) {
39-
logger.log(Level.INFO, "Since you didn't specify a element name, 'element' will be the parental node.");
40-
elementName = "element";
41-
}
42-
43-
try {
44-
if (args[3] == "-s")
45-
csvSplit = ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
46-
} catch (ArrayIndexOutOfBoundsException e){
47-
}
29+
String elementName = args.length>=3 && !args[3].equals("-s") ? args[2] : "element";
30+
String csvSplit = (args.length>3 && args[3].equals("-s") || args.length>4 && args[4].equals("-s")) ? ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)":",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
4831

49-
List<String[]> elements;
50-
elements = XMLutils.readCsvFile(csvFile, csvSplit);
51-
Document xmlDoc;
52-
xmlDoc = XMLutils.createXmlDocument(elements, elementName);
53-
XMLutils.writeXmlDocumentToFile(xmlDoc, xmlFile);
54-
}
32+
List <String[]> elements = XMLutils.readCsvFile(csvFile, csvSplit);
33+
Document xmlDoc = XMLutils.createXmlDocument(elements, elementName);
34+
XMLutils.writeXmlDocumentToFile(xmlDoc, xmlFile);
35+
}
5536
}
5637

5738

0 commit comments

Comments
 (0)
0