All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testingisdocumenting.webtau.data.DataCsv Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 webtau maintainers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.testingisdocumenting.webtau.data;

import org.testingisdocumenting.webtau.data.table.TableData;
import org.testingisdocumenting.webtau.utils.CsvUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.stream.Collectors.*;
import static org.testingisdocumenting.webtau.data.DataContentUtils.*;

public class DataCsv {
    /**
     * Use data.csv.table to read data as {@link TableData} from CSV file.
     * 

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return table data with CSV content */ public TableData table(String fileOrResourcePath) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), (text) -> tableFromListOfMaps(CsvUtils.parse(text))); } /** * Use data.csv.table to read data as {@link TableData} from CSV file. It will convert values based on provided converter. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @param valueConverter converter to convert values from String * @return table data with CSV content */ public TableData table(String fileOrResourcePath, DataCsvValueConverter valueConverter) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), (text) -> tableFromListOfMaps(parseAndCovertValues(valueConverter, text))); } /** * Use data.csv.table to read data as {@link TableData} from CSV file. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @return table data with CSV content */ public TableData table(Path filePath) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), (text) -> tableFromListOfMaps(CsvUtils.parse(text))); } /** * Use data.csv.table to read data as {@link TableData} from CSV file. It will convert values based on provided converter. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @param valueConverter converter to convert values from String * @return table data with CSV content */ public TableData table(Path filePath, DataCsvValueConverter valueConverter) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), (text) -> tableFromListOfMaps(parseAndCovertValues(valueConverter, text))); } /** * Use data.csv.tableAutoConverted to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return table data with CSV content */ public TableData tableAutoConverted(String fileOrResourcePath) { return table(fileOrResourcePath, new DataCsvAutoNumberConversion()); } /** * Use data.csv.tableAutoConverted to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @return table data with CSV content */ public TableData tableAutoConverted(Path filePath) { return table(filePath, new DataCsvAutoNumberConversion()); } /** * Use data.csv.listOfMaps to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return list of maps */ public List> listOfMaps(String fileOrResourcePath) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), CsvUtils::parse); } /** * Use data.csv.listOfMaps to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @return list of maps */ public List> listOfMaps(Path filePath) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), CsvUtils::parse); } /** * Use data.csv.listOfMaps to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. It will convert values based on provided converter. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @param valueConverter converter to convert values from String * @return list of maps */ public List> listOfMaps(String fileOrResourcePath, DataCsvValueConverter valueConverter) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), (csv) -> parseAndCovertValues(valueConverter, csv)); } /** * Use data.csv.listOfMaps to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. It will convert values based on provided converter. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @param valueConverter converter to convert values from String * @return list of maps */ public List> listOfMaps(Path filePath, DataCsvValueConverter valueConverter) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), (csv) -> parseAndCovertValues(valueConverter, csv)); } /** * Use data.csv.listOfMapsAutoConverted to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. * Numeric values become values of Numeric type instead of String type. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return list of maps */ public List> listOfMapsAutoConverted(String fileOrResourcePath) { return listOfMaps(fileOrResourcePath, new DataCsvAutoNumberConversion()); } /** * Use data.csv.listOfMapsAutoConverted to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. * Numeric values become values of Numeric type instead of String type. *

* Passed path is either relative based on working dir or absolute file path. * @param filePath relative file path or absolute file path * @return list of maps */ public List> listOfMapsAutoConverted(Path filePath) { return listOfMaps(filePath, new DataCsvAutoNumberConversion()); } /** * Use data.csv.listOfMaps(header, path) to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. *

* Header will be taken from first parameter and first row of CSV file will not be treated as header. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param header header values * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return list of maps */ public List> listOfMaps(Collection header, String fileOrResourcePath) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), (text) -> CsvUtils.parse(header, text)); } /** * Use data.csv.listOfMaps(header, path) to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. *

* Header will be taken from first parameter and first row of CSV file will not be treated as header. *

* Passed path is either relative based on working dir or absolute file path. * @param header header values * @param filePath relative file path or absolute file path * @return list of maps */ public List> listOfMaps(Collection header, Path filePath) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), (text) -> CsvUtils.parse(header, text)); } /** * Use data.csv.listOfMapsAutoConverted(header, path) to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. *

* Header will be taken from first parameter and first row of CSV file will not be treated as header. * Numeric values become values of Numeric type instead of String type. *

* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. * @param header header values * @param fileOrResourcePath relative file path, absolute file path or classpath resource path * @return list of maps */ public List> listOfMapsAutoConverted(List header, String fileOrResourcePath) { return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), (text) -> convertValues(new DataCsvAutoNumberConversion(), CsvUtils.parse(header, text))); } /** * Use data.csv.listOfMapsAutoConverted(header, path) to read data as {@link java.util.List} of {@link java.util.Map} from CSV file. * Header will be taken from first parameter and first row of CSV file will not be treated as header. * Numeric values become values of Numeric type instead of String type. * Passed path is either relative based on working dir or absolute file path. * @param header header values * @param filePath relative file path or absolute file path * @return list of maps */ public List> listOfMapsAutoConverted(List header, Path filePath) { return parseCsvTextAsStep(DataPath.fromFilePath(filePath), (text) -> convertValues(new DataCsvAutoNumberConversion(), CsvUtils.parse(header, text))); } /** * Use data.csv.write to write data to CSV file. * @param path relative path or absolute file path of file to create * @param rows list of maps to write as CSV * @return full path to a newly created file */ public Path write(Path path, List> rows) { return writeCsvContentAsStep(path, () -> CsvUtils.serialize(rows)); } /** * Use data.csv.write to write data to CSV file. * @param path relative path or absolute file path of file to create * @param rows list of maps to write as CSV * @return full path to a newly created file */ public Path write(String path, List> rows) { return writeCsvContentAsStep(Paths.get(path), () -> CsvUtils.serialize(rows)); } /** * Use data.csv.write to write data to CSV file. * @param path relative path or absolute file path of file to create * @param tableData {@link TableData} to write as CSV * @return full path to a newly created file */ public Path write(String path, TableData tableData) { return writeCsvContentAsStep(Paths.get(path), tableData::toCsv); } private static R parseCsvTextAsStep(DataPath dataPath, Function convertor) { return readAndConvertTextContentFromDataPathAsStep("csv", dataPath, convertor); } private static Path writeCsvContentAsStep(Path path, Supplier convertor) { return writeTextContentAsStep("csv", path, convertor); } @SuppressWarnings("unchecked") private TableData tableFromListOfMaps(List listOfMaps) { if (listOfMaps.isEmpty()) { return new TableData(Collections.emptyList()); } Map firstRow = (Map) listOfMaps.get(0); TableData result = new TableData(firstRow.keySet().stream()); listOfMaps.forEach((row) -> { Map asMap = (Map) row; result.addRow(asMap.values().stream()); }); return result; } private static List> parseAndCovertValues(DataCsvValueConverter converter, String csv) { List> listOfMaps = CsvUtils.parse(csv); return convertValues(converter, listOfMaps); } private static List> convertValues(DataCsvValueConverter converter, List> data) { return data.stream().map((e) -> convertRecord(converter, e)).collect(toList()); } private static Map convertRecord(DataCsvValueConverter converter, Map row) { Map entry = new LinkedHashMap<>(); row.forEach((k, v) -> entry.put(k, converter.convert(k, ((Object) v).toString()))); return entry; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy