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; package net.knarcraft.ffmpegconverter.utility;
import net.knarcraft.ffmpegconverter.parser.ConverterArgument; import net.knarcraft.ffmpegconverter.parser.ConverterArgument;
import net.knarcraft.ffmpegconverter.parser.ConverterArgumentValue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,53 +16,42 @@ public final class Parser {
} }
/** /**
* This function parses command inputs into understandable converter instructions * This function parses the input to understandable converter instructions
* @param tokens <p>A list of tokens containing all arguments</p> * @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> * @return <p>A map with all parsed arguments.</p>
*/ */
private static Map<String, String> parse(List<String> tokens) { static Map<String, String> parse(String input, List<ConverterArgument> validArguments) {
String[] types = {"animeconverter", "audioconverter", "videoconverter"}; return parse(tokenize(input), validArguments);
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.");
} }
/**
* 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<>(); Map<String, String> parsedArguments = new HashMap<>();
//TODO: Refactor and test this function
while (!tokens.isEmpty()) { while (!tokens.isEmpty()) {
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); String currentToken = tokens.remove(0);
List<ConverterArgument> foundArguments; List<ConverterArgument> foundArguments;
if (currentToken.startsWith("--")) { if (currentToken.startsWith("--")) {
String argumentName = currentToken.substring(2); String argumentName = currentToken.substring(2);
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName)); foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName));
@ -74,11 +61,29 @@ public final class Parser {
} else { } else {
throw new IllegalArgumentException("Unexpected value when not given an argument."); throw new IllegalArgumentException("Unexpected value when not given an argument.");
} }
if (foundArguments.isEmpty()) { if (foundArguments.isEmpty()) {
throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken)); throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken));
} }
ConverterArgument foundArgument = foundArguments.get(0); ConverterArgument foundArgument = foundArguments.get(0);
String argumentValue = tokens.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("-"); boolean valueIsArgument = argumentValue.startsWith("-");
if (valueIsArgument) { if (valueIsArgument) {
@ -88,15 +93,18 @@ public final class Parser {
parsedArguments.put(foundArgument.getName(), "true"); parsedArguments.put(foundArgument.getName(), "true");
} }
} else { } else {
String value = tokens.remove(0); String value;
if (tokens.isEmpty()) {
value = "";
} else {
value = tokens.remove(0);
}
if (!foundArgument.testArgumentValue(value)) { if (!foundArgument.testArgumentValue(value)) {
throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName())); throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName()));
} }
parsedArguments.put(foundArgument.getName(), value); parsedArguments.put(foundArgument.getName(), value);
} }
} }
return parsedArguments;
}
/** /**
* Tokenizes a string * Tokenizes a string