List of usage examples for org.apache.commons.csv CSVParser CSVParser
public CSVParser(final Reader reader, final CSVFormat format) throws IOException
If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .
From source file:org.mercycorps.translationcards.txcmaker.GetTxcServlet.java
private void produceTxcJson(Drive drive, HttpServletRequest req, HttpServletResponse resp) throws IOException { TxcPortingUtility.ExportSpec exportSpec = new TxcPortingUtility.ExportSpec() .setDeckLabel(req.getParameter("deckName")).setPublisher(req.getParameter("publisher")) .setDeckId(req.getParameter("deckId")).setLicenseUrl(req.getParameter("licenseUrl")) .setLocked(req.getParameter("locked") != null); String spreadsheetFileId = req.getParameter("docId"); Drive.Files.Export sheetExport = drive.files().export(spreadsheetFileId, CSV_EXPORT_TYPE); Reader reader = new InputStreamReader(sheetExport.executeMediaAsInputStream()); CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader()); try {//ww w .j a v a2 s . c o m for (CSVRecord row : parser) { String language = row.get(SRC_HEADER_LANGUAGE); TxcPortingUtility.CardSpec card = new TxcPortingUtility.CardSpec() .setLabel(row.get(SRC_HEADER_LABEL)).setFilename(row.get(SRC_HEADER_FILENAME)) .setTranslationText(row.get(SRC_HEADER_TRANSLATION_TEXT)); exportSpec.addCard(language, card); } } finally { parser.close(); reader.close(); } resp.getWriter().println(TxcPortingUtility.buildTxcJson(exportSpec)); }
From source file:org.nuxeo.ecm.directory.DirectoryCSVLoader.java
/** * Loads the CSV data file based on the provided schema, and creates the corresponding entries using the provided * loader.//from w w w . j a v a 2s . c o m * * @param dataFileName the file name containing CSV data * @param delimiter the CSV column separator * @param schema the data schema * @param loader the actual consumer of loaded rows * @since 8.4 */ public static void loadData(String dataFileName, char delimiter, Schema schema, Consumer<Map<String, Object>> loader) throws DirectoryException { try (InputStream in = getResource(dataFileName); // CSVParser csvParser = new CSVParser(new InputStreamReader(in, "UTF-8"), CSVFormat.DEFAULT.withDelimiter(delimiter).withHeader())) { Map<String, Integer> header = csvParser.getHeaderMap(); List<Field> fields = new ArrayList<>(); for (String columnName : header.keySet()) { Field field = schema.getField(columnName.trim()); if (field == null) { throw new DirectoryException( "Column not found: " + columnName + " in schema: " + schema.getName()); } fields.add(field); } int lineno = 1; // header was first line for (CSVRecord record : csvParser) { lineno++; if (record.size() == 0 || record.size() == 1 && StringUtils.isBlank(record.get(0))) { // NXP-2538: allow columns with only one value but skip empty lines continue; } if (!record.isConsistent()) { log.error("Invalid column count while reading CSV file: " + dataFileName + ", line: " + lineno + ", values: " + record); continue; } Map<String, Object> map = new HashMap<String, Object>(); for (int i = 0; i < header.size(); i++) { Field field = fields.get(i); String value = record.get(i); Object v = CSV_NULL_MARKER.equals(value) ? null : decode(field, value); map.put(field.getName().getPrefixedName(), v); } loader.accept(map); } } catch (IOException e) { throw new DirectoryException("Read error while reading data file: " + dataFileName, e); } }
From source file:org.nuxeo.ecm.platform.filemanager.service.extension.CSVZipImporter.java
@Override public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite, String filename, TypeManager typeService) throws IOException { ZipFile zip = null;/*from ww w. j av a2 s . co m*/ try (CloseableFile source = content.getCloseableFile()) { zip = getArchiveFileIfValid(source.getFile()); if (zip == null) { return null; } DocumentModel container = documentManager.getDocument(new PathRef(path)); ZipEntry index = zip.getEntry(MARKER); try (Reader reader = new InputStreamReader(zip.getInputStream(index)); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());) { Map<String, Integer> header = csvParser.getHeaderMap(); for (CSVRecord csvRecord : csvParser) { String type = null; String id = null; Map<String, String> stringValues = new HashMap<>(); for (String headerValue : header.keySet()) { String lineValue = csvRecord.get(headerValue); if ("type".equalsIgnoreCase(headerValue)) { type = lineValue; } else if ("id".equalsIgnoreCase(headerValue)) { id = lineValue; } else { stringValues.put(headerValue, lineValue); } } boolean updateDoc = false; // get doc for update DocumentModel targetDoc = null; if (id != null) { // update ? String targetPath = new Path(path).append(id).toString(); if (documentManager.exists(new PathRef(targetPath))) { targetDoc = documentManager.getDocument(new PathRef(targetPath)); updateDoc = true; } } // create doc if needed if (targetDoc == null) { if (type == null) { log.error("Can not create doc without a type, skipping line"); continue; } if (id == null) { id = IdUtils.generateStringId(); } targetDoc = documentManager.createDocumentModel(path, id, type); } // update doc properties DocumentType targetDocType = targetDoc.getDocumentType(); for (String fname : stringValues.keySet()) { String stringValue = stringValues.get(fname); Field field = null; boolean usePrefix = false; String schemaName = null; String fieldName = null; if (fname.contains(":")) { if (targetDocType.hasField(fname)) { field = targetDocType.getField(fname); usePrefix = true; } } else if (fname.contains(".")) { String[] parts = fname.split("\\."); schemaName = parts[0]; fieldName = parts[1]; if (targetDocType.hasSchema(schemaName)) { field = targetDocType.getField(fieldName); usePrefix = false; } } else { if (targetDocType.hasField(fname)) { field = targetDocType.getField(fname); usePrefix = false; schemaName = field.getDeclaringType().getSchemaName(); } } if (field != null) { Serializable fieldValue = getFieldValue(field, stringValue, zip); if (fieldValue != null) { if (usePrefix) { targetDoc.setPropertyValue(fname, fieldValue); } else { targetDoc.setProperty(schemaName, fieldName, fieldValue); } } } } if (updateDoc) { documentManager.saveDocument(targetDoc); } else { documentManager.createDocument(targetDoc); } } } return container; } finally { IOUtils.closeQuietly(zip); } }
From source file:org.nuxeo.ecm.ui.web.auth.digest.DigestAuthenticator.java
public static Map<String, String> splitParameters(String auth) { Map<String, String> map = new HashMap<>(); try (CSVParser reader = new CSVParser(new StringReader(auth), CSVFormat.DEFAULT)) { Iterator<CSVRecord> iterator = reader.iterator(); if (iterator.hasNext()) { CSVRecord record = iterator.next(); for (String itemPairStr : record) { itemPairStr = StringUtils.remove(itemPairStr, QUOTE); String[] parts = itemPairStr.split(EQUAL_SEPARATOR, 2); if (parts == null) { continue; } else { map.put(parts[0].trim(), parts[1].trim()); }// ww w .ja va2 s . co m } } } catch (IOException e) { log.error(e.getMessage(), e); } return map; }
From source file:org.nuxeo.theme.presets.PaletteParser.java
public static Map<String, String> parseCsv(String text) { Map<String, String> properties = new HashMap<>(); if (text == null) { return properties; }//from w w w . j av a 2s . c om try (StringReader sr = new StringReader(text); CSVParser reader = new CSVParser(sr, CSVFormat.DEFAULT.withDelimiter('\t'))) { for (CSVRecord record : reader) { properties.put(record.get(0), record.get(1)); } } catch (IOException e) { log.error(e, e); } return properties; }
From source file:org.nuxeo.theme.Utils.java
public static List<String> csvToList(String str) throws IOException { if ("".equals(str) || str == null) { return new ArrayList<>(); }/*from w ww . j av a2 s . com*/ StringReader sr = new StringReader(str); try (CSVParser reader = new CSVParser(sr, CSVFormat.DEFAULT.withDelimiter(','))) { Iterator<CSVRecord> iterator = reader.iterator(); if (!iterator.hasNext()) { return new ArrayList<>(); } else { CSVRecord nextRecord = iterator.next(); List<String> result = new ArrayList<>(nextRecord.size()); for (String value : nextRecord) { result.add(value); } return result; } } }
From source file:org.nuxeo.theme.vocabularies.VocabularyManager.java
public List<VocabularyItem> getItems(String name) { VocabularyType vocabularyType = (VocabularyType) Manager.getTypeRegistry().lookup(TypeFamily.VOCABULARY, name);/* ww w . j a va 2 s. c o m*/ if (vocabularyType == null) { return null; } final String path = vocabularyType.getPath(); final String className = vocabularyType.getClassName(); if (path == null && className == null) { log.error("Must specify a class name or a path for vocabulary: " + name); return null; } if (path != null && className != null) { log.error("Cannot specify both a class name and a path for vocabulary: " + name); return null; } if (className != null) { Vocabulary vocabulary = getInstance(className); if (vocabulary == null) { log.error("Vocabulary class not found: " + className); return null; } return vocabulary.getItems(); } if (path != null) { if (!path.endsWith(".csv")) { log.error("Only .csv vocabularies are supported: " + path); return null; } final List<VocabularyItem> items = new ArrayList<>(); try (InputStream is = getClass().getClassLoader().getResourceAsStream(path)) { if (is == null) { log.error("Vocabulary file not found: " + path); return null; } try (CSVParser reader = new CSVParser(new InputStreamReader(is, Charsets.UTF_8), CSVFormat.DEFAULT)) { for (CSVRecord record : reader) { final String value = record.get(0); String label = value; if (record.size() >= 2) { label = record.get(1); } items.add(new VocabularyItem(value, label)); } } } catch (IOException e) { log.error("Could not read vocabulary file: " + path, e); } return items; } return null; }
From source file:org.ofbiz.magento.CatalogServices.java
public static Map<String, Object> importMagentoProducts(DispatchContext ctx, Map<String, ? extends Object> context) { Delegator delegator = ctx.getDelegator(); LocalDispatcher dispatcher = ctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); Map<String, Object> serviceResult = new HashMap<String, Object>(); int errorRecords = 0; int processedRecords = 0; try {/*ww w . j a v a2s . c om*/ serviceResult = dispatcher.runSync("getMagentoProducts", context); if (ServiceUtil.isError(serviceResult)) { ServiceUtil.returnError( UtilProperties.getMessage(resource, "MagentoErrorInGettingProductsFromMagento", locale)); } serviceResult.clear(); File csvFile = new File(System.getProperty("ofbiz.home") + "/runtime/tmp/MagentoProductInfo.csv"); BufferedReader reader = new BufferedReader(new FileReader(csvFile)); String fieldDelimiter = ","; String fieldEncapsulator = "\""; CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter.charAt(0)) .withQuote(fieldEncapsulator.charAt(0)).withIgnoreEmptyLines(true) .withIgnoreSurroundingSpaces(true); CSVParser parser = new CSVParser(reader, csvFormat); Boolean isFirstLine = true; String[] mappedKeys = null; Map<String, Object> processedResult = new HashMap<String, Object>(); Map<String, Object> serviceResp = new HashMap<String, Object>(); for (CSVRecord csvRecord : csvFormat.parse(reader)) { int csvRecordSize = csvRecord.size(); if (isFirstLine) { mappedKeys = new String[csvRecordSize]; for (int i = 0; i < csvRecordSize; i++) { mappedKeys[i] = csvRecord.get(i); } isFirstLine = false; } else { Map<String, Object> mappedValues = new HashMap<String, Object>(); for (int i = 0; i < mappedKeys.length; i++) { String csvValue = csvRecord.get(i); String value = (i < csvRecordSize ? csvValue : ""); if (UtilValidate.isNotEmpty(value)) { value = value.trim().replaceAll(">|<", ""); } mappedValues.put(mappedKeys[i], value); } Map<String, Object> serviceInMap = UtilMisc.toMap("userLogin", userLogin, "productId", mappedValues.get("Product Id"), "externalId", mappedValues.get("Product Id"), "sku", mappedValues.get("SKU"), "productTypeId", mappedValues.get("Product Type Id"), "productName", mappedValues.get("Product Name"), "bundleParentId", mappedValues.get("Bundle Parent Id"), "configurableParentId", mappedValues.get("Configurable Parent Id"), "groupedParentId", mappedValues.get("Grouped Parent Id"), "description", mappedValues.get("Description"), "longDescription", mappedValues.get("Long Description"), "price", mappedValues.get("Price"), "taxClassId", mappedValues.get("Tax Class Id"), "categoryIds", mappedValues.get("Category Ids"), "webSiteIds", mappedValues.get("Web Site Ids"), "thumbnailImageUrl", mappedValues.get("Thumbnail Image Url"), "smallImageUrl", mappedValues.get("Small Image Url"), "originalImageUrl", mappedValues.get("Original Image Url"), "urlKey", mappedValues.get("Url Key"), "urlPath", mappedValues.get("Url Path"), "metaDescription", mappedValues.get("Meta Description"), "metaKeyword", mappedValues.get("Meta Keyword"), "metaTitle", mappedValues.get("Meta Title"), "status", mappedValues.get("Status"), "specialFromDate", mappedValues.get("Special From Date"), "specialPrice", mappedValues.get("Special Price"), "createdDate", mappedValues.get("Created At"), "lastModifiedDate", mappedValues.get("Updated At")); Boolean isError = false; if (UtilValidate.isEmpty(serviceInMap.get("productId"))) { isError = true; Debug.logError("Product ID is missing : ", module); } String productId = (String) serviceInMap.get("productId"); productId = "MAG-" + productId.trim(); serviceInMap.put("productId", productId); String bundleParentId = ((String) serviceInMap.get("bundleParentId")).trim(); if (!("NA".equalsIgnoreCase(bundleParentId))) { bundleParentId = "MAG-" + bundleParentId; } serviceInMap.put("bundleParentId", bundleParentId); String configurableParentId = ((String) serviceInMap.get("configurableParentId")).trim(); if (!("NA".equalsIgnoreCase(configurableParentId))) { configurableParentId = "MAG-" + configurableParentId; } serviceInMap.put("configurableParentId", configurableParentId); String groupedParentId = ((String) serviceInMap.get("groupedParentId")).trim(); if (!("NA".equalsIgnoreCase(groupedParentId))) { groupedParentId = "MAG-" + groupedParentId; } serviceInMap.put("groupedParentId", groupedParentId); if (UtilValidate.isEmpty(serviceInMap.get("productTypeId"))) { isError = true; Debug.logError("Product Type ID is missing for product id : " + productId, module); } if (UtilValidate.isEmpty(serviceInMap.get("productName"))) { isError = true; Debug.logError("Name is missing for product id : " + productId, module); } if (UtilValidate.isEmpty(serviceInMap.get("price"))) { isError = true; Debug.logError("Price is missing for product id : " + productId, module); } Debug.logInfo("Begin processing for productId [" + productId + "]", module); if (!isError) { Debug.logInfo("Create / Update product having productId [" + productId + "]", module); serviceResult = dispatcher.runSync("createMagentoProducts", serviceInMap, 600, true); } if (ServiceUtil.isError(serviceResult) || isError) { errorRecords++; processedResult.put(productId, "Error"); Debug.logInfo("Completed processing for productId [" + productId + "] with ERROR ", module); if (ServiceUtil.isError(serviceResult)) { Debug.logInfo(ServiceUtil.getErrorMessage(serviceResp), module); } } else { processedResult.put(productId, "Success"); Debug.logInfo("Processing successfully completed for product [" + productId + "]", module); } processedRecords++; } } } catch (GenericServiceException e) { Debug.logError(e, e.getMessage(), module); e.printStackTrace(); } catch (IOException e) { Debug.logError(e, e.getMessage(), module); e.printStackTrace(); } return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "MagentoProductsHaveBeenImportedSuccessfully", UtilMisc.toMap("processedRecords", processedRecords, "successRecords", (processedRecords - errorRecords)), locale)); }
From source file:org.openestate.io.core.CsvFormat.java
/** * Creates a {@link CsvParser} from a {@link Reader} with CSV data. * * @param input/*from w w w . j a v a 2 s.com*/ * CSV input * * @return * created parser * * @throws IOException * if CSV is not readable */ public Parser parse(Reader input) throws IOException { return this.newParser(new CSVParser(input, this.getFormat())); }
From source file:org.opennms.netmgt.integrations.R.RScriptExecutor.java
/** * Convert the CSV string to an immutable table. *//*from w w w . j av a 2 s. c o m*/ protected static ImmutableTable<Long, String, Double> fromCsv(final String csv) throws IOException { ImmutableTable.Builder<Long, String, Double> builder = ImmutableTable.builder(); try (StringReader reader = new StringReader(csv); CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader());) { long rowIndex = 0; Map<String, Integer> headerMap = parser.getHeaderMap(); for (CSVRecord record : parser) { for (String key : headerMap.keySet()) { Double value; try { value = Double.valueOf(record.get(key)); } catch (NumberFormatException e) { value = Double.NaN; } builder.put(rowIndex, key, value); } rowIndex++; } } return builder.build(); }