Example usage for org.apache.commons.csv QuoteMode NON_NUMERIC

List of usage examples for org.apache.commons.csv QuoteMode NON_NUMERIC

Introduction

In this page you can find the example usage for org.apache.commons.csv QuoteMode NON_NUMERIC.

Prototype

QuoteMode NON_NUMERIC

To view the source code for org.apache.commons.csv QuoteMode NON_NUMERIC.

Click Source Link

Document

Quotes all non-numeric fields.

Usage

From source file:com.lithium.flow.util.CsvFormats.java

@Nullable
private static QuoteMode getQuoteMode(@Nonnull Config config, @Nonnull String key) {
    switch (config.getString(key, "default")) {
    case "default":
        return null;
    case "all":
        return QuoteMode.ALL;
    case "minimal":
        return QuoteMode.MINIMAL;
    case "non_numeric":
        return QuoteMode.NON_NUMERIC;
    case "none":
        return QuoteMode.NONE;
    default:/*  www  .j  av a2 s  .c  o  m*/
        return null;
    }
}

From source file:io.github.hebra.elasticsearch.beat.meterbeat.service.CSVFileOutputService.java

@Override
public synchronized void send(BeatOutput output) {
    if (isEnable()) {

        try (PrintWriter writer = new PrintWriter(new FileOutputStream(outputPath.toFile(), true))) {
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).withQuote(quote)
                    .withQuoteMode(QuoteMode.NON_NUMERIC).withSkipHeaderRecord(true);

            CSVPrinter csvFilePrinter = new CSVPrinter(writer, csvFileFormat);
            csvFilePrinter.printRecord(output.asIterable());
            csvFilePrinter.close();/*from  w w w.  j  a  v  a  2 s  . c om*/
        } catch (IOException ioEx) {
            log.error(ioEx.getMessage());
        }
    }
}

From source file:com.gs.obevo.db.apps.reveng.CsvStaticDataWriter.java

public static void start(final AquaRevengArgs args, File workDir) {
    final DbEnvironment env = new DbEnvironment();
    env.setPlatform(args.getDbPlatform());
    env.setSystemDbPlatform(args.getDbPlatform());
    if (args.getJdbcUrl() != null) {
        env.setJdbcUrl(args.getJdbcUrl());
    } else {//  w w  w . j a  v a 2  s  .  com
        env.setDbHost(args.getDbHost());
        env.setDbPort(args.getDbPort());
        env.setDbServer(args.getDbServer());
    }
    if (args.getDriverClass() != null) {
        env.setDriverClassName(args.getDriverClass());
    }
    final Schema schema = new Schema(args.getDbSchema());
    env.setSchemas(Sets.immutable.with(schema));

    Credential credential = credentialReader.getCredential(args.getUsername(), args.getPassword(), false, null,
            null, null);

    DbDeployerAppContext ctxt = env.getAppContextBuilder().setCredential(credential).setWorkDir(workDir)
            .buildDbContext();
    final CsvStaticDataWriter mw = new CsvStaticDataWriter(ctxt.getSqlExecutor(), ctxt.getDbMetadataManager());

    final MutableList<String> dataTables;
    if (args.getTables() != null && args.getTables().length > 0) {
        dataTables = Lists.mutable.with(args.getTables());
    } else {
        dataTables = FileUtilsCobra.readLines(new File(args.getInputDir(), STATIC_DATA_TABLES_FILE_NAME));
    }

    for (String table : dataTables) {
        System.out.println("Working on table " + table + " at " + new Date());

        CSVFormat csvFormat = CsvReaderDataSource.getCsvFormat(env.getDataDelimiter(), env.getNullToken())
                .withQuoteMode(QuoteMode.NON_NUMERIC);
        mw.writeTable(env.getPlatform(), new PhysicalSchema(schema.getName()), table.trim(),
                new File(args.getOutputDir(),
                        env.getPlatform().getChangeType(ChangeType.STATICDATA_STR).getDirectoryName()),
                args.getUpdateTimeColumns(), csvFormat);
    }
}

