00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.commands;
00023
00024 import java.util.ArrayList;
00025 import java.util.List;
00026 import java.util.regex.Pattern;
00027 import org.jetbrains.annotations.NotNull;
00028
00034 public class CommandExpander {
00035
00039 @NotNull
00040 private static final Pattern PATTERN_SEPARATOR = Pattern.compile(" *; *");
00041
00045 @NotNull
00046 private static final Pattern PATTERN_SPACES = Pattern.compile(" +");
00047
00051 private CommandExpander() {
00052 }
00053
00061 public static List<CommandExec> expand(@NotNull final CharSequence commandList, @NotNull final Commands commands) {
00062 final List<CommandExec> list = new ArrayList<CommandExec>();
00063 CharSequence remainingCommandList = commandList;
00064 while (true) {
00065 final String[] tmp = PATTERN_SEPARATOR.split(remainingCommandList, 2);
00066 final String commandSpec = tmp[0];
00067 if (!commandSpec.isEmpty()) {
00068 final String[] tmp2 = PATTERN_SPACES.split(commandSpec, 2);
00069 final String commandName = tmp2[0];
00070 final String commandArgs = tmp2.length == 2 ? tmp2[1] : "";
00071 final Command command = commands.findCommand(commandName);
00072 if (command == null) {
00073 list.add(new CommandExec(null, commandSpec));
00074 } else if (command.allArguments()) {
00075 final String[] tmp3 = PATTERN_SPACES.split(remainingCommandList, 2);
00076 list.add(new CommandExec(command, tmp3.length == 2 ? tmp3[1] : ""));
00077 break;
00078 } else {
00079 list.add(new CommandExec(command, commandArgs));
00080 }
00081 }
00082 if (tmp.length < 2) {
00083 break;
00084 }
00085 remainingCommandList = tmp[1];
00086 }
00087 return list;
00088 }
00089
00090 }