Search in sources :

Example 1 with CSVNameGen

use of com.teradata.jaqy.utils.CSVNameGen in project jaqy by Teradata.

the class CSVExporterFactory method getHandler.

@Override
public JaqyExporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
    Charset charset = DEFAULT_CHARSET;
    CSVFormat format = CSVFormat.DEFAULT;
    HashMap<Integer, CSVExportInfo> exportInfoMap = new HashMap<Integer, CSVExportInfo>();
    CSVNameGen nameGen = new CSVNameGen(DEFAULT_NAME_PATTERN);
    Charset encoding = DEFAULT_CHARSET;
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 'c':
                {
                    charset = Charset.forName(option.getValue());
                    break;
                }
            case 'd':
                {
                    char delimiter = CSVUtils.getChar(option.getValue());
                    if (delimiter == 0)
                        throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
                    format = format.withDelimiter(delimiter);
                    break;
                }
            case 't':
                {
                    format = CSVUtils.getFormat(option.getValue());
                    break;
                }
            case 'n':
                {
                    String fmt = option.getValue();
                    nameGen = new CSVNameGen(fmt);
                    // now check if the name is a valid format.
                    if (fmt.equals(nameGen.getName(1)))
                        interpreter.error("Invalid name pattern: " + fmt);
                    break;
                }
            case 'e':
                {
                    encoding = Charset.forName(option.getValue());
                    break;
                }
            case 'f':
                {
                    int column = Integer.parseInt(option.getValue());
                    if (column < 1) {
                        interpreter.error("Column index cannot be smaller than 1.");
                    }
                    CSVExportInfo info = new CSVExportInfo(nameGen, encoding);
                    exportInfoMap.put(column, info);
                    break;
                }
        }
    }
    String[] args = cmdLine.getArgs();
    if (args.length == 0)
        throw new IllegalArgumentException("missing file name.");
    Path file = interpreter.getPath(args[0]);
    return new CSVExporter(file, charset, format, exportInfoMap);
}
Also used : Path(com.teradata.jaqy.interfaces.Path) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) CSVExportInfo(com.teradata.jaqy.utils.CSVExportInfo) CSVNameGen(com.teradata.jaqy.utils.CSVNameGen) CSVFormat(org.apache.commons.csv.CSVFormat) Option(org.apache.commons.cli.Option)

Aggregations

Path (com.teradata.jaqy.interfaces.Path)1 CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)1 CSVNameGen (com.teradata.jaqy.utils.CSVNameGen)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Option (org.apache.commons.cli.Option)1 CSVFormat (org.apache.commons.csv.CSVFormat)1