Example usage for org.apache.commons.csv CSVFormat withQuote

List of usage examples for org.apache.commons.csv CSVFormat withQuote

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat withQuote.

Prototype

public CSVFormat withQuote(final Character quoteChar) 

Source Link

Document

Sets the quoteChar of the format to the specified character.

Usage

From source file:com.datascience.hadoop.CsvOutputFormat.java

/**
 * Creates a CSV format from a Hadoop configuration.
 *//*from   w w w.j a v  a2 s  . c o m*/
private static CSVFormat createFormat(Configuration conf) {
    CSVFormat format = CSVFormat
            .newFormat(conf.get(CSV_WRITER_DELIMITER, DEFAULT_CSV_WRITER_DELIMITER).charAt(0))
            .withSkipHeaderRecord(conf.getBoolean(CSV_WRITER_SKIP_HEADER, DEFAULT_CSV_WRITER_SKIP_HEADER))
            .withRecordSeparator(conf.get(CSV_WRITER_RECORD_SEPARATOR, DEFAULT_CSV_WRITER_RECORD_SEPARATOR))
            .withIgnoreEmptyLines(
                    conf.getBoolean(CSV_WRITER_IGNORE_EMPTY_LINES, DEFAULT_CSV_WRITER_IGNORE_EMPTY_LINES))
            .withIgnoreSurroundingSpaces(conf.getBoolean(CSV_WRITER_IGNORE_SURROUNDING_SPACES,
                    DEFAULT_CSV_WRITER_IGNORE_SURROUNDING_SPACES))
            .withNullString(conf.get(CSV_WRITER_NULL_STRING, DEFAULT_CSV_WRITER_NULL_STRING));

    String[] header = conf.getStrings(CSV_WRITER_COLUMNS);
    if (header != null && header.length > 0)
        format = format.withHeader(header);

    String escape = conf.get(CSV_WRITER_ESCAPE_CHARACTER, DEFAULT_CSV_WRITER_ESCAPE_CHARACTER);
    if (escape != null)
        format = format.withEscape(escape.charAt(0));

    String quote = conf.get(CSV_WRITER_QUOTE_CHARACTER, DEFAULT_CSV_WRITER_QUOTE_CHARACTER);
    if (quote != null)
        format = format.withQuote(quote.charAt(0));

    String quoteMode = conf.get(CSV_WRITER_QUOTE_MODE, DEFAULT_CSV_WRITER_QUOTE_MODE);
    if (quoteMode != null)
        format = format.withQuoteMode(QuoteMode.valueOf(quoteMode));
    return format;
}

From source file:com.datascience.hadoop.CsvInputFormat.java

/**
 * Creates a CSV format from a Hadoop configuration.
 *///from  w ww .j ava 2 s.c  o  m
private static CSVFormat createFormat(Configuration conf) {
    CSVFormat format = CSVFormat
            .newFormat(conf.get(CSV_READER_DELIMITER, DEFAULT_CSV_READER_DELIMITER).charAt(0))
            .withSkipHeaderRecord(conf.getBoolean(CSV_READER_SKIP_HEADER, DEFAULT_CSV_READER_SKIP_HEADER))
            .withRecordSeparator(conf.get(CSV_READER_RECORD_SEPARATOR, DEFAULT_CSV_READER_RECORD_SEPARATOR))
            .withIgnoreEmptyLines(
                    conf.getBoolean(CSV_READER_IGNORE_EMPTY_LINES, DEFAULT_CSV_READER_IGNORE_EMPTY_LINES))
            .withIgnoreSurroundingSpaces(conf.getBoolean(CSV_READER_IGNORE_SURROUNDING_SPACES,
                    DEFAULT_CSV_READER_IGNORE_SURROUNDING_SPACES))
            .withNullString(conf.get(CSV_READER_NULL_STRING, DEFAULT_CSV_READER_NULL_STRING));

    String[] header = conf.getStrings(CSV_READER_COLUMNS);
    if (header != null && header.length > 0)
        format = format.withHeader(header);

    String escape = conf.get(CSV_READER_ESCAPE_CHARACTER, DEFAULT_CSV_READER_ESCAPE_CHARACTER);
    if (escape != null)
        format = format.withEscape(escape.charAt(0));

    String quote = conf.get(CSV_READER_QUOTE_CHARACTER, DEFAULT_CSV_READER_QUOTE_CHARACTER);
    if (quote != null)
        format = format.withQuote(quote.charAt(0));

    String quoteMode = conf.get(CSV_READER_QUOTE_MODE, DEFAULT_CSV_READER_QUOTE_MODE);
    if (quoteMode != null)
        format = format.withQuoteMode(QuoteMode.valueOf(quoteMode));
    return format;
}

