|
| 1 | + import org.apache.commons.cli.CommandLine; |
| 2 | + import org.apache.commons.cli.CommandLineParser; |
| 3 | + import org.apache.commons.cli.DefaultParser; |
| 4 | + import org.apache.commons.cli.HelpFormatter; |
| 5 | + import org.apache.commons.cli.Option; |
| 6 | + import org.apache.commons.cli.Options; |
| 7 | + import org.apache.commons.cli.ParseException; |
| 8 | + import org.openas2.util.FileUtil; |
| 9 | + import java.io.File; |
| 10 | + |
| 11 | + /** |
| 12 | + * Class used to add the server's certificate to the KeyStore with your trusted |
| 13 | + * certificates. |
| 14 | + */ |
| 15 | + public class SplitCsvFile { |
| 16 | + |
| 17 | + public static final String SOURCE_FILE = "s"; |
| 18 | + public static final String OUT_FILENAME_PREFIX = "p"; |
| 19 | + public static final String OUTPUT_DIR = "o"; |
| 20 | + public static final String MAX_FILE_SIZE = "m"; |
| 21 | + public static final String HAS_HEADER_ROW = "h"; |
| 22 | + public static final String DEBUG = "d"; |
| 23 | + public static final String HELP_OPT = "h"; |
| 24 | + |
| 25 | + /* |
| 26 | + * Options in this format: short-opt, long-opt, has-argument, required, |
| 27 | + * description |
| 28 | + */ |
| 29 | + public String[][] opts = { |
| 30 | + {SOURCE_FILE, "source", "true", "true", "the source file name including path"}, |
| 31 | + {MAX_FILE_SIZE, "max_size", "true", "true", "the maximum size of the files"}, |
| 32 | + {HAS_HEADER_ROW, "has_header", "false", "false", "if set, the file has a header row that wil be replicated into every file"}, |
| 33 | + {OUT_FILENAME_PREFIX, "out_file_prefix", "true", "false", "the prefix for the split file names"}, |
| 34 | + {OUTPUT_DIR, "out_dir", "true", "false", "output directory for the split files - defaults to current dir"}, |
| 35 | + {DEBUG, "debug", "true", "false", "Enabling debug logging"}, |
| 36 | + {HELP_OPT, "help", "false", "false", "print this help"} |
| 37 | + }; |
| 38 | + |
| 39 | + private void usage(Options options) { |
| 40 | + String header = "Splits CSV file." + "\nReads the file as a line based file creating new files that will not exceed the specified maximum size."; |
| 41 | + String footer = "NOTE: The file is expected to contain lines separated by newline characters."; |
| 42 | + |
| 43 | + HelpFormatter formatter = new HelpFormatter(); |
| 44 | + formatter.printHelp(this.getClass().getName(), header, options, footer, true); |
| 45 | + } |
| 46 | + |
| 47 | + private CommandLine parseCommandLine(String[] args) { |
| 48 | + // create the command line parser |
| 49 | + CommandLineParser parser = new DefaultParser(); |
| 50 | + |
| 51 | + // create the Options |
| 52 | + Options options = new Options(); |
| 53 | + for (String[] opt : opts) { |
| 54 | + Option option = Option.builder(opt[0]).longOpt(opt[1]).hasArg("true".equalsIgnoreCase(opt[2])).desc(opt[4]).build(); |
| 55 | + option.setRequired("true".equalsIgnoreCase(opt[3])); |
| 56 | + options.addOption(option); |
| 57 | + } |
| 58 | + |
| 59 | + // parse the command line arguments |
| 60 | + CommandLine line = null; |
| 61 | + try { |
| 62 | + line = parser.parse(options, args); |
| 63 | + } catch (ParseException e) { |
| 64 | + System.out.println("Command line parsing error: " + e.getMessage()); |
| 65 | + usage(options); |
| 66 | + System.exit(-1); |
| 67 | + } |
| 68 | + return line; |
| 69 | + } |
| 70 | + |
| 71 | + private void process(String[] args) throws Exception { |
| 72 | + CommandLine options = parseCommandLine(args); |
| 73 | + if (options == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + String sourceFileName = options.getOptionValue(SOURCE_FILE); |
| 77 | + File sourceFile = new File(sourceFileName); |
| 78 | + if (!sourceFile.exists()) { |
| 79 | + throw new Exception("File does not exist: " + sourceFileName); |
| 80 | + } |
| 81 | + long maxFileSize = Long.parseLong(options.getOptionValue(MAX_FILE_SIZE)); |
| 82 | + String outputDirName = null; |
| 83 | + if (options.hasOption(OUTPUT_DIR)) { |
| 84 | + outputDirName = options.getOptionValue(OUTPUT_DIR); |
| 85 | + } else { |
| 86 | + outputDirName = System.getProperty("user.dir"); |
| 87 | + } |
| 88 | + String prefix = (options.hasOption(OUT_FILENAME_PREFIX)) ? options.getOptionValue(OUT_FILENAME_PREFIX) : ""; |
| 89 | + boolean hasHeaderRow = options.hasOption(HAS_HEADER_ROW); |
| 90 | + |
| 91 | + if (options.hasOption(DEBUG) && "true".equalsIgnoreCase(options.getOptionValue(DEBUG))) { |
| 92 | + System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); |
| 93 | + System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); |
| 94 | + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG"); |
| 95 | + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "ERROR"); |
| 96 | + } |
| 97 | + try { |
| 98 | + FileUtil.splitLineBasedFile(sourceFile, outputDirName, maxFileSize, hasHeaderRow, sourceFile.getName(), prefix); |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + System.out.println("Processing error occurred: " + e.getMessage()); |
| 102 | + System.exit(-1); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) { |
| 107 | + try { |
| 108 | + SplitCsvFile mgr = new SplitCsvFile(); |
| 109 | + mgr.process(args); |
| 110 | + System.exit(0); |
| 111 | + } catch (Exception e) { |
| 112 | + System.out.println("Processing error occurred: " + e.getMessage()); |
| 113 | + System.exit(-1); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
0 commit comments