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