From source file:biz.ganttproject.impex.csv.GanttCSVExport.java

private CSVFormat getCSVFormat() {
    CSVFormat format = CSVFormat.DEFAULT.withEscape('\\');
    if (myCsvOptions.sSeparatedChar.length() == 1) {
        format = format.withDelimiter(myCsvOptions.sSeparatedChar.charAt(0));
    }//from   w  ww  .  ja v  a 2  s .com
    if (myCsvOptions.sSeparatedTextChar.length() == 1) {
        format = format.withQuote(myCsvOptions.sSeparatedTextChar.charAt(0));
    }

    return format;
}

From source file:ca.uhn.fhir.jpa.term.TerminologyLoaderSvc.java

private void iterateOverZipFile(List<byte[]> theZipBytes, String fileNamePart, IRecordHandler handler,
        char theDelimiter, QuoteMode theQuoteMode) {
    boolean found = false;

    for (byte[] nextZipBytes : theZipBytes) {
        ZipInputStream zis = new ZipInputStream(
                new BufferedInputStream(new ByteArrayInputStream(nextZipBytes)));
        try {/*ww w.j  a  v  a  2 s.c o  m*/
            for (ZipEntry nextEntry; (nextEntry = zis.getNextEntry()) != null;) {
                ZippedFileInputStream inputStream = new ZippedFileInputStream(zis);

                String nextFilename = nextEntry.getName();
                if (nextFilename.contains(fileNamePart)) {
                    ourLog.info("Processing file {}", nextFilename);
                    found = true;

                    Reader reader = null;
                    CSVParser parsed = null;
                    try {
                        reader = new InputStreamReader(zis, Charsets.UTF_8);
                        CSVFormat format = CSVFormat.newFormat(theDelimiter).withFirstRecordAsHeader();
                        if (theQuoteMode != null) {
                            format = format.withQuote('"').withQuoteMode(theQuoteMode);
                        }
                        parsed = new CSVParser(reader, format);
                        Iterator<CSVRecord> iter = parsed.iterator();
                        ourLog.debug("Header map: {}", parsed.getHeaderMap());

                        int count = 0;
                        int logIncrement = LOG_INCREMENT;
                        int nextLoggedCount = 0;
                        while (iter.hasNext()) {
                            CSVRecord nextRecord = iter.next();
                            handler.accept(nextRecord);
                            count++;
                            if (count >= nextLoggedCount) {
                                ourLog.info(" * Processed {} records in {}", count, nextFilename);
                                nextLoggedCount += logIncrement;
                            }
                        }

                    } catch (IOException e) {
                        throw new InternalErrorException(e);
                    }
                }
            }
        } catch (IOException e) {
            throw new InternalErrorException(e);
        } finally {
            IOUtils.closeQuietly(zis);
        }
    }

    // This should always be true, but just in case we've introduced a bug...
    Validate.isTrue(found);
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java

/**
 * Parses a sample file to allow schema specification when creating a new feed.
 *
 * @param sampleText the sample text/*w ww . j  a  v  a  2 s. c o m*/
 * @return A configured parser
 * @throws IOException If there is an error parsing the sample file
 */
public CSVFormat detectCSVFormat(String sampleText, boolean headerRow, String seperatorStr) throws IOException {
    CSVFormat format = CSVFormat.DEFAULT.withAllowMissingColumnNames();
    Character separatorChar = null;
    if (StringUtils.isNotBlank(seperatorStr)) {
        separatorChar = seperatorStr.charAt(0);
    }
    try (BufferedReader br = new BufferedReader(new StringReader(sampleText))) {
        List<LineStats> lineStats = generateStats(br, separatorChar);
        Character quote = guessQuote(lineStats);
        Character delim = guessDelimiter(lineStats, sampleText, quote, headerRow);
        if (delim == null) {
            throw new IOException("Unrecognized format");
        }
        format = format.withDelimiter(delim);
        format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(quote);
    }
    return format;
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVFileSchemaParser.java

private CSVFormat createCSVFormat(String sampleData) throws IOException {
    CSVFormat format;
    if (autoDetect) {
        CSVAutoDetect autoDetect = new CSVAutoDetect();
        format = autoDetect.detectCSVFormat(sampleData, this.headerRow, this.separatorChar);
        this.separatorChar = Character.toString(format.getDelimiter());
        this.quoteChar = Character.toString(format.getQuoteCharacter());
    } else {/*from   w  w w.ja va  2s.c o  m*/
        format = CSVFormat.DEFAULT.withAllowMissingColumnNames();

        if (StringUtils.isNotEmpty(separatorChar)) {
            format = format.withDelimiter(toChar(separatorChar).charAt(0));
        }
        if (StringUtils.isNotEmpty(escapeChar)) {
            format = format.withEscape(toChar(escapeChar).charAt(0));
        }
        if (StringUtils.isNotEmpty(quoteChar)) {
            format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(toChar(quoteChar).charAt(0));
        }
    }

    return format;
}

From source file:org.apache.batchee.csv.CSVFormatFactory.java

static CSVFormat newFormat(final String format, final String delimiter, final String quoteCharacter,
        final String quoteMode, final String commentMarker, final String escapeCharacter,
        final String ignoreSurroundingSpaces, final String ignoreEmptyLines, final String recordSeparator,
        final String nullString, final String headerComments, final String header,
        final String skipHeaderRecord, final String allowMissingColumnNames, final String readHeaders) {
    //CHECKSTYLE:ON
    CSVFormat out = format == null ? CSVFormat.DEFAULT : CSVFormat.valueOf(format);
    if (delimiter != null) {
        out = out.withDelimiter(delimiter.charAt(0));
    }/*from  w ww . jav a  2 s .  c o  m*/
    if (quoteCharacter != null) {
        out = out.withQuote(quoteCharacter.charAt(0));
    }
    if (quoteMode != null) {
        out = out.withQuoteMode(QuoteMode.valueOf(quoteMode));
    }
    if (commentMarker != null) {
        out = out.withCommentMarker(commentMarker.charAt(0));
    }
    if (escapeCharacter != null) {
        out = out.withEscape(escapeCharacter.charAt(0));
    }
    if (ignoreSurroundingSpaces != null) {
        out = out.withIgnoreSurroundingSpaces(Boolean.parseBoolean(ignoreSurroundingSpaces));
    }
    if (ignoreEmptyLines != null) {
        out = out.withIgnoreEmptyLines(Boolean.parseBoolean(ignoreEmptyLines));
    }
    if (recordSeparator != null) {
        if ("\\n".equals(recordSeparator)) {
            out = out.withRecordSeparator('\n');
        } else if ("\\r\\n".equals(recordSeparator)) {
            out = out.withRecordSeparator("\r\n");
        } else {
            out = out.withRecordSeparator(recordSeparator);
        }
    }
    if (nullString != null) {
        out = out.withNullString(nullString);
    }
    if (headerComments != null && !headerComments.trim().isEmpty()) {
        out = out.withHeaderComments(headerComments.split(" *, *"));
    }
    if (Boolean.parseBoolean(readHeaders)) {
        out = out.withHeader();
    }
    if (header != null && !header.trim().isEmpty()) {
        try { // headers can have CSV header names so parse it there
            final Iterator<CSVRecord> iterator = out.withHeader(new String[0])
                    .parse(new StringReader(header + '\n' + header)).iterator();
            final CSVRecord record = iterator.next();
            final List<String> list = new ArrayList<String>(record.size());
            for (final String h : record) {
                list.add(h);
            }
            out = out.withHeader(list.toArray(new String[record.size()]));
        } catch (final IOException e) { // can't occur actually
            out = out.withHeader(header.split(" *, *"));
        }
    }
    if (skipHeaderRecord != null) {
        out = out.withSkipHeaderRecord(Boolean.parseBoolean(skipHeaderRecord));
    }
    if (allowMissingColumnNames != null) {
        out = out.withAllowMissingColumnNames(Boolean.parseBoolean(allowMissingColumnNames));
    }
    return out;
}

From source file:org.apache.logging.log4j.core.layout.AbstractCsvLayout.java

protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape,
        final Character quote, final QuoteMode quoteMode, final String nullString,
        final String recordSeparator) {
    CSVFormat csvFormat = CSVFormat.valueOf(format);
    if (isNotNul(delimiter)) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }//from  w  w  w  .  j a  v a2 s.co  m
    if (isNotNul(escape)) {
        csvFormat = csvFormat.withEscape(escape);
    }
    if (isNotNul(quote)) {
        csvFormat = csvFormat.withQuote(quote);
    }
    if (quoteMode != null) {
        csvFormat = csvFormat.withQuoteMode(quoteMode);
    }
    if (nullString != null) {
        csvFormat = csvFormat.withNullString(nullString);
    }
    if (recordSeparator != null) {
        csvFormat = csvFormat.withRecordSeparator(recordSeparator);
    }
    return csvFormat;
}

