KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > ValidCommand


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.util.Vector JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 /**
33    The <code>ValidCommand</code> object represents a valid command.
34       @version $Revision: 1.3 $
35  */

36 public class ValidCommand implements ICommand, Serializable JavaDoc
37 {
38     // name of the valid command
39
private String JavaDoc name;
40     // number of operands in this command
41
private String JavaDoc numberOfOperands;
42     // List of valid options
43
private Vector JavaDoc validOptions;
44     // list of required options
45
private Vector JavaDoc requiredOptions;
46     // list of deprecated options
47
private Vector JavaDoc deprecatedOptions;
48     // class name of this command
49
private String JavaDoc className;
50     // usage text for this command
51
private String JavaDoc usageText = null;
52     // List of properties
53
private Hashtable JavaDoc properties;
54     //default value for optional operand
55
private String JavaDoc defaultOperand = null;
56     
57     // Regular expression of defaultoperand to be valid for number of operands
58
private transient static final String JavaDoc DEFAULTOPERAND_QUANTIFIER_REGEXP = "[\\*\\?]";
59     
60     /**
61         Construct a vanila ValidCommand object
62      */

63     public ValidCommand()
64     {
65         validOptions = new Vector JavaDoc();
66         requiredOptions = new Vector JavaDoc();
67         deprecatedOptions = new Vector JavaDoc();
68         properties = new Hashtable JavaDoc();
69     }
70
71    
72     /**
73         Construct ValidCommand object with the given arguments.
74     @param name the name of the valid command
75     @param numberOfOperands number of operands in this command
76     @param validOptions a list of validOptions
77     @param requiredOptions a list of required options
78     @param deprecatedOptions a list of deprecated options
79     @param usageText usage text for this command
80     
81     */

82     public ValidCommand(String JavaDoc name, String JavaDoc numberOfOperands,
83                         Vector JavaDoc validOptions, Vector JavaDoc requiredOptions,
84                         Vector JavaDoc deprecatedOptions, String JavaDoc usageText)
85     {
86         this.name = name;
87         this.numberOfOperands = numberOfOperands;
88         this.validOptions = validOptions;
89         this.requiredOptions = requiredOptions;
90         this.deprecatedOptions = deprecatedOptions;
91         this.usageText = usageText;
92     }
93
94     /**
95         Get the name of the command
96         @return the name of the Valid Command
97      */

98     public String JavaDoc getName()
99     {
100         return name;
101     }
102
103     /**
104         Sets the name of the Valid Command
105     @param name name of the command
106      */

107     public void setName(String JavaDoc name)
108     {
109         this.name = name;
110     }
111
112     /**
113         Returns the number of operands in this Command
114         @return the number of operands
115      */

116     public String JavaDoc getNumberOfOperands()
117     {
118         return numberOfOperands;
119     }
120
121    /**
122         Get the default operand for this command
123         defaultoperand is valid if numberofoperand attribute is
124         either * or ?. If the numberofoperand is finite or "+"
125         then this attribute is ignored.
126     */

127     public String JavaDoc getDefaultOperand()
128     {
129         return this.defaultOperand;
130     }
131
132
133     /**
134         Sets the number of operands for this command
135         @param numberOfOperands the number of operands
136      */

137     public void setNumberOfOperands(String JavaDoc numberOfOperands)
138     {
139         this.numberOfOperands = numberOfOperands;
140     }
141
142     /**
143         Sets the default operand for this command
144         defaultoperand is valid if numberofoperand attribute is
145         either * or ?. If the numberofoperand is finite or "+"
146         then this attribute is ignored and set to nil.
147         @param defaultOperand
148      */

149     public void setDefaultOperand(String JavaDoc defaultOperand)
150     {
151         if (defaultOperand != null &&
152             numberOfOperands.matches(DEFAULTOPERAND_QUANTIFIER_REGEXP))
153         {
154             this.defaultOperand = defaultOperand;
155         }
156         else
157             this.defaultOperand = null;
158     }
159
160
161     /**
162         Returns the list of valid options for this command
163         @return the list of valid options
164      */

165     public Vector JavaDoc getValidOptions()
166     {
167         return validOptions;
168     }
169
170     /**
171         Sets the list of valid options for this command
172         @param validOptions the valid options to set
173      */

174     public void setValidOptions(Vector JavaDoc validOptions)
175     {
176         this.validOptions = validOptions;
177     }
178
179     /**
180         Gets the list of required options for this command
181         @return list of required options
182      */

183     public Vector JavaDoc getRequiredOptions()
184     {
185         return requiredOptions;
186     }
187
188     /**
189         Sets the list of Required options for this command
190         @param requiredOptions the list of required options to set
191      */

192     public void setRequiredOptions(Vector JavaDoc requiredOptions)
193     {
194         this.requiredOptions = requiredOptions;
195     }
196
197     /**
198         Gets the list of deprecated options for this command
199         @return list of deprecated options
200      */

201     public Vector JavaDoc getDeprecatedOptions()
202     {
203         return deprecatedOptions;
204     }
205
206     /**
207         Sets the list of Deprecated options for this command
208         @param deprecatedOptions the list of deprecated options to set
209      */

210     public void setDeprecatedOptions(Vector JavaDoc deprecatedOptions)
211     {
212         this.deprecatedOptions = deprecatedOptions;
213     }
214
215     /**
216         Returns the class name for this command
217         @return the class name
218      */

219     public String JavaDoc getClassName()
220     {
221         return className;
222     }
223     
224     /**
225         Sets the class name for this command
226         @param className the class name to set
227      */

228     public void setClassName(String JavaDoc className)
229     {
230         this.className = className;
231     }
232     
233     /**
234         Return the usage text
235         @return the usage text of the command
236      */

237     public String JavaDoc getUsageText()
238     {
239         return usageText;
240     }
241     
242     /*
243         set the usage text
244         @param usageText the usage text to set
245      */

246     public void setUsageText(String JavaDoc usageText)
247     {
248         this.usageText = usageText;
249     }
250
251     /**
252         Returns the option which matches in the options list
253         @param longOptionName the long option name
254         @return the ValidOption object, return null if option
255             does not exist.
256      */

257     public ValidOption getOption(String JavaDoc longOptionName)
258     {
259         Vector JavaDoc allOptions = this.getOptions();
260         if (longOptionName != null) {
261             for (int ii=0; ii<allOptions.size(); ii++)
262             {
263                 ValidOption option = (ValidOption)allOptions.get(ii);
264                 if ((option.getName()).equals(longOptionName))
265                     return option;
266             }
267         }
268         return null;
269     }
270     
271     /**
272         Returns the alternate deprecated option
273         @param optionName the deprecated option name
274         @return the ValidOption object, return null if option
275             does not exist.
276      */

277     public ValidOption getAlternateDeprecatedOption(String JavaDoc optionName)
278     {
279         Vector JavaDoc allOptions = new Vector JavaDoc(validOptions);
280         allOptions.addAll(requiredOptions);
281         boolean hasAltDeprecatedOption = false;
282         ValidOption altDeprecatedOption = null;
283         for (int i = 0; (i < allOptions.size()) && !hasAltDeprecatedOption; i++)
284         {
285             ValidOption validOption = (ValidOption)allOptions.get(i);
286             final String JavaDoc deprecatedOptionStr = validOption.getDeprecatedOption();
287             if ((deprecatedOptionStr != null) &&
288                 (deprecatedOptionStr.equals(optionName)))
289             {
290                 hasAltDeprecatedOption = true;
291                 altDeprecatedOption = validOption;
292             }
293         }
294         return altDeprecatedOption;
295     }
296     
297     /**
298         Gets all the options (required and valid)
299         @return the list of valid options
300      */

301     public Vector JavaDoc getOptions()
302     {
303         Vector JavaDoc options = new Vector JavaDoc(validOptions);
304         options.addAll(requiredOptions);
305         options.addAll(deprecatedOptions);
306         return options;
307     }
308     
309     /**
310       Gets all the properties of the command
311       @return the list of properties
312      */

313     public Hashtable JavaDoc getProperties()
314     {
315         return properties;
316     }
317     
318     /**
319       Searches for the property with the specified key in this properties list
320       @return the property
321      */

322     public Object JavaDoc getProperty(String JavaDoc key)
323     {
324         return properties.get(key);
325     }
326
327     /**
328      Sets the properties of the command
329      @param key
330      @param value
331      */

332     public void setProperty(String JavaDoc key, Object JavaDoc value)
333     {
334         properties.put(key, value);
335     }
336     
337     /**
338         Add ValidOption object to the valid option list
339         @param option valid option list
340      */

341     public void addValidOption(ValidOption option)
342     {
343         if ((option != null) && !hasValidOption(option.getName()))
344         {
345             validOptions.add(option);
346         }
347     }
348
349     /**
350         Adds the required option to the required options list
351         @param option option to add to the required
352      */

353     public void addRequiredOption(ValidOption option)
354     {
355         if ((option != null) && !hasRequiredOption(option.getName()))
356         {
357             requiredOptions.add(option);
358         }
359     }
360
361     /**
362         Adds the deprecated option to the deprecated options list
363         @param option option to add to the deprecated
364      */

365     public void addDeprecatedOption(ValidOption option)
366     {
367         if ((option != null) && !hasDeprecatedOption(option.getName()))
368         {
369             deprecatedOptions.add(option);
370         }
371     }
372
373     /**
374         Deletes the option from the options list
375         @param option option to delete from the list
376      */

377     public void deleteOption(ValidOption option)
378     {
379         if (option != null)
380         {
381             validOptions.remove(option);
382         }
383     }
384
385     /**
386         Checks if the option exists
387         @param optionName The ValidOption name
388         @return true if the option exist
389      */

390     public boolean hasValidOption(String JavaDoc optionName)
391     {
392         boolean hasValidOption = false;
393         if (optionName != null) {
394             for (int i = 0; (i < validOptions.size()) && !hasValidOption; i++)
395             {
396                 if (((ValidOption)validOptions.get(i)).getName().equals(optionName))
397                     hasValidOption = true;
398             }
399         }
400         return hasValidOption;
401     }
402
403     /**
404      * Checks if the option exists
405      * @param option The ValidOption object
406      * @return true if the option exist
407      */

408     public boolean hasValidOption(ValidOption option)
409     {
410         return validOptions.contains(option);
411     }
412     
413     /**
414      * Checks if the required option exists
415      * @param optionName The required option name
416      * @return true if the option exist
417      */

418     public boolean hasRequiredOption(String JavaDoc optionName)
419     {
420         boolean hasRequiredOption = false;
421         for (int i = 0; (i < requiredOptions.size()) && !hasRequiredOption; i++)
422         {
423             if (((ValidOption)requiredOptions.get(i)).getName().equals(optionName))
424                 hasRequiredOption = true;
425         }
426         return hasRequiredOption;
427     }
428
429     /**
430      * Checks if the required option exists
431      * @param option The RequiredOption object
432      * @return true if the option exist
433      */

434     public boolean hasRequiredOption(ValidOption option)
435     {
436         return requiredOptions.contains(option);
437     }
438     
439     /**
440      * Checks if the deprecated option exists
441      * @param optionName The deprecated option name
442      * @return true if the option exist
443      */

444     public boolean hasDeprecatedOption(String JavaDoc optionName)
445     {
446         boolean hasDeprecatedOption = false;
447         for (int i = 0; (i < deprecatedOptions.size()) && !hasDeprecatedOption; i++)
448         {
449             if (((ValidOption)deprecatedOptions.get(i)).getName().equals(optionName))
450                 hasDeprecatedOption = true;
451         }
452         return hasDeprecatedOption;
453     }
454
455     /**
456      * Checks if the deprecated option exists
457      * @param option The DeprecatedOption object
458      * @return true if the option exist in the deprecatedOptions list
459      */

460     public boolean hasDeprecatedOption(ValidOption option)
461     {
462         return deprecatedOptions.contains(option);
463     }
464     
465     /**
466      * Checks if the alternate deprecated option exists
467      * @param optionName The deprecated option name
468      * @return true if the option exist
469      */

470     public boolean hasAlternateDeprecatedOption(String JavaDoc optionName)
471     {
472         Vector JavaDoc allOptions = new Vector JavaDoc(validOptions);
473         allOptions.addAll(requiredOptions);
474         boolean hasAltDeprecatedOption = false;
475         for (int i = 0; (i < allOptions.size()) && !hasAltDeprecatedOption; i++)
476         {
477             String JavaDoc deprecatedOptionStr =
478                        ((ValidOption)allOptions.get(i)).getDeprecatedOption();
479             if ((deprecatedOptionStr != null) &&
480                 (deprecatedOptionStr.equals(optionName)))
481             {
482                 hasAltDeprecatedOption = true;
483             }
484         }
485         return hasAltDeprecatedOption;
486     }
487
488     
489     /**
490      Finds if a property by name exists in the list
491      @param propertyName name of the property to find in the list
492      @return true if the property is found
493      */

494     public boolean hasProperty(String JavaDoc propertyName)
495     {
496         return properties.containsKey(propertyName);
497     }
498
499     
500     /**
501      * Calls ReplaceOptionsList to relace the options with replaceOption
502      * in validOptions, requiredOptions and deprecatedOptions.
503      **/

504     public void replaceAllOptions(ValidOption replaceOption)
505     {
506         if (replaceOption != null)
507         {
508             final String JavaDoc replaceOptionName = replaceOption.getName();
509             if (hasValidOption(replaceOptionName))
510             {
511                 replaceOptionsList(validOptions, replaceOption);
512             }
513             if (hasRequiredOption(replaceOptionName))
514             {
515                 replaceOptionsList(requiredOptions, replaceOption);
516             }
517             if (hasDeprecatedOption(replaceOptionName))
518             {
519                 replaceOptionsList(deprecatedOptions, replaceOption);
520             }
521         }
522     }
523
524     
525     /**
526      * Search in the options list and replace it with replaceOption object.
527      **/

528     private void replaceOptionsList(Vector JavaDoc optionsList, ValidOption replaceOption)
529     {
530         final String JavaDoc replaceOptionName = replaceOption.getName();
531         for (int ii = 0; ii<optionsList.size(); ii++)
532         {
533             try {
534                 if (((ValidOption)optionsList.get(ii)).getName().equals(replaceOptionName))
535                 {
536                     if (((ValidOption)optionsList.get(ii)).hasDeprecatedOption()) {
537                         final String JavaDoc deprecatedOption=((ValidOption)optionsList.get(ii)).getDeprecatedOption();
538                         replaceOption.setDeprecatedOption(deprecatedOption);
539                     }
540                     
541                     optionsList.set(ii, replaceOption);
542                     break;
543                 }
544             }
545             catch (Exception JavaDoc e)
546             {
547                 e.printStackTrace();
548             }
549         }
550         return;
551     }
552     
553     
554
555     
556     /**
557      Returns the toString()
558      @return String the object in the string format
559      */

560     public String JavaDoc toString()
561     {
562         String JavaDoc validOptionsStr = "{";
563         for (int i = 0; i < validOptions.size(); i++)
564         {
565             validOptionsStr += validOptions.get(i).toString() + ",";
566         }
567         validOptionsStr += "}";
568        
569         String JavaDoc requiredOptionsStr = "";
570         for (int i = 0; i < requiredOptions.size(); i++)
571         {
572             requiredOptionsStr += requiredOptions.get(i).toString() + ",";
573         }
574         requiredOptionsStr += "}";
575
576         String JavaDoc deprecatedOptionsStr = "";
577         for (int i = 0; i < deprecatedOptions.size(); i++)
578         {
579             deprecatedOptionsStr += deprecatedOptions.get(i).toString() + ",";
580         }
581         deprecatedOptionsStr += "}";
582
583         String JavaDoc propertiesStr = "{";
584         for (Enumeration JavaDoc propertyNames = properties.keys() ; propertyNames.hasMoreElements() ;)
585         {
586             String JavaDoc name = (String JavaDoc) propertyNames.nextElement();
587             Vector JavaDoc value = (Vector JavaDoc) properties.get(name);
588             propertiesStr += "|" + name + " , " + value;
589         }
590         propertiesStr += "}";
591          
592         return (name + " " + numberOfOperands + " | " + validOptionsStr +
593                 " | " + requiredOptionsStr + " | " +
594                 " | " + deprecatedOptionsStr + " | " + usageText + " " +
595                 propertiesStr);
596     }
597 }
598
Popular Tags