Search in sources :

Example 1 with Printer

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();
}
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 Printer

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());
    }
}
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 3 with Printer

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();
}
Also used : Collection(collection.Collection) Printer(io.Printer) Application(io.Application)

Aggregations

Printer (io.Printer)3 JsonParseException (com.google.gson.JsonParseException)2 TextFile (file.TextFile)2 Collection (collection.Collection)1 JsonIOException (com.google.gson.JsonIOException)1 commands (commands)1 ExecutionException (exceptions.ExecutionException)1 InputOutputException (exceptions.InputOutputException)1 RecursiveCallException (exceptions.RecursiveCallException)1 AdvancedScript (execute.AdvancedScript)1 JsonFile (file.JsonFile)1 Application (io.Application)1 JsonString (io.JsonString)1 java.io (java.io)1 File (java.io.File)1 IOException (java.io.IOException)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1 BiFunction (java.util.function.BiFunction)1