View Javadoc

1   /*
2   Karma CLI - Command Line Interface for the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library 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 GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.cli.cmd;
20  
21  import nl.toolforge.karma.console.CommandRenderer;
22  import nl.toolforge.karma.core.bundle.BundleCache;
23  import nl.toolforge.karma.core.cmd.CommandDescriptor;
24  import nl.toolforge.karma.core.cmd.CommandException;
25  import nl.toolforge.karma.core.cmd.CommandFactory;
26  import nl.toolforge.karma.core.cmd.CommandLoadException;
27  import nl.toolforge.karma.core.cmd.CommandResponse;
28  import nl.toolforge.karma.core.cmd.DefaultCommand;
29  import nl.toolforge.karma.core.cmd.event.MessageEvent;
30  import nl.toolforge.karma.core.cmd.event.SimpleMessage;
31  import org.apache.commons.lang.StringUtils;
32  
33  import java.util.ResourceBundle;
34  
35  /***
36   * @author W.G.Helmantel
37   * @version $Id: HelpImpl.java,v 1.5 2004/10/09 21:28:06 asmedes Exp $
38   */
39  public class HelpImpl extends DefaultCommand {
40  
41    private static final ResourceBundle FRONTEND_MESSAGES = BundleCache.getInstance().getBundle(BundleCache.FRONTEND_MESSAGES_KEY);
42  
43    private CommandResponse response = new CommandResponse();
44  
45    public HelpImpl(CommandDescriptor descriptor) {
46      super(descriptor);
47    }
48  
49    /***
50     *
51     * @throws CommandException Occurs when a command load error occurred.
52     */
53    public void execute() throws CommandException {
54  
55      String commandName = getCommandLine().getOptionValue("c");
56  
57      try {
58        String renderedStuff = null;
59        SimpleMessage message = null;
60  
61        if (commandName != null) {
62          renderedStuff = CommandRenderer.renderCommand(commandName);
63  
64          int usage = FRONTEND_MESSAGES.getString("message.COMMAND_HELP").length() + commandName.length();
65  
66          message =
67              new SimpleMessage(
68                  "\n" + FRONTEND_MESSAGES.getString("message.COMMAND_HELP") + "\n" + StringUtils.repeat("-", usage) + "\n" + renderedStuff,
69                  new Object[]{commandName}
70                  );
71        } else {
72          renderedStuff = CommandRenderer.renderedCommands(CommandFactory.getInstance().getCommands());
73          renderedStuff += "\n" + FRONTEND_MESSAGES.getString("message.HELP_DETAILS") + "\n";
74          renderedStuff += CommandRenderer.renderCommand("help");
75          message = new SimpleMessage("\n" + FRONTEND_MESSAGES.getString("message.VALID_COMMANDS") + "\n" + renderedStuff);
76        }
77  
78        response.addEvent(new MessageEvent(message));
79  
80      } catch (CommandLoadException e) {
81        throw new CommandException(e.getErrorCode(), e.getMessageArguments());
82      }
83    }
84  
85    /***
86     * Returns the response object for this command.
87     *
88     * @return The response object for this command.
89     */
90    public CommandResponse getCommandResponse() {
91      return response;
92    }
93  
94  }