KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > commands > options > Option


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: Option.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.commands.options;
25
26 import org.enhydra.xml.io.ErrorReporter;
27 import org.enhydra.xml.xmlc.XMLCException;
28
29 /**
30  * Definition of an option. The general or specific options are classes
31  * derived from this class. A general class is parameterizes at instantiation
32  * and is able to create parse multiple options. A specific class only parses
33  * one option. The Option classes should be immutable; a generic client data
34  * object is pass to the parse methods to recieve the configuration.
35  */

36 public abstract class Option {
37     /**
38      * The option name, including the leading `-'.
39      */

40     protected final String JavaDoc name;
41
42     /**
43      * The number of arguments for the option.
44      */

45     protected final int numArgs;
46
47     /**
48      * Are multiple instances of the option allowed?
49      */

50     protected final boolean multipleAllowed;
51
52     /**
53      * Help message for the option.
54      */

55     protected String JavaDoc help;
56
57     /**
58      * Construct a new instance of the object.
59      *
60      * @param name Name of the option, including the leading `-'.
61      * @param numArgs Number of arguments the option takes.
62      * @param multipleAllowed Can the option be specified multiple times?
63      * @param help A string that is used in generating the help message.
64      * It should include the arguments, a `-', then one line message. The
65      * option name and a space will be prepended.
66      */

67     public Option(String JavaDoc name,
68                   int numArgs,
69                   boolean multipleAllowed,
70                   String JavaDoc help) {
71         this.name = name;
72         this.numArgs = numArgs;
73         this.multipleAllowed = multipleAllowed;
74         this.help = help;
75     }
76
77     /**
78      * Get the option name, including leading `-'.
79      */

80     public String JavaDoc getName() {
81         return name;
82     }
83
84     /**
85      * Get the number of arguments for a single instance of the option. If
86      * the option repeats, the actual number of arguments is a multiple of
87      * this.
88      */

89     public int getNumArgs() {
90         return numArgs;
91     }
92     
93     /**
94      * Return true if the option maybe specified multiple times, resulting
95      * in a list of values. Return false if the option can only be specified
96      * once.
97      */

98     public boolean getMultipleAllowed() {
99         return multipleAllowed;
100     }
101
102     /**
103      * Get the help string, including option name.
104      */

105     public String JavaDoc getHelp() {
106         return name + " " + help;
107     }
108
109     /**
110      * Determine if two options are equal. Only the name is considered. It
111      * is illegal to have options with the same name and different attributes.
112      *
113      * @param obj This can be either another Option object or a string. If
114      * an Option object is supplied, the names are compared.
115      * @return True if this object name is the same as the name in
116      */

117     public boolean equals(Object JavaDoc obj) {
118         if (obj instanceof Option) {
119             return name.equals(((Option)obj).getName());
120         } else if (obj instanceof String JavaDoc) {
121             return name.equals((String JavaDoc)obj);
122         } else {
123             return false;
124         }
125     }
126
127     /**
128      * Get the hashcode for the object.
129      */

130     public int hashCode() {
131         return name.hashCode();
132     }
133
134     /**
135      * Parse an instance of the option.
136      *
137      * @param args The option's arguments. Will always have the number of
138      * arguments defined for an option.
139      * @param errorReporter Used to report warnings, such as deprecated
140      * options. Errors should be thrown.
141      * @param clientData Object that is passed to the parser. Normally
142      * contains the object to store the configuration in.
143      */

144     abstract protected void parse(String JavaDoc[] args,
145                                   ErrorReporter errorReporter,
146                                   Object JavaDoc clientData) throws XMLCException;
147 }
148
Popular Tags