From source file:org.apache.nifi.csv.CSVUtils.java

private static CSVFormat buildCustomFormat(final PropertyContext context) {
    final char valueSeparator = getUnescapedChar(context, VALUE_SEPARATOR);
    CSVFormat format = CSVFormat.newFormat(valueSeparator).withAllowMissingColumnNames().withIgnoreEmptyLines();

    final PropertyValue skipHeaderPropertyValue = context.getProperty(FIRST_LINE_IS_HEADER);
    if (skipHeaderPropertyValue.getValue() != null && skipHeaderPropertyValue.asBoolean()) {
        format = format.withFirstRecordAsHeader();
    }/*from  w  w  w.j  a va2  s.  c om*/

    format = format.withQuote(getChar(context, QUOTE_CHAR));
    format = format.withEscape(getChar(context, ESCAPE_CHAR));
    format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());

    if (context.getProperty(COMMENT_MARKER).isSet()) {
        format = format.withCommentMarker(getChar(context, COMMENT_MARKER));
    }
    if (context.getProperty(NULL_STRING).isSet()) {
        format = format.withNullString(CSVUtils.unescape(context.getProperty(NULL_STRING).getValue()));
    }

    final PropertyValue quoteValue = context.getProperty(QUOTE_MODE);
    if (quoteValue != null) {
        final QuoteMode quoteMode = QuoteMode.valueOf(quoteValue.getValue());
        format = format.withQuoteMode(quoteMode);
    }

    final PropertyValue trailingDelimiterValue = context.getProperty(TRAILING_DELIMITER);
    if (trailingDelimiterValue != null) {
        final boolean trailingDelimiter = trailingDelimiterValue.asBoolean();
        format = format.withTrailingDelimiter(trailingDelimiter);
    }

    final PropertyValue recordSeparator = context.getProperty(RECORD_SEPARATOR);
    if (recordSeparator != null) {
        final String separator = unescape(recordSeparator.getValue());
        format = format.withRecordSeparator(separator);
    }

    return format;
}

From source file:org.structr.csv.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) {

        try {/*from   w  w  w .  j  a  v  a 2  s.c o m*/

            final List<Map<String, String>> objects = new LinkedList<>();
            final String source = sources[0].toString();
            String delimiter = ";";
            String quoteChar = "\"";
            String recordSeparator = "\n";

            switch (sources.length) {

            case 4:
                recordSeparator = (String) sources[3];
            case 3:
                quoteChar = (String) sources[2];
            case 2:
                delimiter = (String) sources[1];
                break;
            }

            CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
            format = format.withQuote(quoteChar.charAt(0));
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withSkipHeaderRecord(true);
            format = format.withQuoteMode(QuoteMode.ALL);

            CSVParser parser = new CSVParser(new StringReader(source), format);
            for (final CSVRecord record : parser.getRecords()) {

                objects.add(record.toMap());
            }

            return objects;

        } catch (Throwable t) {

            logException(t, "{0}: Exception for parameter: {1}",
                    new Object[] { getName(), getParametersAsString(sources) });

        }

        return "";

    } else {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

    }

    return usage(ctx.isJavaScriptContext());
}