List of usage examples for org.apache.commons.csv CSVParser parse
public static CSVParser parse(final String string, final CSVFormat format) throws IOException
From source file:org.qcert.util.DataLoader.java
/** Process an individual table, producing its rows in JSON form * @param data the CSV file contents as a String * @param def the type definition as an ObjectType * @param format the CSVFormat to use/*from w w w . java 2s. c o m*/ * @return a JsonArray of the translation of the rows * @throws Exception */ private static JsonArray process(String data, ObjectType def, CSVFormat format) throws Exception { JsonArray ans = new JsonArray(); List<CSVRecord> records = CSVParser.parse(data, format).getRecords(); for (CSVRecord record : records) { Map<String, String> recmap = record.toMap(); JsonObject datum = new JsonObject(); for (Entry<String, String> col : recmap.entrySet()) { datum.add(col.getKey(), processColumn(col.getKey(), col.getValue(), def)); } ans.add(datum); } return ans; }
From source file:org.sonar.db.version.v51.FeedFileSourcesBinaryData.java
private byte[] toBinary(Long fileSourceId, @Nullable String data) { DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder(); CSVParser parser = null;/*from www . ja v a 2 s.com*/ try { if (data != null) { parser = CSVParser.parse(data, CSVFormat.DEFAULT); Iterator<CSVRecord> rows = parser.iterator(); int line = 1; while (rows.hasNext()) { CSVRecord row = rows.next(); if (row.size() == 16) { DbFileSources.Line.Builder lineBuilder = dataBuilder.addLinesBuilder(); lineBuilder.setLine(line); String s = row.get(0); if (StringUtils.isNotEmpty(s)) { lineBuilder.setScmRevision(s); } s = row.get(1); if (StringUtils.isNotEmpty(s)) { lineBuilder.setScmAuthor(s); } Date scmDate = DateUtils.parseDateTimeQuietly(row.get(2)); if (scmDate != null) { lineBuilder.setScmDate(scmDate.getTime()); } s = row.get(3); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtLineHits(Integer.parseInt(s)); } s = row.get(4); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtConditions(Integer.parseInt(s)); } s = row.get(5); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtCoveredConditions(Integer.parseInt(s)); } s = row.get(6); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItLineHits(Integer.parseInt(s)); } s = row.get(7); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItConditions(Integer.parseInt(s)); } s = row.get(8); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItCoveredConditions(Integer.parseInt(s)); } s = row.get(9); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallLineHits(Integer.parseInt(s)); } s = row.get(10); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallConditions(Integer.parseInt(s)); } s = row.get(11); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallCoveredConditions(Integer.parseInt(s)); } s = row.get(12); if (StringUtils.isNotEmpty(s)) { lineBuilder.setHighlighting(s); } s = row.get(13); if (StringUtils.isNotEmpty(s)) { lineBuilder.setSymbols(s); } s = row.get(14); if (StringUtils.isNotEmpty(s)) { lineBuilder.addAllDuplication(splitIntegers(s)); } s = row.get(15); if (s != null) { lineBuilder.setSource(s); } } line++; } } return FileSourceDto.encodeSourceData(dataBuilder.build()); } catch (Exception e) { throw new IllegalStateException( "Invalid FILE_SOURCES.DATA on row with ID " + fileSourceId + ": " + data, e); } finally { IOUtils.closeQuietly(parser); } }
From source file:org.sonar.server.db.migrations.v51.FeedFileSourcesBinaryData.java
private byte[] toBinary(Long fileSourceId, @Nullable String data) { FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder(); CSVParser parser = null;/*from w ww . ja v a 2 s . c om*/ try { if (data != null) { parser = CSVParser.parse(data, CSVFormat.DEFAULT); Iterator<CSVRecord> rows = parser.iterator(); int line = 1; while (rows.hasNext()) { CSVRecord row = rows.next(); if (row.size() == 16) { FileSourceDb.Line.Builder lineBuilder = dataBuilder.addLinesBuilder(); lineBuilder.setLine(line); String s = row.get(0); if (StringUtils.isNotEmpty(s)) { lineBuilder.setScmRevision(s); } s = row.get(1); if (StringUtils.isNotEmpty(s)) { lineBuilder.setScmAuthor(s); } s = row.get(2); if (StringUtils.isNotEmpty(s)) { lineBuilder.setScmDate(DateUtils.parseDateTimeQuietly(s).getTime()); } s = row.get(3); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtLineHits(Integer.parseInt(s)); } s = row.get(4); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtConditions(Integer.parseInt(s)); } s = row.get(5); if (StringUtils.isNotEmpty(s)) { lineBuilder.setUtCoveredConditions(Integer.parseInt(s)); } s = row.get(6); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItLineHits(Integer.parseInt(s)); } s = row.get(7); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItConditions(Integer.parseInt(s)); } s = row.get(8); if (StringUtils.isNotEmpty(s)) { lineBuilder.setItCoveredConditions(Integer.parseInt(s)); } s = row.get(9); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallLineHits(Integer.parseInt(s)); } s = row.get(10); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallConditions(Integer.parseInt(s)); } s = row.get(11); if (StringUtils.isNotEmpty(s)) { lineBuilder.setOverallCoveredConditions(Integer.parseInt(s)); } s = row.get(12); if (StringUtils.isNotEmpty(s)) { lineBuilder.setHighlighting(s); } s = row.get(13); if (StringUtils.isNotEmpty(s)) { lineBuilder.setSymbols(s); } s = row.get(14); if (StringUtils.isNotEmpty(s)) { lineBuilder.addAllDuplication(splitIntegers(s)); } s = row.get(15); if (s != null) { lineBuilder.setSource(s); } } line++; } } return FileSourceDto.encodeSourceData(dataBuilder.build()); } catch (Exception e) { throw new IllegalStateException( "Invalid FILE_SOURCES.DATA on row with ID " + fileSourceId + ": " + data, e); } finally { IOUtils.closeQuietly(parser); } }
From source file:org.wso2.carbon.event.simulator.csvFeedSimulation.core.CSVFeedEventSimulator.java
/** * This method must be called within a synchronized block to avoid multiple file simulators from running simultaneously. * Read the values from uploaded CSV file and convert those values into event and send those events to * input handler//ww w . j a va 2s . c o m * <p> * <p> * To read the CSV file It uses CSV parser Library. * {@link <a href="https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVParser.html">CSVParser</a>} * </p> * <p> * <p> * CSV file can be separated by one of these fallowing character , , ; , \t by default * It has capability to have user defined delimiter * Any field may be quoted (with double quotes) * Fields with embedded commas or delimiter characters must be double quoted. * </p> * <p> * Initialize CSVParser * * @param executionPlanDto ExecutionPlanDto * @param csvFileConfig CSVFileSimulationDto */ private void sendEvent(ExecutionPlanDto executionPlanDto, CSVFileSimulationDto csvFileConfig) { /* return no of events read from CSV file during ever iteration */ long noOfEvents = 0; int delay = csvFileConfig.getDelay(); /* Reader for reading character streams from file */ Reader in = null; /* CSVParser to read CSV Values */ CSVParser csvParser = null; if (delay <= 0) { log.warn("Events will be sent continuously since the delay between events are set to " + delay + "milliseconds"); delay = 0; } try { /* Initialize Reader */ in = new FileReader(String.valueOf(Paths.get(System.getProperty("java.io.tmpdir"), csvFileConfig.getFileDto().getFileInfo().getFileName()))); /* Initialize CSVParser with appropriate CSVFormat according to delimiter */ switch (csvFileConfig.getDelimiter()) { case ",": csvParser = CSVParser.parse(in, CSVFormat.DEFAULT); break; case ";": csvParser = CSVParser.parse(in, CSVFormat.EXCEL); break; case "\\t": csvParser = CSVParser.parse(in, CSVFormat.TDF); break; default: csvParser = CSVParser.parse(in, CSVFormat.newFormat(csvFileConfig.getDelimiter().charAt(0))); } int attributeSize = executionPlanDto.getInputStreamDtoMap().get(csvFileConfig.getStreamName()) .getStreamAttributeDtos().size(); /* Iterate through the CSV file line by line */ for (CSVRecord record : csvParser) { try { synchronized (this) { if (isStopped) { isStopped = false; break; } if (isPaused) { this.wait(); } } if (record.size() != attributeSize) { log.warn("No of attribute is not equal to attribute size: " + attributeSize + " is needed" + "in Row no:" + noOfEvents + 1); } String[] attributes = new String[attributeSize]; noOfEvents = csvParser.getCurrentLineNumber(); for (int i = 0; i < record.size(); i++) { attributes[i] = record.get(i); } //convert Attribute values into event Event event = EventConverter.eventConverter(csvFileConfig.getStreamName(), attributes, executionPlanDto); // TODO: 13/12/16 delete sout System.out.println("Input Event " + Arrays.deepToString(event.getEventData())); // //send the event to input handler send(csvFileConfig.getStreamName(), event); //delay between two events if (delay > 0) { Thread.sleep(delay); } } catch (EventSimulationException e) { log.error("Event dropped due to Error occurred during generating an event" + e.getMessage()); } catch (InterruptedException e) { log.error("Error occurred during send event" + e.getMessage()); } } } catch (IllegalArgumentException e) { // TODO: 02/12/16 proper error message throw new EventSimulationException("File Parameters are null" + e.getMessage()); } catch (FileNotFoundException e) { throw new EventSimulationException( "File not found :" + csvFileConfig.getFileDto().getFileInfo().getFileName()); } catch (IOException e) { throw new EventSimulationException("Error occurred while reading the file"); } finally { try { if (in != null && csvParser != null) in.close(); csvParser.close(); } catch (IOException e) { throw new EventSimulationException("Error occurred during closing the file"); } } }
From source file:org.wso2.carbon.event.simulator.randomdatafeedsimulation.bean.CustomBasedAttribute.java
/** * Method to split the data list into seperated values and assign it to customDataList * * @param customData String that has data list values * Initial string format is ""CEP,Siddhi",ESB,DAS" *//*from ww w. j a va 2 s.com*/ public void setCustomData(String customData) { CSVParser csvParser = null; List<String> dataList = null; try { csvParser = CSVParser.parse(customData, CSVFormat.newFormat(',').withQuote('/')); dataList = new ArrayList<>(); for (CSVRecord record : csvParser) { for (int i = 0; i < record.size(); i++) { dataList.add(record.get(i)); } } } catch (IOException e) { throw new EventSimulationException("I/O error occurs :" + e.getMessage()); } catch (IllegalArgumentException e) { throw new EventSimulationException("Data set is null :" + e.getMessage()); } customDataList = dataList.toArray(new String[dataList.size()]); }
From source file:trainer.userinput.TrainingFileDB.java
public static UserInputTrainingRecord parseLine(String line) throws IOException { CSVParser lineParser = CSVParser.parse(line, TrainingFileDB.getCSVFormat()); List<CSVRecord> csvRecords = lineParser.getRecords(); UserInputTrainingRecord retVal = null; for (CSVRecord record : csvRecords) { retVal = new UserInputTrainingRecord(record.get(0), record.get(1)); }/*from ww w . j a va2s . c om*/ return retVal; }