From source file:edu.harvard.mcz.dwcaextractor.DwCaExtractor.java

/**
 * Setup conditions to run.//from   w  ww. j  a  v a2  s. com
 * 
 * @param args command line arguments
 * @return true if setup was successful, false otherwise.
 */
protected boolean setup(String[] args) {
    boolean setupOK = false;
    CmdLineParser parser = new CmdLineParser(this);
    //parser.setUsageWidth(4096);
    try {
        parser.parseArgument(args);

        if (help) {
            parser.printUsage(System.out);
            System.exit(0);
        }

        if (archiveFilePath != null) {
            String filePath = archiveFilePath;
            logger.debug(filePath);
            File file = new File(filePath);
            if (!file.exists()) {
                // Error
                logger.error(filePath + " not found.");
            }
            if (!file.canRead()) {
                // error
                logger.error("Unable to read " + filePath);
            }
            if (file.isDirectory()) {
                // check if it is an unzipped dwc archive.
                dwcArchive = openArchive(file);
            }
            if (file.isFile()) {
                // unzip it
                File outputDirectory = new File(file.getName().replace(".", "_") + "_content");
                if (!outputDirectory.exists()) {
                    outputDirectory.mkdir();
                    try {
                        byte[] buffer = new byte[1024];
                        ZipInputStream inzip = new ZipInputStream(new FileInputStream(file));
                        ZipEntry entry = inzip.getNextEntry();
                        while (entry != null) {
                            String fileName = entry.getName();
                            File expandedFile = new File(outputDirectory.getPath() + File.separator + fileName);
                            new File(expandedFile.getParent()).mkdirs();
                            FileOutputStream expandedfileOutputStream = new FileOutputStream(expandedFile);
                            int len;
                            while ((len = inzip.read(buffer)) > 0) {
                                expandedfileOutputStream.write(buffer, 0, len);
                            }

                            expandedfileOutputStream.close();
                            entry = inzip.getNextEntry();
                        }
                        inzip.closeEntry();
                        inzip.close();
                        logger.debug("Unzipped archive into " + outputDirectory.getPath());
                    } catch (FileNotFoundException e) {
                        logger.error(e.getMessage());
                    } catch (IOException e) {
                        logger.error(e.getMessage(), e);
                    }
                }
                // look into the unzipped directory
                dwcArchive = openArchive(outputDirectory);
            }
            if (dwcArchive != null) {
                if (checkArchive()) {
                    // Check output 
                    csvPrinter = new CSVPrinter(new FileWriter(outputFilename, append),
                            CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC));
                    // no exception thrown
                    setupOK = true;
                }
            } else {
                System.out.println("Problem opening archive.");
                logger.error("Unable to unpack archive file.");
            }
            logger.debug(setupOK);
        }

    } catch (CmdLineException e) {
        logger.error(e.getMessage());
        parser.printUsage(System.err);
    } catch (IOException e) {
        logger.error(e.getMessage());
        System.out.println(e.getMessage());
        parser.printUsage(System.err);
    }
    return setupOK;
}

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

UploadStatistics processLoincFiles(List<byte[]> theZipBytes, RequestDetails theRequestDetails) {
    final TermCodeSystemVersion codeSystemVersion = new TermCodeSystemVersion();
    final Map<String, TermConcept> code2concept = new HashMap<String, TermConcept>();

    IRecordHandler handler = new LoincHandler(codeSystemVersion, code2concept);
    iterateOverZipFile(theZipBytes, LOINC_FILE, handler, ',', QuoteMode.NON_NUMERIC);

    handler = new LoincHierarchyHandler(codeSystemVersion, code2concept);
    iterateOverZipFile(theZipBytes, LOINC_HIERARCHY_FILE, handler, ',', QuoteMode.NON_NUMERIC);

    theZipBytes.clear();//from  w  ww .ja  v  a 2  s  .  c  o  m

    for (Iterator<Entry<String, TermConcept>> iter = code2concept.entrySet().iterator(); iter.hasNext();) {
        Entry<String, TermConcept> next = iter.next();
        // if (isBlank(next.getKey())) {
        // ourLog.info("Removing concept with blankc code[{}] and display [{}", next.getValue().getCode(), next.getValue().getDisplay());
        // iter.remove();
        // continue;
        // }
        TermConcept nextConcept = next.getValue();
        if (nextConcept.getParents().isEmpty()) {
            codeSystemVersion.getConcepts().add(nextConcept);
        }
    }

    ourLog.info("Have {} total concepts, {} root concepts", code2concept.size(),
            codeSystemVersion.getConcepts().size());

    String url = LOINC_URL;
    storeCodeSystem(theRequestDetails, codeSystemVersion, url);

    return new UploadStatistics(code2concept.size());
}

From source file:org.asoem.greyfish.utils.space.cluster.DBSCANTest.java

@Before
public void setUp() throws Exception {
    csvRecords = CSVParser.parse(Resources.getResource(DBSCANTest.class, "iris.csv"), Charset.defaultCharset(),
            CSVFormat.newFormat(',').withHeader().withQuote('"').withQuoteMode(QuoteMode.NON_NUMERIC));
}

From source file:umich.ms.batmass.filesupport.files.types.mzrt.model.MzrtFile.java

public void load() throws DataLoadingException {

    int[] counts = new int[3]; // [0] - \r\n, [1] - \n, [2] - \r
    final String[] separators = { "\r\n", "\n", "\r" };
    // detecting line separator
    try (InputStreamReader isr = new InputStreamReader(
            new BufferedInputStream(new FileInputStream(file.toFile())), Charsets.UTF_8)) {
        int c;/*w  w w  .j  ava 2 s  .c om*/
        int encountered = 0;
        boolean isPrevR = false;
        int cutoff = 50;
        readLoop: while ((c = isr.read()) != -1) {
            char ch = (char) c;
            switch (ch) {
            case '\r':
                if (++counts[2] > cutoff) {
                    break readLoop;
                }
                isPrevR = true;
                break;
            case '\n':
                if (isPrevR) {
                    counts[2]--;
                    if (++counts[0] > cutoff) {
                        break readLoop;
                    }
                } else {
                    if (++counts[1] > cutoff) {
                        break readLoop;
                    }
                }
                isPrevR = false;
                break;
            default:
                isPrevR = false;
            }
        }
    } catch (IOException ex) {
        throw new DataLoadingException("Could not detect line separator", ex);
    }

    List<Integer> idxMax = new ArrayList<>();
    for (int i = 0; i < counts.length; i++) {
        if (idxMax.isEmpty()) {
            idxMax.add(i);
        } else if (counts[i] > counts[idxMax.get(0)]) {
            idxMax.clear();
            idxMax.add(i);
        } else if (counts[i] == counts[idxMax.get(0)]) {
            idxMax.add(i);
        }
    }

    String recordSeparator;
    if (idxMax.size() > 1) {
        if (idxMax.contains(0)) {
            recordSeparator = separators[0];
        } else if (idxMax.contains(1)) {
            recordSeparator = separators[1];
        } else {
            recordSeparator = separators[idxMax.get(0)];
        }
    } else {
        recordSeparator = separators[idxMax.get(0)];
    }

    // detecting delimiter
    char delimiter;
    try (BufferedReader br = new BufferedReader(new FileReader(file.toFile()))) {
        List<String> lines = new ArrayList<>();
        String line;
        int numTestLines = 10;
        while ((line = br.readLine()) != null) {
            if (!line.isEmpty()) {
                lines.add(line);
                if (lines.size() >= numTestLines)
                    break;
            }
        }

        delimiter = guessDelimiter(lines);
    } catch (IOException ex) {
        throw new DataLoadingException("Could not detect delimiter character", ex);
    }

    try (BufferedReader br = new BufferedReader(new FileReader(file.toFile()))) {
        CSVFormat fmt = CSVFormat.newFormat(delimiter);
        fmt = fmt.withHeader().withIgnoreEmptyLines(true).withTrim(true).withIgnoreHeaderCase(true)
                .withQuoteMode(QuoteMode.NON_NUMERIC).withRecordSeparator(recordSeparator).withQuote('"');

        CSVParser parser = fmt.parse(br);

        records = parser.getRecords();
        header = parser.getHeaderMap();

        String[] colNames = { HEAD_MZLO, HEAD_MZHI, HEAD_RTLO, HEAD_RTHI };
        for (int i = 0; i < colNames.length; i++) {
            Integer index = header.get(colNames[i]);
            if (index == null)
                throw new DataLoadingException(String.format("Missing header column [%s]", colNames[i]));
            indexesMzRtColorOpacity[i] = index;
        }
        Integer indexColor = header.get(HEAD_COLOR);
        if (indexColor != null && indexColor >= 0)
            indexesMzRtColorOpacity[4] = indexColor;
        Integer indexOpacity = header.get(HEAD_OPACITY);
        if (indexOpacity != null && indexOpacity >= 0)
            indexesMzRtColorOpacity[5] = indexOpacity;

    } catch (IOException ex) {
        throw new DataLoadingException(ex);
    }
}

