List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:data.io.csv.CSVDataReader.java
/** * Seek for the next valid record in the CSV file * @return the next valid CSVRecord/*from w w w.java 2 s. co m*/ */ private CSVRecord nextValidCSVRecord() { CSVRecord res = null; boolean abort = !csvIt.hasNext(); boolean done = false; //TODO : re-think logic : csvIt will not return nulls but throws IOException wrapped in RuntimeException while (!abort && !done) { // Try to read a CSV line res = (csvIt.hasNext()) ? csvIt.next() : null; // Break if nothing readable if (res == null) { abort = true; continue; } // Skip invalid and empty lines String key = res.get("key"); if (key != null && !key.isEmpty()) { done = true; continue; } } return done ? res : null; }
From source file:co.cask.hydrator.plugin.CSVParser.java
private StructuredRecord createStructuredRecord(CSVRecord record, StructuredRecord in) { StructuredRecord.Builder builder = StructuredRecord.builder(outSchema); int i = 0;//from w w w .j a v a 2 s . c om for (Field field : fields) { String name = field.getName(); // If the field specified in the output field is present in the input, then // it's directly copied into the output, else field is parsed in from the CSV parser. if (in.get(name) != null) { builder.set(name, in.get(name)); } else { builder.set(name, TypeConvertor.get(record.get(i), field.getSchema().getType())); ++i; } } return builder.build(); }
From source file:br.com.hslife.orcamento.service.ImportacaoLancamentoService.java
@Override public void processarArquivoCSVImportado(Arquivo arquivo, Conta conta) throws ApplicationException { try {//from w w w .ja v a2s . c om // Declarao e leitura dos dados do CSV final Reader reader = new InputStreamReader(new ByteArrayInputStream(arquivo.getDados()), "UTF-8"); final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader()); // Declarao das variveis LancamentoImportado lancamentoImportado = new LancamentoImportado(); for (CSVRecord record : parser) { int quantidade = Integer.parseInt(record.get("QUANTIDADE")); lancamentoImportado.setConta(conta); lancamentoImportado.setData(new SimpleDateFormat("yyyy-MM-dd").parse(record.get("DATA"))); lancamentoImportado.setHistorico(record.get("HISTORICO")); lancamentoImportado.setValor(Double.parseDouble(record.get("VALOR"))); lancamentoImportado.setMoeda(record.get("MOEDA")); lancamentoImportado.setDocumento(record.get("DOCUMENTO")); lancamentoImportado.setObservacao(record.get("OBSERVACAO")); lancamentoImportado.setCategoria(record.get("CATEGORIA")); lancamentoImportado.setFavorecido(record.get("FAVORECIDO")); lancamentoImportado.setMeiopagamento(record.get("MEIOPAGAMENTO")); lancamentoImportado.setHash(lancamentoImportado.getFullLabel()); // Insere o lanamento importado X vezes de acordo com o campo QUANTIDADE for (int i = 1; i <= quantidade; i++) { getRepository().save(lancamentoImportado.clonarLancamento(i)); } } // Fecha os streams parser.close(); reader.close(); } catch (Exception e) { throw new ApplicationException(e); } }
From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java
public GeoEventDataStoreProxy() { try (InputStream is = GeoEventDataStoreProxy.class.getResourceAsStream("/arcgisservers.properties")) { Properties props = new Properties(); props.load(is);/*from w ww. j a v a 2s . c o m*/ StringReader csvServers = new StringReader(props.getProperty("servers", "")); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(csvServers); ServerInfo currInfo; String currServerName = "<not initialized>"; String username, password; int port; Iterator<CSVRecord> iterator = records.iterator(); if (iterator.hasNext()) { CSVRecord record = iterator.next(); int size = record.size(); for (int i = 0; i < size; i++) { try { currInfo = new ServerInfo(); currServerName = record.get(i); currInfo.url = new URL(props.getProperty(currServerName + ".url", "")); port = (currInfo.url.getPort() == -1) ? getDefaultPortForScheme(currInfo.url.getProtocol()) : currInfo.url.getPort(); currInfo.authscope = new AuthScope(currInfo.url.getHost(), port); username = props.getProperty(currServerName + ".username", ""); password = props.getProperty(currServerName + ".password", ""); if (!StringUtils.isEmpty(username)) { username = username.replace('\\', '/'); String encryptedPassword = Crypto.doEncrypt(password); currInfo.credentials = new UsernameEncryptedPasswordCredentials(username, encryptedPassword); currInfo.ntCredentials = new NTCredentialsEncryptedPassword( username + ":" + encryptedPassword); } currInfo.httpContext = createContextForServer(currInfo); String tokenUrlKey = currServerName + ".tokenUrl"; String tokenUrl = props.getProperty(tokenUrlKey); if (tokenUrl != null) { currInfo.tokenUrl = new URL(tokenUrl); } username = props.getProperty(currServerName + ".gisTierUsername", ""); if (!StringUtils.isEmpty(username)) { password = props.getProperty(currServerName + ".gisTierPassword", ""); currInfo.gisTierUsername = username; currInfo.gisTierEncryptedPassword = Crypto.doEncrypt(password); } currInfo.name = currServerName; serverInfos.put(currServerName, currInfo); } catch (Throwable t) { LOG.log(Level.ALL, "Failed to parse properties for server " + currServerName, t); } } } } catch (Throwable t) { LOG.log(Level.SEVERE, "Unable to initialize. Will not be able to proxy any requests.", t); } }
From source file:com.xceptance.xlt.common.util.CSVBasedURLAction.java
/** * Constructor based upon read CSV data//from w w w . ja v a2s. c om * * @param record * the record to process * @param interpreter * the bean shell interpreter to use * @throws UnsupportedEncodingException * @throws MalformedURLException */ public CSVBasedURLAction(final CSVRecord record, final ParamInterpreter interpreter) throws UnsupportedEncodingException, MalformedURLException { // no bean shell, so we do not do anything, satisfy final here this.interpreter = interpreter; // the header is record 1, so we have to subtract one, for autonaming this.name = StringUtils.defaultIfBlank(record.get(NAME), "Action-" + (record.getRecordNumber() - 1)); // take care of type String _type = StringUtils.defaultIfBlank(record.get(TYPE), TYPE_ACTION); if (!_type.equals(TYPE_ACTION) && !_type.equals(TYPE_STATIC) && !_type.equals(TYPE_XHR_ACTION)) { XltLogger.runTimeLogger.warn(MessageFormat.format("Unknown type '{0}' in line {1}, defaulting to 'A'", _type, record.getRecordNumber())); _type = TYPE_ACTION; } this.type = _type; // we need at least an url, stop here of not given this.urlString = record.get(URL); if (this.urlString == null) { throw new IllegalArgumentException(MessageFormat .format("No url given in record in line {0}. Need at least that.", record.getRecordNumber())); } this.url = interpreter == null ? new URL(this.urlString) : null; // take care of method String _method = StringUtils.defaultIfBlank(record.get(METHOD), GET); if (!_method.equals(GET) && !_method.equals(POST)) { XltLogger.runTimeLogger.warn(MessageFormat.format( "Unknown method '{0}' in line {1}, defaulting to 'GET'", _method, record.getRecordNumber())); _method = GET; } this.method = _method; // get the response code validator this.httpResponseCodeValidator = StringUtils.isNotBlank(record.get(RESPONSECODE)) ? new HttpResponseCodeValidator(Integer.parseInt(record.get(RESPONSECODE))) : HttpResponseCodeValidator.getInstance(); // compile pattern only, if no interpreter shall be used this.regexpString = StringUtils.isNotEmpty(record.get(REGEXP)) ? record.get(REGEXP) : null; if (interpreter == null) { this.regexp = StringUtils.isNotEmpty(regexpString) ? RegExUtils.getPattern(regexpString) : null; } else { this.regexp = null; } this.xPath = StringUtils.isNotBlank(record.get(XPATH)) ? record.get(XPATH) : null; this.text = StringUtils.isNotEmpty(record.get(TEXT)) ? record.get(TEXT) : null; this.encoded = StringUtils.isNotBlank(record.get(ENCODED)) ? Boolean.parseBoolean(record.get(ENCODED)) : false; // ok, get all the parameters for (int i = 1; i <= DYNAMIC_GETTER_COUNT; i++) { xpathGetterList.add(StringUtils.isNotBlank(record.get(XPATH_GETTER_PREFIX + i)) ? record.get(XPATH_GETTER_PREFIX + i) : null); regexpGetterList.add(StringUtils.isNotBlank(record.get(REGEXP_GETTER_PREFIX + i)) ? record.get(REGEXP_GETTER_PREFIX + i) : null); } // ok, this is the tricky part this.parameters = StringUtils.isNotBlank(record.get(PARAMETERS)) ? setupParameters(record.get(PARAMETERS)) : null; }
From source file:frames.MainGUI.java
public void LoadFeeDataToJTable(JTable t, File file) { try {/*ww w .j a va 2s . c o m*/ CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); //t.setModel(tm); DefaultTableModel model = new DefaultTableModel(); //model.setRowCount(0); for (CSVRecord c : parser) { if (c.getRecordNumber() == 1) { model.addColumn(c.get(datatype.GlobalVariable.TYPE)); model.addColumn(c.get(datatype.GlobalVariable.AMOUNT)); model.addColumn(c.get(datatype.GlobalVariable.PAID_BY)); model.addColumn(c.get(datatype.GlobalVariable.PAYER)); t.setModel(model); model = (DefaultTableModel) t.getModel(); continue; } model.addRow( new Object[] { c.get(datatype.GlobalVariable.TYPE), c.get(datatype.GlobalVariable.AMOUNT), c.get(datatype.GlobalVariable.PAID_BY), c.get(datatype.GlobalVariable.PAYER) }); } } catch (Exception e) { System.out.println(e); } }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * Add a model record's information//w w w . j a v a 2 s .co m */ private void parseModelRecord(CSVRecord csvRecord) { // If the id is set for the model we will not set the model's id to it but we will keep a reference to it // because the properties CSV file might use this as the reference id for adding model properties String id = csvRecord.get(0); if (StringUtils.isSet(id)) { modelID = id; } modelName = csvRecord.get(2); modelPurpose = csvRecord.get(3); }
From source file:com.adobe.aem.demo.communities.Loader.java
private static List<NameValuePair> buildNVP(CSVRecord record, int start) { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("_charset_", "UTF-8")); for (int i = start; i < record.size() - 1; i = i + 2) { if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) { // We have a non String hint to pass to the POST servlet String name = record.get(i); String value = record.get(i + 1); if (value.equals("TRUE")) { value = "true"; }// w w w . j a v a2 s . c o m if (value.equals("FALSE")) { value = "false"; } int hint = name.indexOf("@"); if (hint > 0) { logger.debug(name.substring(0, hint) + "@TypeHint:" + name.substring(1 + hint)); nameValuePairs.add(new BasicNameValuePair(name.substring(0, hint) + "@TypeHint", name.substring(1 + hint))); name = name.substring(0, hint); } else { nameValuePairs.add(new BasicNameValuePair(name + "@TypeHint", "String")); } // We have multiple values to pass to the POST servlet, e.g. for a String[] int multiple = value.indexOf("|"); if (multiple > 0) { List<String> values = Arrays.asList(value.split("\\|", -1)); for (String currentValue : values) { nameValuePairs.add(new BasicNameValuePair(name, currentValue)); logger.debug(name + " " + currentValue); } } else { nameValuePairs.add(new BasicNameValuePair(name, value)); } logger.debug("Setting property " + name + " with value " + value); } } return nameValuePairs; }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * @param csvRecord/*from w w w.ja v a 2 s . c o m*/ * @param fields * @return True if csvRecord matches a header with given fields */ private boolean isHeaderRecord(CSVRecord csvRecord, String[] fields) { if (csvRecord.getRecordNumber() != 1 && csvRecord.size() != fields.length) { return false; } for (int i = 0; i < fields.length; i++) { String field = fields[i]; if (!field.equalsIgnoreCase(csvRecord.get(i))) { return false; } } return true; }
From source file:com.amazonaws.services.dynamodbv2.online.index.integration.tests.ViolationCorrectionTest.java
/** * Iterates through detection output file: first leave updates blank based on missing updates per key. * Once it has reached the missing update number, it removes the expected gsi values as per the specified 'missingGsiExpectedHashValues'. * Note that once blank number is reached, it also starts adding updates. * It then iterates over the rows again and adds values for Yes/No/Invalid in the delete column. * It returns all error records, if present. If not, it returns all records. *//*w w w . j av a 2s. c om*/ private static List<List<String>> createCorrectionFile(final String detectionFile, final String correctionFile, final String gsiHashKeyName, final String gsiHashKeyType, final String gsiRangeKeyName, final String gsiRangeKeyType, final Map<String, String> tableHashToNewGsiHashValueMap, final Map<String, String> tableHashToNewGsiRangeValueMap, final int missingUpdatesPerKey, final int missingGsiExpectedHashValues, final int invalidValuesForDelete, final int numOfYesForDelete, final int numOfNoForDelete) throws IOException { List<List<String>> errorRecords = null; List<List<String>> allRecords = null; BufferedReader br = null; BufferedWriter bw = null; CSVParser parser = null; CSVPrinter csvPrinter = null; try { br = new BufferedReader(new FileReader(new File(detectionFile))); bw = new BufferedWriter(new FileWriter(new File(correctionFile))); parser = new CSVParser(br, TestUtils.csvFormat); csvPrinter = new CSVPrinter(bw, TestUtils.csvFormat); List<CSVRecord> detectorRecords = parser.getRecords(); int hashMissingUpdates = 0; int rangeMissingUpdates = 0; int missingGsiExpectedHashValuesCurrent = 0; // Print Header Map<String, Integer> header = parser.getHeaderMap(); csvPrinter.printRecord(header.keySet()); allRecords = new ArrayList<List<String>>(); for (CSVRecord csvRecord : detectorRecords) { List<String> newRecord = new ArrayList<String>(); String tableHashKeyRecorded = csvRecord.get(ViolationRecord.TABLE_HASH_KEY); String hashKeyViolationType = null; if (gsiHashKeyName != null) { hashKeyViolationType = csvRecord.get(ViolationRecord.GSI_HASH_KEY_VIOLATION_TYPE); } String rangeKeyViolationType = null; if (gsiRangeKeyName != null) { rangeKeyViolationType = csvRecord.get(ViolationRecord.GSI_RANGE_KEY_VIOLATION_TYPE); } for (int i = 0; i < csvRecord.size(); i++) { newRecord.add(i, csvRecord.get(i)); } String newGsiVal = null; if (hashKeyViolationType != null && (hashKeyViolationType.equals("Size Violation") || hashKeyViolationType.equals("Type Violation"))) { if (hashMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); hashMissingUpdates++; continue; } //Remove expected hash Values if (missingGsiExpectedHashValuesCurrent < missingGsiExpectedHashValues) { newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY)); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY), ""); missingGsiExpectedHashValuesCurrent++; } newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiHashKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiHashValueMap.put(tableHashKeyRecorded, newGsiVal); } if (rangeKeyViolationType != null && (rangeKeyViolationType.equals("Size Violation") || rangeKeyViolationType.equals("Type Violation"))) { if (rangeMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); rangeMissingUpdates++; continue; } newRecord.remove(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiRangeKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiRangeValueMap.put(tableHashKeyRecorded, newGsiVal); } allRecords.add(newRecord); } // Add 'Y' or 'N' for delete column if (numOfNoForDelete > 0 || numOfYesForDelete > 0 || invalidValuesForDelete > 0) { errorRecords = new ArrayList<List<String>>(); int numOfYesAdded = 0; int numOfNoAdded = 0; int numOfInvalids = 0; for (List<String> record : allRecords) { if (numOfInvalids < invalidValuesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "xx"); numOfInvalids++; errorRecords.add(record); continue; } if (numOfYesAdded < numOfYesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "Y"); numOfYesAdded++; continue; } if (numOfNoAdded < numOfNoForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "N"); numOfNoAdded++; continue; } } } // Add all records to file csvPrinter.printRecords(allRecords); } finally { br.close(); bw.close(); parser.close(); csvPrinter.close(); } if (errorRecords != null) return errorRecords; else return allRecords; }