Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV Data Support #216

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions selcukes-databind/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private DataBind lookup(final String extension) {
return new YamlData();
case "json":
return new JsonData();
case "xml":
return new XmlData();
default:
throw new DataMapperException(String.format("File Type[%s] not supported...", extension));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) Ramesh Babu Prudhvi.
*
* 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 io.github.selcukes.databind;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

class XmlData extends AbstractDataBind {

XmlData() {
super(new XmlMapper());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* 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 io.github.selcukes.databind.csv;

import io.github.selcukes.databind.exception.DataMapperException;
import io.github.selcukes.databind.utils.Maps;
import lombok.experimental.UtilityClass;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@UtilityClass
public class CsvMapper {

public List<Map<String, String>> parse(Path filePath) {
try (var lines = Files.lines(filePath)) {
return Maps.asListOfMap(lines.map(line -> line.split(","))
.filter(line -> line.length != 0)
.map(Arrays::asList).collect(Collectors.toList()));
} catch (Exception e) {
throw new DataMapperException("Failed parsing CSV File: ", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@ public <V> Map<String, V> caseInsensitive(Map<String, V> map) {
newMap.putAll(map);
return newMap;
}

/**
* Skip the first row, then for each row, create a map from the headers to
* the values.
*
* @param cells The list of lists of strings that represent the table.
* @return A list of maps.
*/
public List<Map<String, String>> asListOfMap(List<List<String>> cells) {
var headers = cells.get(0);
return cells.stream().skip(1).map(row -> IntStream.range(0, headers.size())
.boxed()
.collect(Collectors.toMap(headers::get, row::get))).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import io.github.selcukes.databind.exception.DataMapperException;
import lombok.experimental.UtilityClass;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static io.github.selcukes.databind.properties.PropertiesMapper.systemProperties;

Expand Down Expand Up @@ -208,4 +211,18 @@ public String substitute(final String value, final String format) {
public static String normalizeText(final String text) {
return text != null ? text.replaceAll("\u00A0|\\r\\n|\\r|\\n", " ").trim() : null;
}

/**
* It splits the input string by newline, then splits each line by the delimiter, and finally returns a list of lists
* of strings
*
* @param line The string to be split into a list of lists.
* @param delimiter The delimiter to use when splitting the line.
* @return A list of lists of strings.
*/
public static List<List<String>> asListOfList(String line, String delimiter) {
return Arrays.stream(line.split("\n"))
.map(row -> Arrays.asList(row.split(delimiter)))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* 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 io.github.selcukes.databind.tests;

import io.github.selcukes.databind.csv.CsvMapper;
import lombok.SneakyThrows;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.util.Objects;

public class CsvTest {
@SneakyThrows
@Test
public void csvDataReaderTest() {
Path filePath = Path.of(
Objects.requireNonNull(CsvTest.class.getClassLoader().getResource("employee.csv")).toURI()
);
var csvData = CsvMapper.parse(filePath);
System.out.println(csvData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* 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 io.github.selcukes.databind.tests;

import io.github.selcukes.databind.DataMapper;
import io.github.selcukes.databind.annotation.DataFile;
import lombok.Data;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Map;

public class XmlTest {
@Test
public void xmlTest() {
CustomerInfo memberInfo = DataMapper.parse(CustomerInfo.class);
System.out.println(memberInfo.getMetaData().get("eventNotificationDate"));
System.out.println(memberInfo.getContactPersonList().get(0).get("firstName"));
}

@Data
@DataFile(fileName = "CustomerInfo.xml")
static class CustomerInfo {
Map<String, String> metaData;
String name;
String maturityDate;
Map<String, String> address;
List<Map<String, String>> contactPersonList;
}
}
42 changes: 42 additions & 0 deletions selcukes-databind/src/test/resources/CustomerInfo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<MemberProfile>
<metaData>
<eventNotificationDate>2019-03-06T07:32:26</eventNotificationDate>
<eventType>CREATE</eventType>
<refNo>1258D20190306T073226</refNo>
<status>SCD_SENT</status>
</metaData>
<name>Central Pacific Bank</name>
<maturityDate>2100-31-12</maturityDate>
<address>
<addressLine1>125 Street</addressLine1>
<addressLine2></addressLine2>
<city>Des moines</city>
<country>USA</country>
<state>IA</state>
<zip>50303</zip>
</address>
<contactPersonList>
<contactPerson>
<contactPersonId>1256</contactPersonId>
<email>[email protected]</email>
<firstName>Ramesh</firstName>
<lastName>Babu</lastName>
<mobilePhone>+15168978352</mobilePhone>
<title>Treasury</title>
<workPhone>+19087253123</workPhone>
</contactPerson>
<contactPerson>
<contactPersonId>5689</contactPersonId>
<email>[email protected]</email>
<firstName>Mohan</firstName>
<lastName>Babu</lastName>
<mobilePhone>+19011978123</mobilePhone>
<title>Treasury</title>
<workPhone>+19011253456</workPhone>
</contactPerson>
</contactPersonList>
</MemberProfile>




5 changes: 5 additions & 0 deletions selcukes-databind/src/test/resources/employee.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name , Email , Phone , Country
Rajeev Kumar Singh ,[email protected],+91-9999999999,India
Sachin Tendulkar,[email protected],+91-9999999998,India
Barak Obama,[email protected],+1-1111111111,United States
Donald Trump,[email protected],+1-2222222222,United States