From source file:umich.ms.batmass.filesupport.files.types.xcms.peaks.model.XCMSPeaks.java

/**
 * Parse XCMS peaks from the file, which you can create from R after running
 * XCMS feature finding. <br/>/*from w ww  . j  ava 2s.c o m*/
 * Example:<br/>
 * {@code xs <- xcmsSet(files = file_mzml, method = "massifquant", prefilter = c(1, 10000), peakwidth = c(5, 500), ppm = 20, criticalValue = 1.0, consecMissedLimit = 0, withWave = 0, nSlaves=4)} <br/>
 * {@code peaks <- ixs@peaks[1:7108,1:9]} <br/>
 * {@code write.table(peaks, sep = "\t",file = "D:/projects/XCMS/peaks.xcms.csv")}
 * @param path
 * @return
 */
public static XCMSPeaks create(Path path) throws IOException {
    if (!Files.exists(path))
        throw new IllegalArgumentException("File path for XCMS peaks does not exist.");

    XCMSPeaks peaks = new XCMSPeaks();
    BufferedReader reader = new BufferedReader(new FileReader(path.toFile()));
    String[] header = {};
    CSVFormat format = CSVFormat.newFormat(',').withHeader().withIgnoreSurroundingSpaces()
            .withAllowMissingColumnNames().withQuoteMode(QuoteMode.NON_NUMERIC).withQuote('"');
    CSVParser parser = new CSVParser(reader, format);
    String val;
    for (final CSVRecord r : parser) {
        XCMSPeak p = new XCMSPeak();
        val = r.get(0);
        p.setRowNum(Integer.parseInt(val));
        val = r.get("mz");
        p.setMz(Double.parseDouble(val));
        val = r.get("mzmin");
        p.setMzMin(Double.parseDouble(val));
        val = r.get("mzmax");
        p.setMzMax(Double.parseDouble(val));
        val = r.get("rt");
        p.setRt(Double.parseDouble(val));
        val = r.get("rtmin");
        p.setRtMin(Double.parseDouble(val));
        val = r.get("rtmax");
        p.setRtMax(Double.parseDouble(val));
        val = r.get("into");
        p.setInto(Double.parseDouble(val));
        val = r.get("maxo");
        p.setMaxo(Double.parseDouble(val));
        val = r.get("sample");
        p.setSample(val);

        // these are optional and are only added by further R package 
        // called 'CAMERA' processing
        try {
            val = getRecordValueForColName(r, "isotopes");
            p.setIsotopes(val);
        } catch (IllegalArgumentException e) {
        }
        try {
            val = r.get("adduct");
            p.setAdduct(val);
        } catch (IllegalArgumentException e) {
            p.setAdduct("");
        }
        try {
            val = r.get("pcgroup");
            p.setPcgroup(Integer.parseInt(val));
        } catch (IllegalArgumentException e) {
        }

        peaks.add(p);
    }

    return peaks;
}