use of execute.AdvancedScript 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 execute.AdvancedScript in project Lab5 by Arslanka.
the class Application method fillCommands.
public void fillCommands() {
RequestElement requestElement = new RequestElement();
InputData inputData = new InputData();
commandsByName.put("help", new HelpCommand(new Printer(false)));
supplierMap.put("help", () -> new Object[] { commandsByName });
commandsByName.put("exit", new ExitCommand(new Printer(false)));
supplierMap.put("exit", () -> new Object[] {});
commandsByName.put("info", new InfoCommand(collection, printer));
supplierMap.put("info", () -> new Object[] {});
commandsByName.put("show", new ShowCommand(collection, printer));
supplierMap.put("show", () -> new Object[] {});
commandsByName.put("add", new AddCommand(collection, printer));
supplierMap.put("add", () -> new Object[] { new RequestDragon(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("update", new UpdateIdCommand(collection, printer));
supplierMap.put("update", () -> new Object[] { collection.validatedId(requestElement.get("Enter id:", sc, printer, inputData::getId, true)), new RequestDragon(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("remove_by_id", new RemoveByIdCommand(collection, printer));
supplierMap.put("remove_by_id", () -> new Object[] { requestElement.get("Enter id:", sc, printer, inputData::getId, true) });
commandsByName.put("clear", new ClearCommand(collection, printer));
supplierMap.put("clear", () -> new Object[] {});
commandsByName.put("save", new SaveCommand(collection, printer));
supplierMap.put("save", () -> new Object[] { new FileManager(requestElement.get("Enter the file name:", sc, printer, inputData::getFileName, false), printer).getJsonFileByName() });
commandsByName.put("execute_script", new ExecuteScriptCommand());
supplierMap.put("execute_script", () -> new Object[] { new AdvancedScript(new FileManager(requestElement.get("Enter the name of the script:", sc, printer, inputData::getFileName, false), printer).getTextFileByName(), commandsByName, supplierMap, requestMap, printer) });
commandsByName.put("add_if_max", new AddIfMaxCommand(collection, printer));
supplierMap.put("add_if_max", () -> new Object[] { new RequestDragon(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("remove_greater", new RemoveGreaterCommand(collection, printer));
supplierMap.put("remove_greater", () -> new Object[] { new RequestDragon(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("remove_lower", new RemoveLowerCommand(collection, printer));
supplierMap.put("remove_lower", () -> new Object[] { new RequestDragon(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("remove_all_by_weight", new RemoveAllByWeight(collection, printer));
supplierMap.put("remove_all_by_weight", () -> new Object[] { requestElement.get("Enter the weight:", sc, printer, inputData::getWeight, true) });
commandsByName.put("count_greater_than_killer", new CountGreaterThanKillerCommand(collection, printer));
supplierMap.put("count_greater_than_killer", () -> new Object[] { new RequestPerson(requestElement, sc, printer, inputData).get().build() });
commandsByName.put("filter_greater_than_age", new FilterGreaterThanAgeCommand(collection, printer));
supplierMap.put("filter_greater_than_age", () -> new Object[] { requestElement.get("Enter the age:", sc, printer, inputData::getAge, true) });
requestMap.put(Dragon.class, (Scanner scanner, Printer printer) -> new RequestDragon(requestElement, scanner, printer, inputData).get().build());
requestMap.put(Person.class, (Scanner scanner, Printer printer) -> new RequestPerson(requestElement, scanner, printer, inputData).get().build());
requestMap.put(Coordinates.class, (Scanner scanner, Printer printer) -> new RequestCoordinates(requestElement, scanner, printer, inputData).get().build());
requestMap.put(Location.class, (Scanner scanner, Printer printer) -> new RequestLocation(requestElement, scanner, printer, inputData).get().build());
}
Aggregations