Crossfire JXClient, Trunk  R20561
CommandExpander.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.commands;
23 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.regex.Pattern;
27 import org.jetbrains.annotations.NotNull;
28 
34 public class CommandExpander {
35 
39  @NotNull
40  private static final Pattern PATTERN_SEPARATOR = Pattern.compile(" *; *");
41 
45  @NotNull
46  private static final Pattern PATTERN_SPACES = Pattern.compile(" +");
47 
51  private CommandExpander() {
52  }
53 
61  public static Collection<CommandExec> expand(@NotNull final CharSequence commandList, @NotNull final Commands commands) {
62  final Collection<CommandExec> list = new ArrayList<>();
63  CharSequence remainingCommandList = commandList;
64  while (true) {
65  final String[] tmp = PATTERN_SEPARATOR.split(remainingCommandList, 2);
66  final String commandSpec = tmp[0];
67  if (!commandSpec.isEmpty()) {
68  final String[] tmp2 = PATTERN_SPACES.split(commandSpec, 2);
69  final String commandName = tmp2[0];
70  final String commandArgs = tmp2.length == 2 ? tmp2[1] : "";
71  final Command command = commands.findCommand(commandName);
72  if (command == null) {
73  list.add(new CommandExec(null, commandSpec));
74  } else if (command.allArguments()) {
75  final String[] tmp3 = PATTERN_SPACES.split(remainingCommandList, 2);
76  list.add(new CommandExec(command, tmp3.length == 2 ? tmp3[1] : ""));
77  break;
78  } else {
79  list.add(new CommandExec(command, commandArgs));
80  }
81  }
82  if (tmp.length < 2) {
83  break;
84  }
85  remainingCommandList = tmp[1];
86  }
87  return list;
88  }
89 
90 }
Implements a client-side command.
Definition: Command.java:30
Expands a command (or list of commands) into a sequence of Commands to execute.
A Command instance and its arguments.
static Collection< CommandExec > expand(@NotNull final CharSequence commandList, @NotNull final Commands commands)
Expands a command list into a sequence of Commands to execute.
CommandExpander()
Private constructor to prevent instantiation.
boolean allArguments()
Returns whether all remaining commands should be included as arguments.
static final Pattern PATTERN_SPACES
The Pattern for splitting commands from command arguments.
static final Pattern PATTERN_SEPARATOR
The Pattern for splitting command sequences.
Parses and executes client-side commands.
Definition: Commands.java:33