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

Xml Mapper #161

Merged
merged 8 commits into from
Jul 9, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import io.github.selcukes.commons.exception.SelcukesException;
import io.github.selcukes.commons.xml.XmlMapper;
import io.github.selcukes.databind.xml.XmlMapper;
import io.github.selcukes.databind.DataMapper;
import io.github.selcukes.databind.utils.StringHelper;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

String folderPath() default "";

String rootFolder() default "";

boolean streamLoader() default false;

String sheetName() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package io.github.selcukes.databind.converters;

import io.github.selcukes.databind.utils.Clocks;

import java.time.LocalDate;

import static io.github.selcukes.databind.utils.Clocks.DATE_FORMAT;
import static java.time.LocalDate.parse;
import static java.time.format.DateTimeFormatter.ofPattern;
import static java.util.Optional.ofNullable;

public class LocalDateConverter extends DefaultConverter<LocalDate> {
private static final String DEFAULT_FORMAT = "yyyy-MM-dd";

@Override
public LocalDate convert(final String value) {
return convert(value, DEFAULT_FORMAT);
return convert(value, DATE_FORMAT);
}

@Override
public LocalDate convert(final String value, final String format) {
return parse(value, ofPattern(ofNullable(format).filter(f -> !f.isEmpty()).orElse(DEFAULT_FORMAT)));
return parse(value, Clocks.dateTimeFormatter(format, DATE_FORMAT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@

package io.github.selcukes.databind.converters;

import io.github.selcukes.databind.utils.Clocks;

import java.time.LocalDateTime;

import static io.github.selcukes.databind.utils.Clocks.DATE_TIME_FORMAT;
import static java.time.LocalDateTime.parse;
import static java.time.format.DateTimeFormatter.ofPattern;
import static java.util.Optional.ofNullable;

public class LocalDateTimeConverter extends DefaultConverter<LocalDateTime> {
public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

@Override
public LocalDateTime convert(final String value) {
return convert(value, DEFAULT_FORMAT);
return convert(value, DATE_TIME_FORMAT);
}

@Override
public LocalDateTime convert(final String value, final String format) {
return parse(value, ofPattern(ofNullable(format).filter(f -> !f.isEmpty()).orElse(DEFAULT_FORMAT)));
return parse(value, Clocks.dateTimeFormatter(format, DATE_TIME_FORMAT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public ExcelCell<T> parse(final Row row) {
var cellValue = ofNullable(row.getCell(index, RETURN_BLANK_AS_NULL))
.map(cell -> formatter.formatCellValue(cell).trim())
.orElse("");
var substituted = getSubstitutor().replace(cellValue);
var format = getColumn().map(Key::format).orElse("");
var substituted = getSubstitutor().replace(cellValue,format);
var converted = getConverter().convert(substituted, format);
setConvertedValue(converted);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;


/**
* The type Properties mapper.
*/
@UtilityClass
public class PropertiesMapper {
/**
Expand All @@ -48,34 +52,65 @@ public static <T> T parse(final Class<T> entityClass) {
return propertiesParser.parse(dataFile.getPath());
}

/**
* Parses the Properties file to a map.
*
* @param propertyFile the property file
* @return the map
*/
public static Map<String, String> parse(final String propertyFile) {
return Maps.of(PropertiesLoader.linkedProperties(propertyFile));
}

public static void write(String propertyFile, Map<String, String> dataMap) {
/**
* Writes a Properties file.
*
* @param propertyFile the property file
* @param dataMap the data map
*/
public static void write(final String propertyFile, Map<String, String> dataMap) {
write(propertyFile, new LinkedProperties(), dataMap);
}

private static void write(String propertyFile, LinkedProperties linkedProperties, Map<String, String> dataMap) {
private static void write(final String propertyFile, LinkedProperties linkedProperties, Map<String, String> dataMap) {
dataMap.forEach(linkedProperties::setProperty);
write(propertyFile, linkedProperties);
}

private static void write(String propertyFile, LinkedProperties properties) {
private static void write(final String propertyFile, LinkedProperties properties) {
try (OutputStream output = new FileOutputStream(propertyFile)) {
properties.store(output, null);
} catch (Exception e) {
throw new DataMapperException("Could not write property file '" + propertyFile + "'", e);
}
}

public static void updateProperty(String propertyFile, String key, String value) {
/**
* Update Property file.
*
* @param propertyFile the property file
* @param key the key
* @param value the value
*/
public static void updateProperty(final String propertyFile, String key, String value) {
LinkedProperties properties = PropertiesLoader.linkedProperties(propertyFile);
properties.setProperty(key, value);
write(propertyFile, properties);
}

public static void updateProperties(String propertyFile, Map<String, String> dataMap) {
/**
* Update properties.
*
* @param propertyFile the property file
* @param dataMap the data map
*/
public static void updateProperties(final String propertyFile, Map<String, String> dataMap) {
write(propertyFile, PropertiesLoader.linkedProperties(propertyFile), dataMap);
}

public static Properties systemProperties() {
var properties = System.getProperties();
properties.putAll(System.getenv());
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public PropertyField<T> parse() {
String keyName = getColumn()
.map(Key::name)
.orElse(getFieldName());
var substituted = getSubstitutor().replace(properties, keyName);
setConvertedValue(getConverter().convert(substituted));
var format = getColumn().map(Key::format).orElse("");
var substituted = getSubstitutor().replace(properties, keyName,format);
setConvertedValue(getConverter().convert(substituted, format));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.util.Properties;

public class DefaultSubstitutor implements Substitutor {
public String replace(Properties variables, String key) {
public String replace(Properties variables, String key,final String format) {
return variables.getProperty(key);
}

@Override
public String replace(String strToReplace) {
public String replace(String strToReplace,final String format) {
return strToReplace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,23 @@
* limitations under the License.
*/

package io.github.selcukes.databind.tests;
package io.github.selcukes.databind.substitute;

import io.github.selcukes.databind.substitute.DefaultSubstitutor;
import io.github.selcukes.databind.utils.StringHelper;

import java.time.LocalDate;
import java.util.Properties;

public class EnvPropSubstitutor extends DefaultSubstitutor {
public class StringSubstitutor extends DefaultSubstitutor {

@Override
public String replace(Properties variables, String key) {
public String replace(Properties variables, String key, final String format) {
String value = variables.getProperty(key);
return StringHelper.interpolate(value,
matcher -> matcher.group(1).equals("DATE") ?
LocalDate.now().toString() : System.getProperty(matcher.group(1))
);
return StringHelper.interpolate(value, matcher -> StringHelper.substitute(matcher.group(1), format));
}

@Override
public String replace(String strToReplace) {
return StringHelper.interpolate(strToReplace,
matcher -> matcher.group(1).equals("DATE") ?
LocalDate.now().toString() : System.getenv(matcher.group(1)));
public String replace(String strToReplace, final String format) {
return StringHelper.interpolate(strToReplace, matcher -> StringHelper.substitute(matcher.group(1), format));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
import java.util.Properties;

public interface Substitutor {
String replace(Properties variables, String key);
String replace(String strToReplace);
String replace(Properties variables, String key,final String format);
String replace(String strToReplace,final String format);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.utils;

import lombok.experimental.UtilityClass;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static java.time.format.DateTimeFormatter.ofPattern;
import static java.util.Optional.ofNullable;

@UtilityClass
public class Clocks {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

public static String date(String format) {
return LocalDate.now().format(dateTimeFormatter(format, DATE_FORMAT));
}

public static String dateTime(String format) {
return LocalDateTime.now().format(dateTimeFormatter(format, DATE_TIME_FORMAT));
}

public static DateTimeFormatter dateTimeFormatter(String format, String defaultFormat) {
return ofPattern(ofNullable(format)
.filter(f -> !f.isEmpty())
.orElse(defaultFormat));
}
}
Loading