use of io.JsonString in project Lab5 by Arslanka.
the class CommandArguments method get.
public Object[] get(ArrayList<String> data) {
Class<?>[] commandArgsClasses = command.getArgumentsClasses();
ArrayList<Object> commandArgs = new ArrayList<>();
int start = 0;
for (Class<?> arg : commandArgsClasses) {
StringBuilder argAsString = new StringBuilder();
JsonString jsonString = new JsonString();
if (command instanceof SaveCommand) {
// todo fix instanceof + open/closed
try {
commandArgs.add(new JsonFile(new TextFile(new File(data.get(0).trim()))));
break;
} catch (ClassCastException | JsonParseException | IOException ignored) {
}
} else if (command instanceof ExecuteScriptCommand) {
try {
commandArgs.add(new AdvancedScript(new TextFile(new File(data.get(0).trim())), commandsByName, stringSupplierMap, requestMap, printer));
break;
} catch (ClassCastException | JsonParseException | IOException ignored) {
}
} else {
for (; start < data.size(); ++start) {
argAsString.append(data.get(start)).append('\n');
try {
Object obj = jsonString.read(argAsString.toString(), arg);
commandArgs.add(obj);
++start;
break;
} catch (Exception ignored) {
}
try {
Object obj = (requestMap.get(arg)).apply(new Scanner(argAsString.toString()), new Printer(true));
commandArgs.add(obj);
break;
} catch (Exception ignored) {
}
}
}
}
if (commandArgs.size() != commandArgsClasses.length && !data.toString().isEmpty()) {
throw new JsonParseException("You have entered incorrect arguments in the command " + command.getName());
}
return commandArgs.toArray();
}
Aggregations