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