Example usage for org.apache.commons.csv CSVRecord toString

List of usage examples for org.apache.commons.csv CSVRecord toString

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVRecord toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the contents of this record.

Usage

From source file:nl.paston.bonita.importfile.Main.java

protected static Map<String, Serializable> parseRecord(CSVRecord record, CSVRecord fullHeader) {
    if (record == null) {
        log.warn("Record is null.");
        return null;
    }//from  www .j  a v  a 2 s .co  m
    log.info("Parsing record number: " + (record.getRecordNumber() - 1));
    log.debug(" with content: " + record.toString());
    final Map<String, Serializable> map = new HashMap<>();
    for (int i = 0; i < record.size(); i++) {
        String headerFieldType = getHeaderFieldType(fullHeader.get(i));
        Object recordField = getRecordField(headerFieldType, record.get(i));
        if (recordField != null) {
            String headerField = getHeaderField(fullHeader.get(i));
            String[] headerFieldParts = headerField.split("\\.");
            Map<String, Serializable> targetMap = map;
            for (int j = 0; j < headerFieldParts.length - 1; j++) {
                try {
                    targetMap = (Map<String, Serializable>) targetMap.computeIfAbsent(headerFieldParts[j],
                            x -> new HashMap<>());
                } catch (ClassCastException ex) {
                    log.debug("Problem parsing: " + headerFieldParts[j]);
                }
            }
            String currentHeaderField = headerFieldParts[headerFieldParts.length - 1];
            Matcher listMatcher = Pattern.compile("\\[(.*)\\]").matcher(currentHeaderField);
            if (listMatcher.find()) {
                String parametersString = listMatcher.group(1);
                Matcher chfNameMatcher = Pattern.compile("(.*)\\[").matcher(currentHeaderField);
                if (chfNameMatcher.find()) {
                    String chfName = chfNameMatcher.group(1);
                    String[] parameters = parametersString.split("&");
                    List<Serializable> list = (List<Serializable>) targetMap.computeIfAbsent(chfName,
                            x -> new ArrayList<>());
                    if (parametersString.isEmpty()) {
                        list.add((Serializable) recordField);
                    } else {
                        Map<String, Serializable> subMap = new HashMap<>();
                        for (String parameter : parameters) {
                            String[] parameterKeys = parameter.split("=");
                            if (parameterKeys.length == 2) {
                                subMap.put(parameterKeys[0], parameterKeys[1]);
                            } else if (parameterKeys.length == 1) {
                                subMap.put(parameterKeys[0], (Serializable) recordField);
                            } else {
                                log.error("Wrong number of parameters for: " + currentHeaderField);
                                System.exit(1);
                            }
                        }
                        list.add((Serializable) subMap);
                    }
                }
            } else {
                targetMap.put(currentHeaderField, (Serializable) recordField);
            }
        } else {
            log.warn("Skipped value for record item: " + record.get(i));
        }
    }
    return map;
}

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.CSVParse.java

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {// www. j  a  v a2s.com
        Map<String, Cell> m = new HashMap<String, Cell>();

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    final String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        int p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if (v.trim().toLowerCase().startsWith("missing ")) {
                        Object o = null;
                        Cell ri = new Cell("null", o);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    URI uri = null;
                    try {
                        uri = new URI(v);
                    } catch (Exception _e) {
                        uri = null;
                    }
                    if (uri != null) {
                        Cell ri = processURI(v);
                        if (ri != null) {
                            m.put(k, ri);
                            continue;
                        }
                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (NoSuchElementException e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.TSVParse.java

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {//from  ww  w.  ja  v  a 2 s. c  om
        Map<String, Cell> m = new HashMap<String, Cell>();

        final Pattern languagePattern = Pattern.compile("\"(.+)\"@(\\w\\w)$");

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            int p;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if ((v.trim().startsWith("<")) && (v.trim().endsWith(">"))) {
                        p = v.indexOf('<');
                        int p1 = v.indexOf('>');
                        if ((p >= 0) && (p1 > 0))
                            v = v.substring(p + 1, p1);
                        URI uri = null;
                        try {
                            uri = new URI(v);
                        } catch (Exception _e) {
                            uri = null;
                        }
                        if (uri != null) {
                            Cell ri = processURI(v);
                            if (ri != null) {
                                m.put(k, ri);
                                continue;
                            }
                        }

                        Matcher patmatch = languagePattern.matcher(v);
                        if ((patmatch.matches()) && (patmatch.groupCount() > 0)) {
                            String s = patmatch.group(1);
                            String lang = null;
                            if (patmatch.groupCount() > 1)
                                lang = patmatch.group(2);
                            if ((s != null) && (lang != null)) {
                                Cell ri = new Cell(s, lang);
                                ri.setUnparsedValue(v);
                                m.put(k, ri);
                                continue;
                            }
                        }

                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (Exception e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

From source file:org.apache.camel.dataformat.csv.CsvDataFormatTest.java

@Test
public void shouldHandleRecordConverter() {
    CsvRecordConverter<String> converter = new CsvRecordConverter<String>() {
        @Override//from  ww w .  j  a  v  a  2 s. c  om
        public String convertRecord(CSVRecord record) {
            return record.toString();
        }
    };

    CsvDataFormat dataFormat = new CsvDataFormat().setRecordConverter(converter);

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertSame(converter, dataFormat.getRecordConverter());

    // Properly used (it doesn't modify the format)
    assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat());
}

From source file:org.apache.phoenix.pherf.result.impl.CSVFileResultHandler.java

public synchronized List<Result> read() throws IOException {
    CSVParser parser = null;// w ww .ja  v  a  2s.  c  om
    util.ensureBaseResultDirExists();
    try {
        File file = new File(resultFileName);
        parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT);
        List<CSVRecord> records = parser.getRecords();
        List<Result> results = new ArrayList<>();
        String header = null;
        for (CSVRecord record : records) {

            // First record is the CSV Header
            if (record.getRecordNumber() == 1) {
                header = record.toString();
                continue;
            }
            List<ResultValue> resultValues = new ArrayList<>();
            for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) {
                resultValues.add(new ResultValue(val));
            }
            Result result = new Result(resultFileDetails, header, resultValues);
            results.add(result);
        }
        return results;
    } finally {
        parser.close();
    }
}

From source file:org.onebusaway.admin.service.impl.BundleCheckParserServiceImpl.java

private BundleValidationParseResults parseRecord(CSVRecord record, BundleValidationParseResults parseResults) {
    // Verify that second field contains a valid test.
    if (record.size() < 2 || !validTests.contains(record.get(1).toLowerCase())) {
        BundleValidationParseError parseError = new BundleValidationParseError();
        parseError.setLinenum((int) record.getRecordNumber());
        parseError.setErrorMessage(PARSE_ERROR);
        parseError.setOffendingLine(record.toString());
        parseResults.getParseErrors().add(parseError);
        return parseResults;
    }//from  ww  w . jav  a  2  s .  co m

    ParsedBundleValidationCheck parsedCheck = new ParsedBundleValidationCheck();
    parsedCheck.setLinenum((int) record.getRecordNumber());
    parsedCheck.setAgencyId(record.get(0));
    parsedCheck.setSpecificTest(record.get(1));
    if (record.get(2) != null) {
        parsedCheck.setRouteName(record.get(2));
    }
    if (record.get(3) != null) {
        parsedCheck.setRouteId(record.get(3));
    }
    if (record.get(4) != null) {
        parsedCheck.setStopName(record.get(4));
    }
    if (record.get(5) != null) {
        parsedCheck.setStopId(record.get(5));
    }
    if (record.get(6) != null) {
        parsedCheck.setDate(record.get(6));
    }
    if (record.get(7) != null) {
        parsedCheck.setDepartureTime(record.get(7));
    }
    parseResults.getParsedBundleChecks().add(parsedCheck);
    return parseResults;
}

From source file:org.pad.pgsql.loadmovies.LoadFiles.java

/**
 * Load movies from csv file and save them in DB.
 *
 * @throws Exception/*from www .  ja va  2  s .c  o m*/
 */
private static void loadMoviesAndLinks() throws Exception {
    MovieDao movieDao = new MovieDao(DS);
    Map<Integer, Integer[]> moviesLinks = new HashMap<>();
    //Loads all links informations in memory to enrich afterwards movies
    CSVParser parser = new CSVParser(new FileReader("C:\\PRIVE\\SRC\\ml-20m\\links.csv"),
            CSVFormat.EXCEL.withHeader());
    for (CSVRecord link : parser) {
        Integer movieId = Integer.parseInt(link.get("movieId"));
        if (keepId(movieId)) {
            System.out.println("Parsing line " + link.toString());
            Integer[] otherIds = new Integer[2];
            otherIds[0] = Integer.parseInt(link.get("imdbId"));
            if (StringUtils.isNoneEmpty(link.get("tmdbId"))) {
                otherIds[1] = Integer.parseInt(link.get("tmdbId"));
            }
            moviesLinks.put(movieId, otherIds);
        }
    }

    //Read movie file
    final Reader reader = new FileReader("C:\\PRIVE\\SRC\\ml-20m\\movies.csv");
    parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());

    for (CSVRecord record : parser) {
        //build a movie object from record
        Integer movieId = Integer.parseInt(record.get("movieId"));
        if (keepId(movieId)) {
            String title = record.get("title");
            String genres = record.get("genres");
            //Splitting title to extract the date
            String movieDate = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(title, "("), ")");
            String movieName = null;
            if (StringUtils.isNumeric(movieDate)) {
                movieName = StringUtils.substringBeforeLast(title, "(");
            } else {
                movieName = title;
                movieDate = null;
            }

            System.out.println(movieName + " - " + movieDate);
            Movie movieToAdd = new Movie(movieId, movieName, movieDate);

            //Enrich movie with links
            Integer[] additionalIds = moviesLinks.get(movieId);
            if (additionalIds != null) {
                movieToAdd.setImdbId(additionalIds[0]);
                movieToAdd.setTmdbId(additionalIds[1]);
            }

            //Save in database
            movieDao.save(movieToAdd);
        }
    }
}

From source file:org.seasr.meandre.components.transform.text.CSVTextToTokenCounts.java

@Override
public void executeCallBack(ComponentContext cc) throws Exception {
    Hashtable<String, Integer> htCounts = new Hashtable<String, Integer>();

    for (String text : DataTypeParser.parseAsString(cc.getDataComponentFromInput(IN_TEXT))) {
        //           boolean skippedHeader = false;
        //String[][] data = ... .getAllValues();
        //           CSVParser parser = new CSVParser(new StringReader(text), strategy); 
        //           CSVParser parser = new CSVParser(new StringReader(text), format); 
        //           String[] tokens = uninitialisedLine;
        //           while (tokens != null) {
        console.finer("received text:\n" + text + "\n");
        for (CSVRecord tokens : format.parse(new StringReader(text))) {
            //              tokens = parser.getLine();
            //              if (tokens == null) break;
            //               if (bHeader && !skippedHeader) {
            //                   skippedHeader = true;
            //                   continue;
            //               }
            //               String token = tokens[tokenPos];
            console.fine("processing row " + tokens.toString());
            if (tokens.size() <= tokenPos || tokens.size() <= countPos) {
                console.warning(//from   ww  w  .j  a  v a2s.  c o m
                        String.format("csv row %d too short (%d) for count pos %d or token pos %d - discarding",
                                tokens.getRecordNumber(), tokens.size(), countPos, tokenPos));
                continue;
            }
            String token = tokens.get(tokenPos);
            int count = 0;
            try {
                count = Integer.parseInt(tokens.get(countPos));
            } catch (NumberFormatException e) {
                console.warning(String.format("Token '%s' had malformed count '%s' - assigning zero!", token,
                        tokens.get(countPos)));
            }

            if (htCounts.containsKey(token))
                console.warning(String.format(
                        "Token '%s' occurs more than once in the dataset - replacing previous count %d with %d...",
                        token, htCounts.get(token), count));

            htCounts.put(token, count);
        }
    }
    cc.pushDataComponentToOutput(OUT_TOKEN_COUNTS, BasicDataTypesTools.mapToIntegerMap(htCounts, bOrdered));
}