Search in sources :

Example 1 with GogoParser

use of org.apache.karaf.shell.support.parsing.GogoParser in project karaf by apache.

the class CommandLineParser method parse.

public static String parse(Session session, String command) {
    StringBuilder parsed = new StringBuilder();
    int pos = 0;
    while (pos < command.length()) {
        String rem = command.substring(pos);
        GogoParser cmdNameParser = new GogoParser(rem, rem.length());
        String name = cmdNameParser.value();
        name = session.resolveCommand(name);
        Parser cmdParser = null;
        for (Command cmd : session.getRegistry().getCommands()) {
            if (name.equals(cmd.getScope() + ":" + cmd.getName())) {
                cmdParser = cmd.getParser();
                break;
            }
        }
        if (cmdParser == null) {
            cmdParser = new DefaultParser();
        }
        CommandLine cmdLine = cmdParser.parse(session, rem, rem.length());
        parsed.append(cmdParser.preprocess(session, cmdLine));
        int length = cmdLine.getBuffer().length();
        if (length < rem.length()) {
            char ch = rem.charAt(length);
            if (ch == ';' || ch == '|') {
                parsed.append(" ");
                parsed.append(ch);
                parsed.append(" ");
                length++;
            } else if (ch == '\n') {
                parsed.append(ch);
                length++;
            } else {
                throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
            }
        }
        pos += length;
    }
    return parsed.toString();
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) Parser(org.apache.karaf.shell.api.console.Parser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser)

Example 2 with GogoParser

use of org.apache.karaf.shell.support.parsing.GogoParser in project karaf by apache.

the class JdbcParser method parse.

@Override
public CommandLine parse(Session session, String command, int cursor) {
    GogoParser parser = new GogoParser(command, cursor);
    List<String> args = parser.statement();
    List<String> nargs = new ArrayList<>();
    int state = 0;
    for (String arg : args) {
        switch(state) {
            // command
            case 0:
                nargs.add(arg);
                state = 1;
                break;
            // option or target
            case 1:
                nargs.add(arg);
                if (!arg.startsWith("-")) {
                    state = 2;
                }
                break;
            // first sql fragment
            case 2:
                nargs.add(arg);
                state = 3;
                break;
            // addtional sql
            case 3:
                nargs.set(nargs.size() - 1, nargs.get(nargs.size() - 1) + " " + arg);
                break;
        }
    }
    nargs.set(nargs.size() - 1, "\"" + nargs.get(nargs.size() - 1) + "\"");
    return new CommandLineImpl(nargs.toArray(new String[nargs.size()]), parser.cursorArgumentIndex(), Math.max(parser.argumentPosition(), nargs.size()), cursor, command.substring(0, parser.position()));
}
Also used : GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) ArrayList(java.util.ArrayList) CommandLineImpl(org.apache.karaf.shell.support.parsing.CommandLineImpl)

Example 3 with GogoParser

use of org.apache.karaf.shell.support.parsing.GogoParser in project karaf by apache.

the class CommandLineParser method buildCommandLine.

public static CommandLine buildCommandLine(Session session, final String command, int cursor, AtomicInteger begOfLine) {
    int pos = 0;
    while (true) {
        String rem = command.substring(pos);
        GogoParser cmdNameParser = new GogoParser(rem, rem.length());
        String name = cmdNameParser.value();
        name = session.resolveCommand(name);
        Parser cmdParser = null;
        for (Command cmd : session.getRegistry().getCommands()) {
            if (name.equals(cmd.getScope() + ":" + cmd.getName())) {
                cmdParser = cmd.getParser();
                break;
            }
        }
        if (cmdParser == null) {
            cmdParser = new DefaultParser();
        }
        CommandLine cmdLine = cmdParser.parse(session, rem, cursor - pos);
        int length = cmdLine.getBuffer().length();
        if (length < rem.length()) {
            char ch = rem.charAt(length);
            if (ch == ';' || ch == '|') {
                length++;
            } else {
                throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
            }
        }
        pos += length;
        if (cursor <= pos) {
            begOfLine.set(pos - length);
            return cmdLine;
        }
    }
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) Parser(org.apache.karaf.shell.api.console.Parser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser)

Aggregations

GogoParser (org.apache.karaf.shell.support.parsing.GogoParser)3 Command (org.apache.karaf.shell.api.console.Command)2 CommandLine (org.apache.karaf.shell.api.console.CommandLine)2 Parser (org.apache.karaf.shell.api.console.Parser)2 DefaultParser (org.apache.karaf.shell.support.parsing.DefaultParser)2 ArrayList (java.util.ArrayList)1 CommandLineImpl (org.apache.karaf.shell.support.parsing.CommandLineImpl)1