KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > CLOption


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 // This file is pulled from package org.apache.avalon.excalibur.cli Excalibur
17
// version 4.1 (Jan 30, 2002). Only the package name has been changed.
18
package org.apache.axis.utils;
19
20 import java.util.Arrays JavaDoc;
21
22 /**
23  * Basic class describing an instance of option.
24  *
25  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
26  * @since 4.0
27  */

28 public final class CLOption
29 {
30     /**
31      * Value of {@link #getId} when the option is a text argument.
32      */

33     public static final int TEXT_ARGUMENT = 0;
34
35     private final int m_id;
36     private String JavaDoc[] m_arguments;
37
38     /**
39      * Retrieve argument to option if it takes arguments.
40      *
41      * @return the (first) argument
42      */

43     public final String JavaDoc getArgument()
44     {
45         return getArgument( 0 );
46     }
47
48     /**
49      * Retrieve indexed argument to option if it takes arguments.
50      *
51      * @param index The argument index, from 0 to
52      * {@link #getArgumentCount()}-1.
53      * @return the argument
54      */

55     public final String JavaDoc getArgument( final int index )
56     {
57         if( null == m_arguments || index < 0 || index >= m_arguments.length )
58         {
59             return null;
60         }
61         else
62         {
63             return m_arguments[ index ];
64         }
65     }
66
67     /**
68      * Retrieve id of option.
69      *
70      * The id is eqivalent to character code if it can be a single letter option.
71      *
72      * @return the id
73      */

74     public final int getId()
75     {
76         return m_id;
77     }
78
79     /**
80      * Constructor taking an id (that must be a proper character code)
81      *
82      * @param id the new id
83      */

84     public CLOption( final int id )
85     {
86         m_id = id;
87     }
88
89     /**
90      * Constructor taking argument for option.
91      *
92      * @param argument the argument
93      */

94     public CLOption( final String JavaDoc argument )
95     {
96         this( TEXT_ARGUMENT );
97         addArgument( argument );
98     }
99
100     /**
101      * Mutator of Argument property.
102      *
103      * @param argument the argument
104      */

105     public final void addArgument( final String JavaDoc argument )
106     {
107         if( null == m_arguments ) m_arguments = new String JavaDoc[] { argument };
108         else
109         {
110             final String JavaDoc[] arguments = new String JavaDoc[ m_arguments.length + 1 ];
111             System.arraycopy( m_arguments, 0, arguments, 0, m_arguments.length );
112             arguments[ m_arguments.length ] = argument;
113             m_arguments = arguments;
114         }
115     }
116
117     /**
118     * Get number of arguments.
119     */

120     public final int getArgumentCount()
121     {
122         if( null == m_arguments )
123         {
124             return 0;
125         }
126         else
127         {
128             return m_arguments.length;
129         }
130     }
131
132     /**
133      * Convert to String.
134      *
135      * @return the string value
136      */

137     public final String JavaDoc toString()
138     {
139         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
140         sb.append( "[Option " );
141         sb.append( (char)m_id );
142
143         if( null != m_arguments )
144         {
145             sb.append( ", " );
146             sb.append( Arrays.asList( m_arguments ) );
147         }
148
149         sb.append( " ]" );
150
151         return sb.toString();
152     }
153 }
154
Popular Tags