From 081bf82811c0c7b9a1eeaeaf64575851bd3a0c9d Mon Sep 17 00:00:00 2001 From: "james.carr@preservica.com" Date: Thu, 25 Jun 2020 15:57:07 +0100 Subject: [PATCH] add additional header prefix --- CSV2Metadata.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/CSV2Metadata.java b/CSV2Metadata.java index ec4ee4e..f4dc133 100644 --- a/CSV2Metadata.java +++ b/CSV2Metadata.java @@ -91,13 +91,14 @@ public static void main(String[] args) { options.addOption( "o", "output", true, "the folder which will contain the xml documents" ); options.addOption( "r", "root", true, "the root element of the dublin core xml, defaults to dc" ); options.addOption( "n", "namespace", true, "the root element namespace, defaults to http://purl.org/dc/elements/1.1/" ); + options.addOption( "hp", "header-prefix", true, "the column header prefix in addition to dc & dcterms" ); options.addOption( "p", "prefix", true, "the root element namespace prefix, defaults to dc" ); options.addOption( "u", "user", true, "the property file with Preservica username & password" ); options.addOption( "h", "help", false, "print this message" ); HelpFormatter formatter = new HelpFormatter(); - final String cmdLine = "csv2dc.cmd -i file.csv -o output [-c \"file name column\"] [-r root] [-p prefix] [-n namespace]"; + final String cmdLine = "csv2dc.cmd -i file.csv -o output [-c \"file name column\"] [-r root] [-p prefix] [-n namespace] [-hp header-prefix]"; String DEFAULT_FILE_COLUMN = "filename"; String DEFAULT_ROOT_ELEMENT = "dc"; @@ -106,6 +107,7 @@ public static void main(String[] args) { String fileColumn; String rootElement; String rootPrefix; + String headerPrefix = null; String rootNamespace; File inputFile = null; File outputDir = null; @@ -132,6 +134,11 @@ public static void main(String[] args) { userDetails.load(new FileInputStream(properties)); } + + if ( line.hasOption( "hp" ) ) { + headerPrefix = line.getOptionValue( "hp" ); + } + if ( line.hasOption( "p" ) ) { rootPrefix = line.getOptionValue( "p" ); } else { @@ -179,7 +186,7 @@ public static void main(String[] args) { try { CSV2Metadata metadata = new CSV2Metadata(userDetails); - int files = metadata.parse(inputFile, outputDir, fileColumn, rootElement, rootPrefix, rootNamespace); + int files = metadata.parse(inputFile, outputDir, fileColumn, rootElement, rootPrefix, rootNamespace, headerPrefix); System.out.println(String.format("Created %d XML files in %s", files, outputDir.getName())); } catch (Exception e) { formatter.printHelp( cmdLine, options ); @@ -221,7 +228,7 @@ private String getClosingElement(String element) { * @param folder The output folder * @throws Exception */ - private int parse(File csvDocument, File folder, String filenameColumn, String rootElement, String rootPrefix, String rootNamespace) throws Exception { + private int parse(File csvDocument, File folder, String filenameColumn, String rootElement, String rootPrefix, String rootNamespace, String headerPrefix) throws Exception { final String DC_NS = "xmlns:dc=\"http://purl.org/dc/elements/1.1/\""; final String DCTERMS_NS = "xmlns:dcterms=\"http://purl.org/dc/terms/\""; @@ -291,8 +298,12 @@ private int parse(File csvDocument, File folder, String filenameColumn, String r for (int i = 0; i < headerCount; i++) { String header = headers[i]; + boolean isAddtional = false; boolean isDublinCore = (header.startsWith("dc:") || header.startsWith("dcterms:")); - if (isDublinCore) { + if (headerPrefix != null) { + isAddtional = header.startsWith(headerPrefix + ":"); + } + if (isDublinCore || isAddtional) { String value = record.get(i).trim(); String closingElement = getClosingElement(header); if (value == null || value.isEmpty()) {