package org.dcsdk12.client; import au.com.bytecode.opencsv.CSVReader; import org.apache.commons.cli.*; import java.io.*; public class CSV2HDA { /** * Main method */ public static void main (String args []) { String mapValue = ""; String srcValue = ""; String outValue = ""; String defValue = ""; java.util.ArrayList mapNames = new java.util.ArrayList(); java.util.ArrayList srcNames = new java.util.ArrayList(); java.util.HashMap defNames = new java.util.HashMap(); Option map = OptionBuilder.withArgName("file") .hasArg() .withDescription("use given file for mapping") .create("map"); Option src = OptionBuilder.withArgName("file") .hasArg() .withDescription("use given file for source") .create("src"); Option out = OptionBuilder.withArgName("file") .hasArg() .withDescription("use given file for output") .create("out"); Option def = OptionBuilder.withArgName("file") .hasArg() .withDescription("use given file for defaults") .create("def"); Options options = new Options(); options.addOption( map ); options.addOption( src ); options.addOption( out ); options.addOption( def ); // create the parser CommandLineParser parser = new GnuParser(); try { // parse the command line arguments CommandLine line = parser.parse( options, args ); if( line.hasOption( "map" ) ) { // initialise the member variable mapValue = line.getOptionValue( "map" ); } if( line.hasOption( "src" ) ) { // initialise the member variable srcValue = line.getOptionValue( "src" ); } if( line.hasOption( "out" ) ) { // initialise the member variable outValue = line.getOptionValue( "out" ); } if( line.hasOption( "def" ) ) { // initialise the member variable defValue = line.getOptionValue( "def" ); } } catch( ParseException exp ) { // oops, something went wrong System.err.println( "Parsing failed. Reason: " + exp.getMessage() ); System.exit(0); } // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp( "java", options ); mapNames = getMapInfo(mapValue); defNames = getDefInfo(defValue); srcNames = getSrcInfo(srcValue, mapNames); writeOutputFile(outValue, defNames, mapNames, srcNames); } /** * Get and Return the mapping information */ private static java.util.ArrayList getMapInfo(String mapValue) { java.util.ArrayList arrayList = new java.util.ArrayList(); try { CSVReader reader = new CSVReader(new FileReader(mapValue)); String [] nextLine; while ((nextLine = reader.readNext()) != null) { for (int i=0;i getDefInfo(String defValue) { java.util.HashMap defList = new java.util.HashMap(); // Get the default information // Read properties file. // System.out.println("defValue = " + defValue); try { java.util.Properties properties = new java.util.Properties(); properties.load(new FileInputStream(defValue)); // java.util.Enumeration propEnum = properties.propertyNames(); java.util.Enumeration propEnum = properties.propertyNames(); while (propEnum.hasMoreElements()) { String theKey = (String) propEnum.nextElement(); String theValue = (String) properties.getProperty(theKey); defList.put(theKey, theValue); // System.out.println("key: " + theKey + "\tvalue: " + theValue); } } catch (IOException ioe) { System.err.println("def - IOException: "+ioe.getMessage()); // System.exit(0); } catch (Exception e) { System.err.println("def - Exception: "+e.getMessage()); // System.exit(0); } return defList; } /** * Get and Return the source information */ private static java.util.ArrayList getSrcInfo( String srcValue, java.util.ArrayList mapNames ) { java.util.ArrayList arrayList = new java.util.ArrayList(); // Get the src information // Store file into arraylist try { CSVReader reader = new CSVReader(new FileReader(srcValue)); String [] nextLine; while ((nextLine = reader.readNext()) != null) { java.util.HashMap theData = new java.util.HashMap(); // We store all mapName values, even if empty. for (int i=0;i defNames, java.util.ArrayList mapNames, java.util.ArrayList srcNames) { // Create the output file try { PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outValue))); for(int i=0;i theData = new java.util.HashMap(); theData = (java.util.HashMap)srcNames.get(i); for (int j=0;j>"); } writer.flush(); writer.close(); } catch (Exception e) { System.err.println("out - Exception: "+e.getMessage()); e.printStackTrace(System.out); System.exit(0); } } }