@Override
protected void doHandle(CommandContext ctx) throws CommandFormatException {
BatchManager batchManager = ctx.getBatchManager();
if(!batchManager.isBatchActive()) {
throw new CommandFormatException("No active batch.");
}
Batch batch = batchManager.getActiveBatch();
final int batchSize = batch.size();
if(batchSize == 0) {
throw new CommandFormatException("The batch is empty.");
}
String argsStr = ctx.getArgumentsString();
if(argsStr == null) {
throw new CommandFormatException("Missing line number.");
}
int i = 0;
while(i < argsStr.length()) {
if(Character.isWhitespace(argsStr.charAt(i))) {
break;
}
++i;
}
if(i == argsStr.length()) {
throw new CommandFormatException("Missing the new command line after the index.");
}
String intStr = argsStr.substring(0, i);
int lineNumber;
try {
lineNumber = Integer.parseInt(intStr);
} catch(NumberFormatException e) {
throw new CommandFormatException("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
}
if(lineNumber < 1 || lineNumber > batchSize) {
throw new CommandFormatException(lineNumber + " isn't in range [1.." + batchSize + "].");
}
String editedLine = argsStr.substring(i).trim();
if(editedLine.length() == 0) {
throw new CommandFormatException("Missing the new command line after the index.");
}
if(editedLine.charAt(0) == '"') {
if(editedLine.length() > 1 && editedLine.charAt(editedLine.length() - 1) == '"') {
editedLine = editedLine.substring(1, editedLine.length() - 1);