Skip to content

Commit

Permalink
Checkin of the XML to CSV Converter POC
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wall authored and Michael Wall committed Mar 26, 2024
1 parent 7e0fbbf commit 097d3c0
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/XML-to-CSV-converter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gradle/
build/
target/
3 changes: 3 additions & 0 deletions modules/XML-to-CSV-converter/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bundle-Name: XML-to-CSV-converter
Bundle-SymbolicName: XML-to-CSV-converter
Bundle-Version: 1.0.0
2 changes: 2 additions & 0 deletions modules/XML-to-CSV-converter/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies {
}
108 changes: 108 additions & 0 deletions modules/XML-to-CSV-converter/src/main/java/com/mw/uad/XMLToCSV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.mw.uad;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XMLToCSV {

public interface XSLT_TEMPLATES {
public static final String HEADER = "/header.xsl";
public static final String CONTENT = "/content.xsl";
}

public static void main(String[] args) {
try {
if (args == null || args.length != 2) {
System.out.println("Arguments not as expected. Unable to proceed.");

return;
}

String inputFolderString = args[0];
String outputFolderString = args[1];

System.out.println("inputFolder: " + inputFolderString);
System.out.println("outputFolder: " + outputFolderString);

File inputFolder = new File(inputFolderString);
File outputFolder = new File(outputFolderString);

if (!inputFolder.exists() || !inputFolder.isDirectory() || !inputFolder.canRead()) {
System.out.println("inputFolder not found or cannot be read. Unable to proceed.");

return;
}

if (!outputFolder.exists() || !outputFolder.isDirectory() || !outputFolder.canWrite()) {
System.out.println("outputFolder not found or cannot be written to. Unable to proceed.");

return;
}

TransformerFactory factory = TransformerFactory.newInstance();
Transformer headerTransformer = factory.newTransformer(new StreamSource(ClassLoader.class.getResourceAsStream(XSLT_TEMPLATES.HEADER)));
Transformer contentTransformer = factory.newTransformer(new StreamSource(ClassLoader.class.getResourceAsStream(XSLT_TEMPLATES.CONTENT)));

processFolderContents(inputFolder.listFiles(), inputFolder.getName(), outputFolderString, headerTransformer, contentTransformer);

System.out.println("Done..");

} catch (Exception e) {
e.printStackTrace();
}
}

private static void processFolderContents(File[] files, String parentFolderName, String outputFolderString, Transformer headerTransformer,
Transformer contentTransformer) {

System.out.println("Processing folder: " + parentFolderName);

FileOutputStream outputStream = null;
OutputStreamWriter writer = null;
File outputFile = null;

boolean headerWritten = false;
boolean fileFound = false;

try {
for (File file : files) {
String outputFileName = outputFolderString + parentFolderName + ".csv";

if (file.isDirectory()) {
processFolderContents(file.listFiles(), file.getName(), outputFolderString, headerTransformer, contentTransformer);
} else if (file.isFile() && file.getName() != null && file.getName().toLowerCase().endsWith(".xml")) { // Only interested in XML files...
if (!fileFound) {
fileFound = true;

outputFile = new File(outputFileName);
outputStream = new FileOutputStream(outputFile);
writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);

System.out.println("Created file: " + outputFile.getName());
}

// All XML files in the folder should be the same so same header applies to all
if (!headerWritten) {
headerWritten = true;

headerTransformer.transform(new StreamSource(file), new StreamResult(writer));
}

contentTransformer.transform(new StreamSource(file), new StreamResult(writer));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) try { writer.close(); } catch(Exception e) {};
if (outputStream != null) try { outputStream.close(); } catch(Exception e) {};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mw.uad;

public class XMLToCSVTestHarness {
public static void main(String[] args) {

String[] arguments = new String[2];

arguments[0] = "C:\\mw_temp\\source\\";
arguments[1] = "C:\\mw_temp\\output\\";

XMLToCSV.main(arguments);
}
}
Empty file.
18 changes: 18 additions & 0 deletions modules/XML-to-CSV-converter/src/main/resources/content.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="model"/>
</xsl:template>

<xsl:template match="model">
<xsl:apply-templates select="model-name"/>
<xsl:apply-templates select="column"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="column">
<xsl:value-of select="concat(',', normalize-space(column-value))"/>
</xsl:template>
</xsl:stylesheet>
22 changes: 22 additions & 0 deletions modules/XML-to-CSV-converter/src/main/resources/header.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="model"/>
</xsl:template>

<xsl:template match="model">
<xsl:apply-templates select="model-name"/>
<xsl:apply-templates select="column"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="model-name">
<xsl:text>Model</xsl:text>
</xsl:template>

<xsl:template match="column">
<xsl:value-of select="concat(',', normalize-space(column-name))"/>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 097d3c0

Please sign in to comment.