View Javadoc

1   /*
2   Karma core - Core of 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.core.cmd;
20  
21  import org.apache.commons.cli.Options;
22  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  import java.util.StringTokenizer;
27  
28  /***
29   * A <code>CommandDescriptor</code> is the object representation of a command as it is specified in a
30   * <code>command.xml</code> file.
31   *
32   * @author D.A. Smedes
33   * @version $Id: CommandDescriptor.java,v 1.21 2004/11/10 23:53:07 asmedes Exp $
34   */
35  public final class CommandDescriptor {
36  
37    private String name = null;
38    private String description = null;
39    private String helpText = null;
40    private String className = null;
41    private String aliasString = null;
42  
43    private Set aliasList = null;
44    private Options options = null;
45  
46    public CommandDescriptor(String name, String aliasString) {
47      this.name = name;
48      this.aliasString = aliasString;
49  
50      aliasList = new HashSet();
51      aliasList.add(name);
52  
53      createAliasList(aliasString);
54    }
55  
56    /***
57     * Returns the name of the command represented by this <code>CommandDescriptor</code>.
58     *
59     * @return The name of the command represented by this <code>CommandDescriptor</code>
60     */
61    public String getName() {
62      return this.name;
63    }
64  
65    /***
66     * The aliasses of a command are all Strings by which the command can be referenced (including its name).
67     * 
68     * @return
69     */
70    public Set getAliasList() {
71      return aliasList;
72    }
73  
74    private void createAliasList(String aliasString) {
75  
76      if (aliasString.indexOf(" ") != -1) {
77        StringTokenizer tokenizer = new StringTokenizer(aliasString, " ");
78        while (tokenizer.hasMoreTokens()) {
79          aliasList.add(tokenizer.nextToken());
80        }
81      } else {
82        aliasList.add(aliasString);
83      }
84    }
85  
86    public String getAlias() {
87      return aliasString;
88    }
89  
90    public String getDescription() {
91      return description;
92    }
93  
94    public void setDescription(String description) {
95      this.description = description;
96    }
97  
98    /***
99     * @param options The name of the command (the lt;options&gt;-child-element attribute of the &lt;command&gt;-element).
100    */
101   public void addOptions(Options options) {
102     this.options = options;
103   }
104 
105   public Options getOptions() {
106     if (options == null) {
107       return new Options();
108     } else {
109       return this.options;
110     }
111   }
112 
113   public String getClassName() {
114     return className;
115   }
116 
117   public void setClassName(String className) {
118     this.className = className;
119   }
120 
121   public void setHelp(String helpText) {
122     this.helpText = helpText;
123   }
124 
125   public String getHelp() {
126     return this.helpText;
127   }
128 
129   /***
130    * Commands are equal when their names are equal or any alias equals an alias from <code>o</code> or the other way
131    * around.
132    *
133    * @param o The object instance that should be compared with <code>this</code>.
134    * @return <code>true</code> if this command descriptor is equal to <code>o</code> or <code>null</code> when
135    *   <code>o</code> is not a <code>CommandDescriptor</code> instance or when it is not the same object.
136    */
137   public boolean equals(Object o) {
138 
139     if (o instanceof CommandDescriptor) {
140 
141       Set s1 = aliasList;
142       Set s2 = ((CommandDescriptor) o).aliasList;
143 
144       for (Iterator i = s1.iterator(); i.hasNext();) {
145         if (s2.contains((String) i.next())) {
146           return true;
147         }
148       }
149 
150       for (Iterator i = s2.iterator(); i.hasNext();) {
151         if (s1.contains((String) i.next())) {
152           return true;
153         }
154       }
155       return false;
156     } else {
157       return false;
158     }
159   }
160 
161 //
162 //  public int hashCode() {
163 //    return (aliasList == null ? 0 : aliasList.hashCode());
164 //  }
165 
166   public String toString() {
167 
168     String a = "";
169     for (Iterator i = aliasList.iterator(); i.hasNext();) {
170       a += (String) i.next();
171       if (i.hasNext()) {
172         a += ", ";
173       }
174     }
175     return name + " (" + a + ")";
176   }
177 }
178 
179