Skip to content

Commit

Permalink
Main update -> Updated arg handling
Browse files Browse the repository at this point in the history
  • Loading branch information
codepressed committed Feb 3, 2023
1 parent 6f8a16f commit 5a64d99
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* It is a console application with var args
* Uses the traditional methodology to generate the xml (without a library), through
of nodes and tree structure.
* Can read ANY CSV File
* Args are:
* Can read ANY CSV File (comma-separated & semicolon-separated)

Args are:
- [0] Input file
- [1] Output file
- [2] Element node name and optionally,
Expand Down
35 changes: 8 additions & 27 deletions src/main/java/com/codepressed/CSVtoXML/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@


import org.w3c.dom.Document;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -23,35 +19,20 @@ public class Main {
public static void main(String[] args) {

//Arg validator
if (args.length == 0) {
if (args.length <2) {
logger.log(Level.SEVERE, "No args were specified.");
System.exit(0);
System.exit(1);
}
//Vars Initialization
String csvFile = args[0];
String xmlFile = args[1];
String elementName;
String csvSplit = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";

try {
elementName = args[2];
} catch (ArrayIndexOutOfBoundsException e) {
logger.log(Level.INFO, "Since you didn't specify a element name, 'element' will be the parental node.");
elementName = "element";
}

try {
if (args[3] == "-s")
csvSplit = ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
} catch (ArrayIndexOutOfBoundsException e){
}
String elementName = args.length>=3 && !args[3].equals("-s") ? args[2] : "element";
String csvSplit = (args.length>3 && args[3].equals("-s") || args.length>4 && args[4].equals("-s")) ? ";(?=([^\"]*\"[^\"]*\")*[^\"]*$)":",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";

List<String[]> elements;
elements = XMLutils.readCsvFile(csvFile, csvSplit);
Document xmlDoc;
xmlDoc = XMLutils.createXmlDocument(elements, elementName);
XMLutils.writeXmlDocumentToFile(xmlDoc, xmlFile);
}
List <String[]> elements = XMLutils.readCsvFile(csvFile, csvSplit);
Document xmlDoc = XMLutils.createXmlDocument(elements, elementName);
XMLutils.writeXmlDocumentToFile(xmlDoc, xmlFile);
}
}


Expand Down

0 comments on commit 5a64d99

Please sign in to comment.