Search in sources :

Example 1 with Parser

use of org.apache.karaf.shell.api.console.Parser 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 Parser

use of org.apache.karaf.shell.api.console.Parser 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)

Example 3 with Parser

use of org.apache.karaf.shell.api.console.Parser in project karaf by apache.

the class KarafParser method doParse.

private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
    org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
    Program program = parser.program();
    List<Statement> statements = parser.statements();
    // Find corresponding statement
    Statement statement = null;
    for (int i = statements.size() - 1; i >= 0; i--) {
        Statement s = statements.get(i);
        if (s.start() <= cursor) {
            boolean isOk = true;
            // check if there are only spaces after the previous statement
            if (s.start() + s.length() < cursor) {
                for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
                    isOk = Character.isWhitespace(line.charAt(j));
                }
            }
            statement = s;
            break;
        }
    }
    if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
        String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
        String[] parts = cmdName.split(":");
        Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
        Parser cmdParser = cmd != null ? cmd.getParser() : null;
        if (cmdParser != null) {
            final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
            return new ParsedLine() {

                @Override
                public String word() {
                    return cmdLine.getCursorArgument();
                }

                @Override
                public int wordCursor() {
                    return cmdLine.getArgumentPosition();
                }

                @Override
                public int wordIndex() {
                    return cmdLine.getCursorArgumentIndex();
                }

                @Override
                public List<String> words() {
                    return Arrays.asList(cmdLine.getArguments());
                }

                @Override
                public String line() {
                    return cmdLine.getBuffer();
                }

                @Override
                public int cursor() {
                    return cmdLine.getBufferPosition();
                }
            };
        }
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine)

Example 4 with Parser

use of org.apache.karaf.shell.api.console.Parser in project karaf by apache.

the class CommandTracker method addingService.

@Override
public Object addingService(final ServiceReference reference) {
    Object service = context.getService(reference);
    if (service instanceof CommandWithAction) {
        final CommandWithAction oldCommand = (CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final Command cmd = oldCommand.getActionClass().getAnnotation(Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else if (service instanceof org.apache.felix.gogo.commands.CommandWithAction) {
        final org.apache.felix.gogo.commands.CommandWithAction oldCommand = (org.apache.felix.gogo.commands.CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final org.apache.felix.gogo.commands.Command cmd = oldCommand.getActionClass().getAnnotation(org.apache.felix.gogo.commands.Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else {
        return null;
    }
}
Also used : List(java.util.List) Completer(org.apache.karaf.shell.api.console.Completer) Method(java.lang.reflect.Method) CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Parser(org.apache.karaf.shell.api.console.Parser) CommandSession(org.apache.felix.service.command.CommandSession) Command(org.apache.karaf.shell.commands.Command) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Aggregations

Parser (org.apache.karaf.shell.api.console.Parser)4 Command (org.apache.karaf.shell.api.console.Command)3 CommandLine (org.apache.karaf.shell.api.console.CommandLine)3 DefaultParser (org.apache.karaf.shell.support.parsing.DefaultParser)2 GogoParser (org.apache.karaf.shell.support.parsing.GogoParser)2 Method (java.lang.reflect.Method)1 List (java.util.List)1 ParsedLineImpl (org.apache.felix.gogo.jline.ParsedLineImpl)1 Program (org.apache.felix.gogo.runtime.Parser.Program)1 Statement (org.apache.felix.gogo.runtime.Parser.Statement)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Completer (org.apache.karaf.shell.api.console.Completer)1 Session (org.apache.karaf.shell.api.console.Session)1 Command (org.apache.karaf.shell.commands.Command)1 CommandWithAction (org.apache.karaf.shell.commands.CommandWithAction)1 ParsedLine (org.jline.reader.ParsedLine)1