KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > util > Arguments


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58
59
60 package util;
61
62
63
64 /**
65  * Utility like Unix getopt.
66  *
67  * Usage:
68  *
69  * int c;
70  *
71  * parseArgumentTokens(argv);
72  * while ( (c = getArguments()) != -1 ){
73  *
74  * switch (c) {
75  * case 'v':
76  * System.out.println( "v" );
77  * break;
78  * case 'V':
79  * System.out.println( "V" );
80  * break;
81  *
82  * @version $id$
83  * @author Jeffrey Rodriguez
84  */

85
86 public class Arguments {
87     private boolean fDbug = false;
88     private Queue queueOfSwitches = new Queue(20);
89     private Queue queueStringParameters = new Queue(20);
90     private Queue queueOfOtherStringParameters = new Queue(20);
91     private String JavaDoc[] messageArray = null;
92     private int lastPopArgument = 0;
93
94
95     public Arguments() {
96     }
97
98     /**
99      * Takes the array of standar Args passed
100      * from main method and parses the '-' and
101      * the characters after arg.
102      *
103      * - The value -1 is a special flag that is
104      * used to indicate the beginning of the queue
105      * of flags and it is also to tell the end of
106      * a group of switches.
107      *
108      * This method will generate 3 internal queues.
109      * - A queue that has the switch flag arguments.
110      * e.g.
111      * -dvV
112      * will hold d, v, V, -1.
113      *
114      * - A queue holding the string arguments needed by
115      * the switch flag arguments.
116      * If character -p requires a string argument.
117      * The string argument is saved in the string argument
118      * queue.
119      *
120      * - A queue holding a list of files string parameters
121      * not associated with a switch flag.
122      * -a -v -p myvalue test.xml test1.xml
123      * this queue will containt test.xml test1.xml
124      *
125      * @param arguments
126      * @param argsWithOptions
127      */

128     public void parseArgumentTokens( String JavaDoc[] arguments , char[] argsWithOptions ){
129         int theDash = 0;
130         int lengthOfToken = 0;
131         char []bufferOfToken = null;
132         Object JavaDoc[] temp;
133
134         int argLength = arguments.length;
135
136         outer:
137         for ( int i = 0; i<argLength; i++ ){
138             bufferOfToken = arguments[i].toCharArray();
139             lengthOfToken = bufferOfToken.length;
140             if ( bufferOfToken[0] == '-' ){
141                 int token;
142                 //System.out.println( "argv = " + arguments[i] );
143
//System.out.println( "leng = " + lengthOfToken );
144
for ( int j = 1; j<lengthOfToken; j++ ){
145                     token = bufferOfToken[j];
146                     //System.out.println( "token = " + token );
147
queueOfSwitches.push( (Object JavaDoc ) new Integer JavaDoc( token ));
148                     for ( int k = 0; k< argsWithOptions.length; k++) {
149                         if ( token == argsWithOptions[k] ){
150                             if ( this.fDbug ) {
151                                 System.out.println( "token = " + token );
152                             }
153                             //queueOfSwitches.push( (Object ) new Integer( -1 ));
154
queueStringParameters.push( arguments[++i] );
155                             continue outer;
156                         }
157                     }
158
159                 }
160
161                 if ( i+1 < argLength ){
162                     if ( !( arguments[i+1].charAt(0) == '-') ) //next argument not start '-'
163
queueOfSwitches.push( (Object JavaDoc ) new Integer JavaDoc( -1 )); //put -1 marker
164
}
165
166             } else{
167                 queueOfOtherStringParameters.push( arguments[i] );
168             }
169         }
170
171
172         if ( this.fDbug ) {
173             queueOfSwitches.print();
174             queueStringParameters.print();
175             queueOfOtherStringParameters.print();
176         }
177     }
178
179
180     /**
181      *
182      * @return
183      */

184     public int getArguments(){
185         if ( this.fDbug ){
186             queueOfSwitches.print();
187         }
188
189         //int value = ((Integer ) queueOfSwitches.pop()).intValue();
190
//if ( this.fDbug ) {
191
// System.out.println("value = " + value );
192
//}
193
return queueOfSwitches.empty() ? -1:((Integer JavaDoc ) queueOfSwitches.pop()).intValue();
194     }
195
196
197
198     /**
199      *
200      * @return
201      */

202     public String JavaDoc getStringParameter(){
203         String JavaDoc s = (String JavaDoc) queueStringParameters.pop();
204         if ( this.fDbug ){
205             queueStringParameters.print();
206         }
207         if ( this.fDbug ) {
208             System.out.println( "string par = " + s );
209         }
210         return s;
211     }
212
213
214     public String JavaDoc getlistFiles(){
215
216         if ( this.fDbug ) {
217             queueOfOtherStringParameters.print();
218         }
219
220         String JavaDoc s = (String JavaDoc) queueOfOtherStringParameters.pop();
221         return s;
222     }
223
224
225
226     public int stringParameterLeft( ){
227         return queueStringParameters.size();
228     }
229
230
231     public void setUsage( String JavaDoc[] message ){
232         messageArray = message;
233     }
234
235     public void printUsage() {
236         for ( int i = 0; i< messageArray.length; i++ ){
237             System.err.println( messageArray[i] );
238         }
239     }
240
241     // Private methods
242

243     // Private inner classes
244

245
246     private static final int maxIncrement = 10;
247
248     private class Queue {
249         //private LinkedList queue;
250
private Object JavaDoc[] queue;
251         private int max;
252         private int front;
253         private int rear;
254         private int items;
255
256
257         public Queue( int size) {
258             queue = new Object JavaDoc[size];
259             front = 0;
260             rear = -1;
261             items = 0;
262             max = size;
263             //queue = new LinkedList();
264
}
265         public void push( Object JavaDoc token ) {
266             try {
267                 queue[++rear] = token;
268                 items++;
269             } catch ( ArrayIndexOutOfBoundsException JavaDoc ex ){
270                 Object JavaDoc[] holdQueue = new Object JavaDoc[max + maxIncrement];
271                 System.arraycopy(queue, 0, holdQueue,0,max );
272                 queue = holdQueue;
273                 max += maxIncrement;
274                 queue[rear] = token;
275                 items++;
276             }
277
278             //queue.addLast( token );
279
}
280         public Object JavaDoc pop() {
281             Object JavaDoc token = null;
282             if ( items != 0 ) {
283                 token = queue[front++];
284                 items--;
285             }
286             return token;
287         }
288         public boolean empty(){
289             return(items==0);
290         }
291
292         public int size(){
293             return items;
294         }
295
296         public void clear(){
297             front = 0;
298             rear = -1;
299             items = 0;
300         }
301
302
303         public void print(){
304             for ( int i = front; i <= rear;i++ ){
305                 System.out.println( "token[ " + i
306                                     + "] = " + queue[i] ) ;
307             }
308
309         }
310
311     }
312 }
313
Popular Tags