Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.commands;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.regex.Pattern;
28 import org.jetbrains.annotations.NotNull;
29 
35 public class CommandExpander {
36 
40  @NotNull
41  private static final Pattern PATTERN_SEPARATOR = Pattern.compile(" *; *");
42 
46  @NotNull
47  private static final Pattern PATTERN_SPACES = Pattern.compile(" +");
48 
52  private CommandExpander() {
53  }
54 
62  @NotNull
63  public static Collection<CommandExec> expand(@NotNull final CharSequence commandList, @NotNull final Commands commands) {
64  final Collection<CommandExec> list = new ArrayList<>();
65  CharSequence remainingCommandList = commandList;
66  while (true) {
67  final String[] tmp = PATTERN_SEPARATOR.split(remainingCommandList, 2);
68  final String commandSpec = tmp[0];
69  if (!commandSpec.isEmpty()) {
70  final String[] tmp2 = PATTERN_SPACES.split(commandSpec, 2);
71  final String commandName = tmp2[0];
72  final String commandArgs = tmp2.length == 2 ? tmp2[1] : "";
73  final Command command = commands.findCommand(commandName);
74  if (command == null) {
75  list.add(new CommandExec(null, commandSpec));
76  } else if (command.allArguments()) {
77  final String[] tmp3 = PATTERN_SPACES.split(remainingCommandList, 2);
78  list.add(new CommandExec(command, tmp3.length == 2 ? tmp3[1] : ""));
79  break;
80  } else {
81  list.add(new CommandExec(command, commandArgs));
82  }
83  }
84  if (tmp.length < 2) {
85  break;
86  }
87  remainingCommandList = tmp[1];
88  }
89  return list;
90  }
91 
92 }
com.realtime.crossfire.jxclient.commands.Command
Definition: Command.java:31
com.realtime.crossfire.jxclient.commands.CommandExpander
Definition: CommandExpander.java:35
com.realtime.crossfire.jxclient.commands.CommandExec
Definition: CommandExec.java:32
com.realtime.crossfire.jxclient.commands.CommandExpander.expand
static Collection< CommandExec > expand(@NotNull final CharSequence commandList, @NotNull final Commands commands)
Definition: CommandExpander.java:63
com.realtime.crossfire.jxclient.commands.CommandExpander.PATTERN_SPACES
static final Pattern PATTERN_SPACES
Definition: CommandExpander.java:47
com.realtime.crossfire.jxclient.commands.Commands
Definition: Commands.java:37
com.realtime.crossfire.jxclient.commands.CommandExpander.PATTERN_SEPARATOR
static final Pattern PATTERN_SEPARATOR
Definition: CommandExpander.java:41
com.realtime.crossfire.jxclient.commands.CommandExpander.CommandExpander
CommandExpander()
Definition: CommandExpander.java:52
com.realtime.crossfire.jxclient.commands.Command.allArguments
boolean allArguments()