org.entur.netex.validation.configuration.DefaultValidationConfigLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netex-validator-java Show documentation
Show all versions of netex-validator-java Show documentation
Library for validating NeTEx datasets against the Nordic NeTEx Profile.
package org.entur.netex.validation.configuration;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
/**
* Default implementation of the validation configuration loader that loads configurations from a hierarchy of yaml files.
*/
public class DefaultValidationConfigLoader implements ValidationConfigLoader {
private static final String DEFAULT_VALIDATION_CONFIG_FILE =
"configuration.default.yaml";
private final Map validationRuleConfigs;
public DefaultValidationConfigLoader() {
this(Collections.emptyList());
}
public DefaultValidationConfigLoader(String configurationFile) {
this(List.of(configurationFile));
}
public DefaultValidationConfigLoader(List configurationFiles) {
this.validationRuleConfigs =
loadConfigurationFile(DEFAULT_VALIDATION_CONFIG_FILE);
for (String configurationFile : configurationFiles) {
this.validationRuleConfigs.putAll(
loadConfigurationFile(configurationFile)
);
}
}
private Map loadConfigurationFile(
String configurationFile
) {
InputStream inputStream = Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream(configurationFile);
Yaml yaml = new Yaml(
new Constructor(ValidationConfig.class, new LoaderOptions())
);
ValidationConfig validationConfig = yaml.load(inputStream);
return validationConfig
.getValidationRuleConfigs()
.stream()
.collect(
Collectors.toMap(ValidationRuleConfig::getCode, Function.identity())
);
}
@Override
public Map getValidationRuleConfigs() {
return validationRuleConfigs;
}
@Override
public ValidationRuleConfig getValidationRuleConfig(String ruleCode) {
return validationRuleConfigs.get(ruleCode);
}
}