Search in sources :

Example 1 with JsonFile

use of file.JsonFile 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 JsonFile

use of file.JsonFile 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 JsonFile (file.JsonFile)2 TextFile (file.TextFile)2 File (java.io.File)2 JsonIOException (com.google.gson.JsonIOException)1 InvalidCommandNameException (exceptions.InvalidCommandNameException)1 RecursiveCallException (exceptions.RecursiveCallException)1 AdvancedScript (execute.AdvancedScript)1 CommandInterpreter (execute.CommandInterpreter)1 JsonString (io.JsonString)1 Printer (io.Printer)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1