View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.cmdl;
21  
22  
23  /***
24   * This class is thread safe.
25   */
26  public class LocalExecuter {
27  
28      //--- Data Field(s) ---
29      
30  	private Command[] commands;
31  
32      //--- Constructor(s) ---
33      
34      public LocalExecuter(Command[] commands) 
35      {
36  		this.commands = commands;
37      }
38      
39      //--- Method(s) ---
40  
41      public Command getCommand(String command)
42      {
43  		Command cmd = Executer.getCommand(command);
44  		if (cmd == null) {
45  			for (int i = 0; i < commands.length; i++) {
46  				if (matches(commands[i], command)) {
47  					cmd = commands[i];
48  					break;
49  				}
50  			}
51  		}
52  		return cmd;
53  	}			
54  
55  	public boolean matches(Command cmd, String name)
56  	{
57  		if (name.equals(cmd.getCommand())) {
58  			return true;
59  		}
60  		else {
61  			String[] aliases = cmd.getAliases();
62  			if (aliases != null) {
63  				for (int i = 0; i < aliases.length; i++) {
64  					if (name.equals(aliases[i])) {
65  						return true;
66  					}
67  				}
68  			}
69  		}
70  		return false;
71      }
72  
73  	public Command[] getCommands()
74  	{
75  		Command[] executerCmds = Executer.getCommands();
76  		Command[] array = new Command[executerCmds.length + commands.length];
77  		System.arraycopy(executerCmds, 0, array, 0, executerCmds.length);
78  		System.arraycopy(commands, 0, array, executerCmds.length, 
79  						 commands.length);
80  		return array;
81  	}
82  
83  }