Search in sources :

Example 1 with RecursiveCallException

use of exceptions.RecursiveCallException 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());
    }
}
Also used : JsonParseException(com.google.gson.JsonParseException) java.util(java.util) java.io(java.io) Printer(io.Printer) BiFunction(java.util.function.BiFunction) ExecutionException(exceptions.ExecutionException) InputOutputException(exceptions.InputOutputException) Supplier(java.util.function.Supplier) commands(commands) RecursiveCallException(exceptions.RecursiveCallException) TextFile(file.TextFile) InputOutputException(exceptions.InputOutputException) RecursiveCallException(exceptions.RecursiveCallException) JsonParseException(com.google.gson.JsonParseException) ExecutionException(exceptions.ExecutionException)

Example 2 with RecursiveCallException

use of exceptions.RecursiveCallException in project Lab5 by Arslanka.

the class Application method startInteractiveMode.

public void startInteractiveMode() {
    try {
        String fileName = this.fileName[0];
        final File file = new File(fileName.trim());
        final TextFile textFile = new TextFile(file);
        final JsonFile jsonFile = new JsonFile(textFile);
        collection.add(jsonFile.read());
        CommandInterpreter commandInterpreter = new CommandInterpreter(this.commandsByName, supplierMap, this.printer, requestMap);
        boolean running = true;
        while (running) {
            printer.println(("Enter the command:"), REQUEST);
            try {
                running = commandInterpreter.run(new ArrayList<>(Arrays.asList(sc.nextLine().trim().split("\\s+"))));
            } catch (NoSuchElementException e) {
                running = false;
            } catch (RecursiveCallException | InvalidCommandNameException | IllegalArgumentException | JsonParseException e) {
                printer.println(e.getMessage(), ERROR);
            }
        }
        printer.println("Program execution stopped", CONSOLE);
    } catch (NoSuchElementException e) {
        printer.println("The program ended incorrectly. Please restart the program.", ERROR);
    } catch (ArrayIndexOutOfBoundsException e) {
        printer.println("You didn't specify the file name to populate the collection", ERROR);
    } catch (Exception e) {
        printer.println(e.getMessage(), ERROR);
    }
}
Also used : InvalidCommandNameException(exceptions.InvalidCommandNameException) TextFile(file.TextFile) RecursiveCallException(exceptions.RecursiveCallException) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) FileNotFoundException(java.io.FileNotFoundException) InvalidCommandNameException(exceptions.InvalidCommandNameException) RecursiveCallException(exceptions.RecursiveCallException) JsonFile(file.JsonFile) File(java.io.File) TextFile(file.TextFile) JsonFile(file.JsonFile) CommandInterpreter(execute.CommandInterpreter)

Aggregations

JsonParseException (com.google.gson.JsonParseException)2 RecursiveCallException (exceptions.RecursiveCallException)2 TextFile (file.TextFile)2 commands (commands)1 ExecutionException (exceptions.ExecutionException)1 InputOutputException (exceptions.InputOutputException)1 InvalidCommandNameException (exceptions.InvalidCommandNameException)1 CommandInterpreter (execute.CommandInterpreter)1 JsonFile (file.JsonFile)1 Printer (io.Printer)1 java.io (java.io)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 java.util (java.util)1 BiFunction (java.util.function.BiFunction)1 Supplier (java.util.function.Supplier)1