use of io.Printer 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();
}
use of io.Printer in project Lab5 by Arslanka.
the class AdvancedScript method execute.
public boolean execute() {
try {
if (executableScripts.contains(textFile.getFile())) {
executableScripts.clear();
throw new RecursiveCallException(textFile.getFile().toString());
}
executableScripts.add(textFile.getFile());
ArrayList<String> stringRep = new ArrayList<>();
try {
Arrays.stream(textFile.read().split("\\s+")).filter(s -> !s.isEmpty()).forEach(stringRep::add);
} catch (InputOutputException e) {
executableScripts.remove(textFile.getFile());
throw new ExecutionException(e.getMessage());
}
ArrayList<String> data = new ArrayList<>();
String lastCommand = "";
CommandInterpreter commandInterpreter = new CommandInterpreter(commandMap, supplierMap, printer, requestMap);
boolean isFirst = true;
for (String s : stringRep) {
if (!noExit) {
executableScripts.remove(this.textFile.getFile());
return false;
}
if (!commandMap.containsKey(s)) {
data.add(s);
continue;
}
if (isFirst) {
if (!data.isEmpty()) {
executableScripts.remove(this.textFile.getFile());
throw new ExecutionException("You have entered incorrect data for the script");
}
lastCommand = s;
isFirst = false;
continue;
}
interpreter(data, lastCommand, commandInterpreter);
data.clear();
lastCommand = s;
}
if (!noExit) {
executableScripts.remove(this.textFile.getFile());
return false;
} else if (lastCommand.isEmpty()) {
executableScripts.remove(this.textFile.getFile());
throw new ExecutionException("The command name was entered incorrectly");
}
interpreter(data, lastCommand, commandInterpreter);
executableScripts.remove(this.textFile.getFile());
return true;
} catch (NullPointerException e) {
executableScripts.remove(this.textFile.getFile());
throw new ExecutionException("You have entered incorrect data for the script");
} catch (JsonParseException | InputOutputException e) {
executableScripts.remove(this.textFile.getFile());
throw new ExecutionException("You have entered incorrect data for the script\n" + e.getMessage());
}
}
use of io.Printer in project Lab5 by Arslanka.
the class Main method main.
public static void main(String[] args) {
// TODO args input + try catch nlpex/c
Application application = new Application(args, new Collection(), new Printer(false));
application.startInteractiveMode();
}
Aggregations