Search in sources :

Example 1 with JsonString

use of io.JsonString 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)

Aggregations

JsonIOException (com.google.gson.JsonIOException)1 JsonParseException (com.google.gson.JsonParseException)1 AdvancedScript (execute.AdvancedScript)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