Search in sources :

Example 1 with AdvancedScript

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();
}
Also used : Scanner(java.util.Scanner) ArrayList(java.util.ArrayList) TextFile(file.TextFile) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) JsonParseException(com.google.gson.JsonParseException) Printer(io.Printer) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) AdvancedScript(execute.AdvancedScript) JsonFile(file.JsonFile) JsonString(io.JsonString) File(java.io.File) TextFile(file.TextFile) JsonFile(file.JsonFile)

Example 2 with AdvancedScript

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());
}
Also used : FileManager(file.FileManager) AdvancedScript(execute.AdvancedScript)

Aggregations

AdvancedScript (execute.AdvancedScript)2 JsonIOException (com.google.gson.JsonIOException)1 JsonParseException (com.google.gson.JsonParseException)1 FileManager (file.FileManager)1 JsonFile (file.JsonFile)1 TextFile (file.TextFile)1 JsonString (io.JsonString)1 Printer (io.Printer)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1