001: /*
002: * Command.java
003: *
004: * Copyright (C) 2007 Ferran Busquets
005: *
006: * This program is free software: you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation, either version 3 of the License, or
009: * any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program. If not, see <http://www.gnu.org/licenses/>.
018: *
019: */
020:
021: package org.naturalcli;
022:
023: /**
024: * Represents a command definition
025: *
026: * @author Ferran Busquets
027: */
028: public class Command {
029:
030: /** Help message */
031: private String help;
032:
033: /** Command executor */
034: private ICommandExecutor executor;
035:
036: /** Syntax definition */
037: private Syntax syntax;
038:
039: private final char CHAR_HIDDEN_COMMAND = '.';
040:
041: /**
042: * Constructs a new command.
043: *
044: * @param syntax the syntax for the command.
045: * @param helpthe help help of the command.
046: * @param ce command executor.
047: * @throws InvalidSyntaxDefinionException.
048: */
049: public Command(String syntax, String help, ICommandExecutor ce)
050: throws InvalidSyntaxException {
051: this .prepare(syntax, help, ce);
052: }
053:
054: /**
055: * Default constructor only for inheritors.
056: */
057: protected Command() {
058: }
059:
060: /**
061: * Initialize the command.
062: *
063: * @param syntax the syntax for the command.
064: * @param helpthe help help of the command.
065: * @param ce command executor.
066: * @throws InvalidSyntaxDefinionException.
067: */
068: protected void prepare(String syntax, String help,
069: ICommandExecutor ce) throws InvalidSyntaxException {
070: if (help == null || help.length() == 0)
071: throw new IllegalArgumentException(
072: "Syntax cannot be empty.");
073: if (ce == null)
074: throw new IllegalArgumentException(
075: "Command executor cannot be null.");
076: this .help = help;
077: this .syntax = new Syntax(syntax);
078: this .executor = ce;
079: }
080:
081: /**
082: * Determine if this is a hidden command.
083: *
084: * @return <code>true</code> if it's a hidden command, <code>false</code> if not.
085: */
086: public boolean isHidden() {
087: return getHelp().charAt(0) == CHAR_HIDDEN_COMMAND;
088: }
089:
090: /**
091: * Returns a string with the syntax for the command.
092: *
093: * @return A string with the syntax for the command.
094: */
095: public Syntax getSyntax() {
096: return syntax;
097: }
098:
099: /**
100: * Returns the help for the commend.
101: *
102: * @return The help for the command.
103: */
104: public String getHelp() {
105: return help;
106: }
107:
108: /**
109: * Get the executor for the command.
110: *
111: * @return the executor.
112: */
113: public ICommandExecutor getExecutor() {
114: return executor;
115: }
116:
117: }
|