Add linux compatibility & Finalize Config Loader

This commit is contained in:
2023-02-19 15:51:50 +01:00
parent f69fbedee0
commit ce9a2fc805
29 changed files with 212 additions and 71 deletions

View File

@@ -18,6 +18,9 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
super(ConfigProperty.ATTRIBUTE_CONFIG, true, null);
}
/**
* {@inheritDoc}
*/
public ValidationResult validate(YAML yaml, CommandLine cmd) {
System.out.printf("%s: ", property.prop());
List<AttributeConfig> result;
@@ -48,11 +51,17 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
return ValidationResult.VALID;
}
/**
* {@inheritDoc}
*/
@Override
List<AttributeConfig> parse(String value) {
return null;
}
/**
* {@inheritDoc}
*/
@Override
boolean isValid(List<AttributeConfig> result) {
if (result.isEmpty()) {

View File

@@ -18,6 +18,9 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
super(property, required, null);
}
/**
* {@inheritDoc}
*/
protected BiFunction<YAML, ConfigProperty, Optional<Boolean>> provideDataYaml() {
return (yaml, property) -> {
if (yaml.isSet(ARGUMENTS.prop())
@@ -28,6 +31,9 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
};
}
/**
* {@inheritDoc}
*/
protected BiFunction<CommandLine, ConfigProperty, Optional<Boolean>> provideDataCmd() {
return (cmd, property) -> {
if (cmd.hasOption(property.prop())) {
@@ -37,11 +43,19 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
};
}
/**
* {@inheritDoc}
* This should not be used.
*/
@Override
Boolean parse(String value) {
throw new RuntimeException("This should not be called");
}
/**
* {@inheritDoc}
* Validation is skipped.
*/
@Override
boolean isValid(Boolean result) {
return true; // skip

View File

@@ -12,11 +12,17 @@ public class ConfigPathValidator extends PathValidator {
super(property, required, defaultValue);
}
/**
* {@inheritDoc}
*/
@Override
protected BiFunction<YAML, ConfigProperty, Optional<File>> provideDataYaml() {
return (yaml, property) -> Optional.empty();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isValid(File result) {
return super.isValid(result) && (result.getAbsolutePath().endsWith(".yml") || result.getAbsolutePath().endsWith(".yaml"));

View File

@@ -24,6 +24,13 @@ public abstract class ConfigValidator<FieldType> {
protected final boolean required;
protected final FieldType defaultValue;
/**
* Validate the user input. Parameters of cmd are prioritised.
*
* @param yaml config file
* @param cmd command line parameters
* @return {@link ValidationResult} containing validity of input.
*/
public ValidationResult validate(YAML yaml, CommandLine cmd) {
System.out.printf("%s: ", property.prop());
FieldType result;
@@ -63,6 +70,9 @@ public abstract class ConfigValidator<FieldType> {
return ValidationResult.VALID;
}
/**
* @return parsed input of yaml config for property
*/
protected BiFunction<YAML, ConfigProperty, Optional<FieldType>> provideDataYaml() {
return (yaml, property) -> {
if (yaml.isSet(property.prop())) {
@@ -76,6 +86,9 @@ public abstract class ConfigValidator<FieldType> {
};
}
/**
* @return parsed input of command line parameters config for property
*/
protected BiFunction<CommandLine, ConfigProperty, Optional<FieldType>> provideDataCmd() {
return (cmd, property) -> {
if (cmd.hasOption(property.prop())) {
@@ -85,10 +98,28 @@ public abstract class ConfigValidator<FieldType> {
};
}
/**
* Parse input parameter to desired format.
*
* @param value input parameter
* @return parsed property
*/
abstract FieldType parse(String value);
/**
* Validate if the data has the desired and allowed format.
*
* @param result parsed property
* @return true if data is in desired format.
*/
abstract boolean isValid(FieldType result);
/**
* Sets valid properties to {@link Config} via reflections.
*
* @param result parsed property
* @return false if method invocation failed
*/
protected boolean setValue(FieldType result) {
List<Method> methods = Arrays.stream(Config.getInstance().getClass().getDeclaredMethods())
.filter(containsSetterOf(property))

View File

@@ -9,6 +9,8 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_MER
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_PROP_EDIT;
public class MkvToolNixPathValidator extends PathValidator {
private static final String EXE = ".exe";
public MkvToolNixPathValidator(ConfigProperty property, boolean required, File defaultValue) {
super(property, required, defaultValue);
}
@@ -16,8 +18,9 @@ public class MkvToolNixPathValidator extends PathValidator {
@Override
protected boolean isValid(File result) {
return result.isDirectory()
&& Path.of(result.getAbsolutePath() + "/" + MKV_MERGER + ".exe").toFile().isFile()
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT+ ".exe").toFile().isFile();
// TODO: make linux compatible
&& (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER + EXE).toFile().isFile()
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT + EXE).toFile().isFile())
|| (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER).toFile().isFile()
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT).toFile().isFile());
}
}

View File

@@ -12,6 +12,9 @@ public class OperatingSystemValidator extends BooleanValidator {
super(property, false);
}
/**
* Gather operating system from system properties.
*/
@Override
public ValidationResult validate(YAML yaml, CommandLine cmd) {
System.out.printf("%s: ", property.prop());

View File

@@ -11,11 +11,17 @@ public class PathValidator extends ConfigValidator<File> {
super(property, required, defaultValue);
}
/**
* {@inheritDoc}
*/
@Override
protected File parse(String value) {
return Path.of(value).toFile();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isValid(File result) {
return result.isDirectory() || result.isFile();

View File

@@ -12,6 +12,9 @@ public class PatternValidator extends ConfigValidator<Pattern> {
super(property, required, defaultValue);
}
/**
* {@inheritDoc}
*/
@Override
Pattern parse(String value) {
try {
@@ -21,6 +24,9 @@ public class PatternValidator extends ConfigValidator<Pattern> {
}
}
/**
* {@inheritDoc}
*/
@Override
boolean isValid(Pattern result) {
return !result.equals(EMPTY_PATTERN);

View File

@@ -35,7 +35,7 @@ public class SetValidator extends ConfigValidator<Set<String>> {
System.out.println("missing");
return ValidationResult.MISSING;
} else {
System.out.println("not present");
System.out.println("ok");
return ValidationResult.NOT_PRESENT;
}

View File

@@ -8,11 +8,17 @@ public class ThreadValidator extends ConfigValidator<Integer>{
super(property, required, defaultValue);
}
/**
* {@inheritDoc}
*/
@Override
Integer parse(String value) {
return NumberUtils.isParsable(value) ? Integer.parseInt(value) : defaultValue;
}
/**
* {@inheritDoc}
*/
@Override
boolean isValid(Integer result) {
return result > 0;