Splits the parse function into more readable functions

This commit is contained in:
Kristian Knarvik 2021-01-21 13:48:55 +01:00
parent 8b0e394ac8
commit 6eec3d7969

View File

@ -1,10 +1,8 @@
package net.knarcraft.ffmpegconverter.utility;
import net.knarcraft.ffmpegconverter.parser.ConverterArgument;
import net.knarcraft.ffmpegconverter.parser.ConverterArgumentValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -18,86 +16,96 @@ public final class Parser {
}
/**
* This function parses command inputs into understandable converter instructions
* @param tokens <p>A list of tokens containing all arguments</p>
* This function parses the input to understandable converter instructions
* @param input <p>The input string to parse.</p>
* @param validArguments <p>All arguments which are considered valid.</p>
* @return <p>A map with all parsed arguments.</p>
*/
private static Map<String, String> parse(List<String> tokens) {
String[] types = {"animeconverter", "audioconverter", "videoconverter"};
ConverterArgument[] commonArgs = {
new ConverterArgument("recursions", 'r', true, ConverterArgumentValue.INT),
new ConverterArgument("infile", 'i', true, ConverterArgumentValue.STRING)
};
ConverterArgument[] animeArgs = {
new ConverterArgument("audiolang", 'a', true, ConverterArgumentValue.COMMA_SEPARATED_LIST),
new ConverterArgument("subtitlelang", 's', true, ConverterArgumentValue.COMMA_SEPARATED_LIST),
new ConverterArgument("tostereo", 't', false, ConverterArgumentValue.BOOLEAN),
new ConverterArgument("preventpartialsubtitles", 'p', false, ConverterArgumentValue.BOOLEAN)
};
ConverterArgument[] audioVideoArgs = {
new ConverterArgument("-outext", 'o', true,
ConverterArgumentValue.STRING)
};
String type = tokens.remove(0).toLowerCase();
if (!ListUtil.listContains(types, s -> s.equals(type))) {
throw new IllegalArgumentException("Unknown converter type chosen.");
}
if (tokens.size() < 2) {
throw new IllegalArgumentException("No file/folder path in argument.");
}
List<ConverterArgument> converterArguments;
switch (type) {
case "animeconverter":
converterArguments = new ArrayList<>(Arrays.asList(ListUtil.concatenate(commonArgs, animeArgs)));
break;
case "audioconverter":
case "videoconverter":
converterArguments = new ArrayList<>(Arrays.asList(ListUtil.concatenate(commonArgs, audioVideoArgs)));
break;
default:
throw new IllegalArgumentException("Unknown converter type chosen.");
}
static Map<String, String> parse(String input, List<ConverterArgument> validArguments) {
return parse(tokenize(input), validArguments);
}
/**
* This function parses command inputs into understandable converter instructions
*
* @param tokens <p>A list of tokens containing all arguments</p>
* @param validArguments <p>A list of arguments which are considered valid.</p>
* @return <p>A map with all parsed arguments.</p>
*/
private static Map<String, String> parse(List<String> tokens, List<ConverterArgument> validArguments) {
Map<String, String> parsedArguments = new HashMap<>();
//TODO: Refactor and test this function
while (!tokens.isEmpty()) {
String currentToken = tokens.remove(0);
List<ConverterArgument> foundArguments;
if (currentToken.startsWith("--")) {
String argumentName = currentToken.substring(2);
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName));
} else if (currentToken.startsWith("-")) {
char argumentShorthand = currentToken.substring(1).charAt(0);
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getShorthand() == argumentShorthand);
} else {
throw new IllegalArgumentException("Unexpected value when not given an argument.");
}
if (foundArguments.isEmpty()) {
throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken));
}
ConverterArgument foundArgument = foundArguments.get(0);
String argumentValue = tokens.get(0);
boolean valueIsArgument = argumentValue.startsWith("-");
if (valueIsArgument) {
if (foundArgument.isValueRequired()) {
throw new IllegalArgumentException(String.format("Argument %s requires a value, but no value was given.", foundArgument.getName()));
} else {
parsedArguments.put(foundArgument.getName(), "true");
}
} else {
String value = tokens.remove(0);
if (!foundArgument.testArgumentValue(value)) {
throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName()));
}
parsedArguments.put(foundArgument.getName(), value);
}
parseArgument(tokens, validArguments, parsedArguments);
}
return parsedArguments;
}
/**
* Parses the first found token as an argument
*
* @param tokens <p>The tokens to parse.</p>
* @param converterArguments <p>A list of all the valid arguments in existence.</p>
* @param parsedArguments <p>The map to store the parsed argument to.</p>
*/
private static void parseArgument(List<String> tokens, List<ConverterArgument> converterArguments, Map<String, String> parsedArguments) {
String currentToken = tokens.remove(0);
List<ConverterArgument> foundArguments;
if (currentToken.startsWith("--")) {
String argumentName = currentToken.substring(2);
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName));
} else if (currentToken.startsWith("-")) {
char argumentShorthand = currentToken.substring(1).charAt(0);
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getShorthand() == argumentShorthand);
} else {
throw new IllegalArgumentException("Unexpected value when not given an argument.");
}
if (foundArguments.isEmpty()) {
throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken));
}
ConverterArgument foundArgument = foundArguments.get(0);
storeArgumentValue(tokens, foundArgument, parsedArguments);
}
/**
* Stores the value of a found argument to parsed arguments
*
* @param tokens <p>The token list to parse.</p>
* @param foundArgument <p>The found argument to store.</p>
* @param parsedArguments <p>The map to store parsed arguments to.</p>
*/
private static void storeArgumentValue(List<String> tokens, ConverterArgument foundArgument, Map<String, String> parsedArguments) {
String argumentValue;
if (tokens.isEmpty()) {
argumentValue = "";
} else {
argumentValue = tokens.get(0);
}
boolean valueIsArgument = argumentValue.startsWith("-");
if (valueIsArgument) {
if (foundArgument.isValueRequired()) {
throw new IllegalArgumentException(String.format("Argument %s requires a value, but no value was given.", foundArgument.getName()));
} else {
parsedArguments.put(foundArgument.getName(), "true");
}
} else {
String value;
if (tokens.isEmpty()) {
value = "";
} else {
value = tokens.remove(0);
}
if (!foundArgument.testArgumentValue(value)) {
throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName()));
}
parsedArguments.put(foundArgument.getName(), value);
}
}
/**
* Tokenizes a string
*