List of usage examples for org.apache.commons.csv CSVParser iterator
@Override
public Iterator<CSVRecord> iterator()
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 {/* w ww . j av a2 s .com*/ 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.itemanalysis.jmetrik.file.JmetrikFileImporter.java
/** * 1. Gets the file header or creates one. * 2. Sets the number of columns/*from w ww. j av a 2 s .c om*/ * 3. Checks the type of data stored in each variable using the first rowsToScan rows. * Variables are integers by default. This method will change the data type to either * double or string. * */ private void setDataTypes() { CSVParser parser = null; Reader reader = null; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); //Get column names from variable attributes colNames = new String[variableAttributeMap.size()]; int index = 0; Iterator<VariableName> iter = variableAttributeMap.keySet().iterator(); VariableName tempName = null; while (iter.hasNext()) { colNames[index++] = iter.next().toString(); } //Create a parser with variable names from the variable attributes if (hasHeader) { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true).withCommentMarker('#')); } else { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withCommentMarker('#')); } //Check data types in each column. String value = ""; Iterator<CSVRecord> csvIter = parser.iterator(); CSVRecord csvRecord = null; double testValue = 0; nrow = 0; while (csvIter.hasNext()) { csvRecord = csvIter.next(); iter = variableAttributeMap.keySet().iterator(); while (iter.hasNext()) { tempName = iter.next(); value = csvRecord.get(tempName.toString()).trim(); //Check that string can be converted to double. If not, Change variable type. //Ignore missing data and other special codes try { if (!"".equals(value) && !specialDataCodes.isMissing(value)) { testValue = Double.parseDouble(value); if (testValue != Math.floor(testValue)) { //if any value is a double, the variable is a double variableAttributeMap.get(tempName).setDataType(DataType.DOUBLE); } } } catch (NumberFormatException ex) { //if any value is a String, the variable is a String variableAttributeMap.get(tempName).setDataType(DataType.STRING); } } nrow++; } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); } catch (IOException ex) { theException = ex; logger.fatal(ex); } } }
From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java
private void convertFile() { CSVParser parser = null; Reader reader = null;//from ww w. j a v a 2s.c om CSVPrinter printer = null; Writer writer = null; try { if (outputFile.exists()) { if (!overwrite) { theException = new IOException("File already exists and overwrite==false"); return; } } else { outputFile.createNewFile(); } //For debugging // System.out.println("CREATED: " + outputFile.getAbsolutePath()); //Writer header to file writer = new OutputStreamWriter(new FileOutputStream(outputFile)); printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#')); printer.printComment("VERSION"); printer.printRecord(new String[] { "jmetrik1" }); printer.printComment("METADATA"); printer.printRecord(new String[] { Integer.valueOf(nrow).toString() }); printer.printComment("ATTRIBUTES"); for (VariableName v : variableAttributeMap.keySet()) { printer.printRecord(variableAttributeMap.get(v).getAttributeArray()); } printer.printComment("DATA"); //Write data to file reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, dataFileFormat); if (hasHeader) { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true)); } else { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames)); } Iterator<CSVRecord> iter = parser.iterator(); CSVRecord csvRecord = null; VariableAttributes variableAttributes = null; DataType dataType = null; String temp = ""; while (iter.hasNext()) { csvRecord = iter.next(); for (VariableName v : variableAttributeMap.keySet()) { temp = csvRecord.get(v.toString()); variableAttributes = variableAttributeMap.get(v); dataType = variableAttributes.getDataType(); if (!variableAttributes.isMissing(temp)) { if (DataType.INTEGER == dataType) { printer.print(Double.valueOf(Double.parseDouble(temp)).intValue()); } else if (DataType.DOUBLE == dataType) { printer.print(Double.parseDouble(temp)); } else { printer.print(temp); } } else { printer.print(temp); } } printer.println(); } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); if (printer != null) printer.close(); if (writer != null) writer.close(); } catch (IOException ex) { theException = ex; logger.fatal(ex); } } }
From source file:edu.harvard.mcz.imagecapture.loader.JobVerbatimFieldLoad.java
@Override public void start() { startDateTime = new Date(); Singleton.getSingletonInstance().getJobList().addJob((RunnableJob) this); runStatus = RunStatus.STATUS_RUNNING; String selectedFilename = ""; if (file == null) { final JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); if (Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_LASTLOADPATH) != null) { fileChooser.setCurrentDirectory(new File(Singleton.getSingletonInstance().getProperties() .getProperties().getProperty(ImageCaptureProperties.KEY_LASTLOADPATH))); }/*from www . j a v a 2s . c o m*/ int returnValue = fileChooser.showOpenDialog(Singleton.getSingletonInstance().getMainFrame()); if (returnValue == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } } if (file != null) { log.debug("Selected file to load: " + file.getName() + "."); if (file.exists() && file.isFile() && file.canRead()) { // Save location Singleton.getSingletonInstance().getProperties().getProperties() .setProperty(ImageCaptureProperties.KEY_LASTLOADPATH, file.getPath()); selectedFilename = file.getName(); String[] headers = new String[] {}; CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers); int rows = 0; try { rows = readRows(file, csvFormat); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found", JOptionPane.OK_OPTION); errors.append("File not found ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } catch (IOException e) { errors.append("Error loading csv format, trying tab delimited: ").append(e.getMessage()) .append("\n"); log.debug(e.getMessage()); try { // try reading as tab delimited format, if successful, use that format. CSVFormat tabFormat = CSVFormat.newFormat('\t').withIgnoreSurroundingSpaces(true) .withHeader(headers).withQuote('"'); rows = readRows(file, tabFormat); csvFormat = tabFormat; } catch (IOException e1) { errors.append("Error Loading data: ").append(e1.getMessage()).append("\n"); log.error(e.getMessage(), e1); } } try { Reader reader = new FileReader(file); CSVParser csvParser = new CSVParser(reader, csvFormat); Map<String, Integer> csvHeader = csvParser.getHeaderMap(); headers = new String[csvHeader.size()]; int i = 0; for (String header : csvHeader.keySet()) { headers[i++] = header; log.debug(header); } boolean okToRun = true; //TODO: Work picking/checking responsibility into a FieldLoaderWizard List<String> headerList = Arrays.asList(headers); if (!headerList.contains("barcode")) { log.error("Input file " + file.getName() + " header does not contain required field 'barcode'."); // no barcode field, we can't match the input to specimen records. errors.append("Field \"barcode\" not found in csv file headers. Unable to load data.") .append("\n"); okToRun = false; } if (okToRun) { Iterator<CSVRecord> iterator = csvParser.iterator(); FieldLoader fl = new FieldLoader(); if (headerList.size() == 3 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode")) { log.debug("Input file matches case 1: Unclassified text only."); // Allowed case 1a: unclassified text only int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatimUnclassifiedText", "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimUnclassifiedText, questions, true); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else if (headerList.size() == 4 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode") && headerList.contains("verbatimClusterIdentifier")) { log.debug( "Input file matches case 1: Unclassified text only (with cluster identifier)."); // Allowed case 1b: unclassified text only (including cluster identifier) int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatimUnclassifiedText", "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText"); String verbatimClusterIdentifier = record.get("verbatimClusterIdentifier"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimUnclassifiedText, verbatimClusterIdentifier, questions, true); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else if (headerList.size() == 8 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode") && headerList.contains("verbatimLocality") && headerList.contains("verbatimDate") && headerList.contains("verbatimNumbers") && headerList.contains("verbatimCollector") && headerList.contains("verbatimCollection")) { // Allowed case two, transcription into verbatim fields, must be exact list of all // verbatim fields, not including cluster identifier or other metadata. log.debug("Input file matches case 2: Full list of verbatim fields."); int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatim fields.", "Verbatim Fields found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimLocality = record.get("verbatimLocality"); String verbatimDate = record.get("verbatimDate"); String verbatimCollector = record.get("verbatimCollector"); String verbatimCollection = record.get("verbatimCollection"); String verbatimNumbers = record.get("verbatimNumbers"); String verbatimUnclasifiedText = record.get("verbatimUnclassifiedText"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimLocality, verbatimDate, verbatimCollector, verbatimCollection, verbatimNumbers, verbatimUnclasifiedText, questions); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else { // allowed case three, transcription into arbitrary sets verbatim or other fields log.debug("Input file case 3: Arbitrary set of fields."); // Check column headers before starting run. boolean headersOK = false; try { HeaderCheckResult headerCheck = fl.checkHeaderList(headerList); if (headerCheck.isResult()) { int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with headers: \n" + headerCheck.getMessage().replaceAll(":", ":\n"), "Fields found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { headersOK = true; } else { errors.append("Load canceled by user.").append("\n"); } } else { int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Problem found with headers in file, try to load anyway?\nHeaders: \n" + headerCheck.getMessage().replaceAll(":", ":\n"), "Problem in fields for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { headersOK = true; } else { errors.append("Load canceled by user.").append("\n"); } } } catch (LoadException e) { errors.append("Error loading data: \n").append(e.getMessage()).append("\n"); JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), e.getMessage().replaceAll(":", ":\n"), "Error Loading Data: Problem Fields", JOptionPane.ERROR_MESSAGE); log.error(e.getMessage(), e); } if (headersOK) { int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; Map<String, String> data = new HashMap<String, String>(); CSVRecord record = iterator.next(); String barcode = record.get("barcode"); Iterator<String> hi = headerList.iterator(); boolean containsNonVerbatim = false; while (hi.hasNext()) { String header = hi.next(); // Skip any fields prefixed by the underscore character _ if (!header.equals("barcode") && !header.startsWith("_")) { data.put(header, record.get(header)); if (!header.equals("questions") && MetadataRetriever.isFieldExternallyUpdatable(Specimen.class, header) && MetadataRetriever.isFieldVerbatim(Specimen.class, header)) { containsNonVerbatim = true; } } } if (data.size() > 0) { try { boolean updated = false; if (containsNonVerbatim) { updated = fl.loadFromMap(barcode, data, WorkFlowStatus.STAGE_CLASSIFIED, true); } else { updated = fl.loadFromMap(barcode, data, WorkFlowStatus.STAGE_VERBATIM, true); } counter.incrementSpecimens(); if (updated) { counter.incrementSpecimensUpdated(); } } catch (HibernateException e1) { // Catch (should just be development) problems with the underlying query StringBuilder message = new StringBuilder(); message.append("Query Error loading row (").append(lineNumber) .append(")[").append(barcode).append("]") .append(e1.getMessage()); RunnableJobError err = new RunnableJobError(selectedFilename, barcode, Integer.toString(lineNumber), e1.getMessage(), e1, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(err); log.error(e1.getMessage(), e1); } catch (LoadException e) { StringBuilder message = new StringBuilder(); message.append("Error loading row (").append(lineNumber).append(")[") .append(barcode).append("]").append(e.getMessage()); RunnableJobError err = new RunnableJobError(selectedFilename, barcode, Integer.toString(lineNumber), e.getMessage(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(err); // errors.append(message.append("\n").toString()); log.error(e.getMessage(), e); } } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { String message = "Can't load data, problem with headers."; errors.append(message).append("\n"); log.error(message); } } } csvParser.close(); reader.close(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found", JOptionPane.OK_OPTION); errors.append("File not found ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } catch (IOException e) { errors.append("Error Loading data: ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } } } else { //TODO: handle error condition log.error("File selection cancelled by user."); } report(selectedFilename); done(); }
From source file:org.apache.nifi.csv.CSVRecordSource.java
public CSVRecordSource(final InputStream in, final PropertyContext context) throws IOException { final String charset = context.getProperty(CSVUtils.CHARSET).getValue(); final Reader reader; try {//from w w w . ja v a 2 s . c om reader = new InputStreamReader(new BOMInputStream(in), charset); } catch (UnsupportedEncodingException e) { throw new ProcessException(e); } final CSVFormat csvFormat = CSVUtils.createCSVFormat(context).withFirstRecordAsHeader().withTrim(); final CSVParser csvParser = new CSVParser(reader, csvFormat); fieldNames = Collections.unmodifiableList(new ArrayList<>(csvParser.getHeaderMap().keySet())); csvRecordIterator = csvParser.iterator(); }
From source file:org.easybatch.extensions.apache.common.csv.ApacheCommonCsvRecordMapperTest.java
private ApacheCommonCsvRecord getApacheCommonCsvRecord(StringReader stringReader, CSVFormat csvFormat) throws IOException { CSVParser parser = new CSVParser(stringReader, csvFormat); CSVRecord csvRecord = parser.iterator().next(); return new ApacheCommonCsvRecord(header, csvRecord); }
From source file:org.onehippo.forge.content.pojo.model.CsvConvertToContentNodesTest.java
@Test public void testReadCsvAndConvertToContentNodes() throws Exception { InputStream input = null;/*from w w w . ja v a 2 s . c o m*/ InputStreamReader reader = null; try { // 1. Open a reader from a CSV file. input = NEWS_CSV_URL.openStream(); reader = new InputStreamReader(input, "UTF-8"); // 2. Create CSV parser to parse the CSV data with column headers. CSVParser parser = CSVFormat.DEFAULT.withHeader("Title", "Introduction", "Date", "Content") .withSkipHeaderRecord().parse(reader); CSVRecord record; // 3. StringCodec to generate a JCR node name from the title column, // and ObjectMapper instance to log a ContentNode to JSON. final StringCodec codec = new StringCodecFactory.UriEncoding(); final ObjectMapper objectMapper = new ObjectMapper(); String name; String title; String introduction; String date; String content; String translationId; String translationLocale = "en"; String targetDocumentLocation; // 4. Iterate each data record and create a ContentNode for a news article with setting properties and child nodes. for (Iterator<CSVRecord> it = parser.iterator(); it.hasNext();) { record = it.next(); // 4.1. Read each column from a CSV record. title = record.get("Title"); name = codec.encode(title); introduction = record.get("Introduction"); date = record.get("Date"); content = record.get("Content"); // 4.2. Create a ContentNode for a news article and set primitive property values. ContentNode newsNode = new ContentNode(name, "ns1:newsdocument"); newsNode.setProperty("ns1:title", title); newsNode.setProperty("ns1:introduction", introduction); newsNode.setProperty("ns1:date", ContentPropertyType.DATE, date); // 4.3. Create/add a child hippostd:html content node and set the content. ContentNode htmlNode = new ContentNode("ns1:content", HippoStdNodeType.NT_HTML); newsNode.addNode(htmlNode); htmlNode.setProperty(HippoStdNodeType.HIPPOSTD_CONTENT, content); // 4.4. In Hippo CMS, the internal translation UUID and locale string are important in most cases. // So, let's generate a translation UUID and use 'en' for simplicity for now. translationId = UUID.randomUUID().toString(); newsNode.setProperty(HippoTranslationNodeType.ID, translationId); newsNode.setProperty(HippoTranslationNodeType.LOCALE, translationLocale); // 4.5. (Optional) Set kind of meta property for localized document name which is displayed in folder view later. // This meta property is not used by Hippo CMS, but can be read/used by a higher level content importing application // to set a localized (translated) name of the document (e.g, using Hippo TranslationWorkflow). newsNode.setProperty("jcr:localizedName", title); // 4.6. (Optional) Determine the target document location where this content should be generated and // store it in a meta property, jcr:path. // This meta property cannot be used in JCR repository in importing process, but can be read/used by a higher level // content importing application to create a document using Hippo DocumentWorkflow for instance. targetDocumentLocation = "/content/documents/ns1/news/" + name; newsNode.setProperty("jcr:path", targetDocumentLocation); // 4.7. (Optional) Log the JSON-ized string of the news ContentNode instance. StringWriter stringWriter = new StringWriter(256); objectMapper.writerWithDefaultPrettyPrinter().writeValue(stringWriter, newsNode); log.debug("newsNode: \n{}\n", stringWriter.toString()); } } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(input); } }
From source file:org.softinica.maven.jmeter.report.parser.CSVReportParser.java
@Override public Input parseInput(InputDefinition definition) { CSVParser parser = null; Input input = new Input(); try {/*from ww w .j a v a 2 s. c o m*/ Reader reader = new InputStreamReader(new FileInputStream(definition.getInputFile())); parser = new CSVParser(reader, CSVFormat.DEFAULT); Iterator<CSVRecord> it = parser.iterator(); while (it.hasNext()) { Sample sample = new Sample(); CSVRecord record = it.next(); sample.setTimestamp(Long.valueOf(record.get(0))); sample.setLabel(record.get(2)); sample.setValue(Double.valueOf(record.get(4)) * definition.getScale()); sample.setSuccess(Boolean.parseBoolean(record.get(7))); input.getSamples().add(sample); } } catch (IOException e) { throw new RuntimeException(e); } finally { Utils.close(parser); } return input; }
From source file:org.softinica.maven.jmeter.report.parser.SimpleCSVParser.java
@Override public Input parseInput(InputDefinition definition) { CSVParser parser = null; List<String> headers = new LinkedList<String>(); Input input = new Input(); try {//from w w w. j av a 2s . c o m Reader reader = new InputStreamReader(new FileInputStream(definition.getInputFile())); parser = new CSVParser(reader, CSVFormat.DEFAULT); Iterator<CSVRecord> it = parser.iterator(); if (it.hasNext()) { CSVRecord header = it.next(); for (String value : header) { headers.add(value); } while (it.hasNext()) { Sample sample = new Sample(); CSVRecord record = it.next(); for (int i = 0; i < record.size(); i++) { sample.put(headers.get(i), record.get(i)); } input.getSamples().add(sample); } } } catch (IOException e) { throw new RuntimeException(e); } finally { Utils.close(parser); } return input; }
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; try {//from w ww. jav a2 s . c o m 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); } }