KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > util > CommandLine


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

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.util;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Simple command like parser/handler.
42  * A dashed argument is one preceded by a dash character.
43  * In a sequence of arguments:
44  * 1) If a dashed argument starts with a command character
45  * the rest of the argument, if any, is assume to be a value.
46  * 2) If a dashed argument is followed by a non-dashed
47  * argument value. The value is assumed to be associated
48  * with the preceding dashed argument name.
49  * 2) If an argument with a dash prefix is not followed by
50  * a non-dashed value, and does not use a command character,
51  * it is assumed to be a flag.
52  * 3) If none of the above is true, the argument is a name.
53  *
54  * Command characters can be added with the addCommand method.
55  * Values can be retrieved with the getValue method.
56  * Flag states can be retrieved with the getFlag method.
57  * Names can be retieved with the getNameCount and getName methods.
58  *
59  * @author Claude Duguay
60 **/

61
62 public class CommandLine
63 {
64     public static boolean VERBOSE = false;
65
66     protected List JavaDoc commands = new ArrayList JavaDoc();
67
68     protected List JavaDoc flags = new ArrayList JavaDoc();
69     protected List JavaDoc names = new ArrayList JavaDoc();
70     protected Map JavaDoc values = new HashMap JavaDoc();
71
72     public CommandLine(String JavaDoc chars, String JavaDoc[] args)
73     {
74         for (int i = 0; i < chars.length(); i++)
75         {
76             addCommand(chars.charAt(i));
77         }
78         parse(args);
79     }
80
81     public CommandLine(String JavaDoc[] args)
82     {
83         parse(args);
84     }
85
86     protected void parse(String JavaDoc[] args)
87     {
88         for (int i = 0; i < args.length; i++)
89         {
90             String JavaDoc thisArg = args[i];
91             String JavaDoc nextArg = null;
92             if (i < args.length - 1)
93             {
94                 nextArg = args[i + 1];
95             }
96
97             if (thisArg.startsWith("-"))
98             {
99                 if (thisArg.length() > 2)
100                 {
101                     Character JavaDoc chr = new Character JavaDoc(thisArg.charAt(1));
102                     if (commands.contains(chr))
103                     {
104                         String JavaDoc key = chr.toString();
105                         String JavaDoc val = thisArg.substring(2);
106                         if (VERBOSE)
107                         {
108                             System.out.println("Value " + key + "=" + val);
109                         }
110                         values.put(key, val);
111                     }
112                 }
113                 if (nextArg != null && !nextArg.startsWith("-"))
114                 {
115                     String JavaDoc key = thisArg.substring(1);
116                     String JavaDoc val = nextArg;
117                     if (VERBOSE)
118                     {
119                         System.out.println("Value " + key + "=" + val);
120                     }
121                     values.put(key, val);
122                     i++;
123                 }
124                 else
125                 {
126                     String JavaDoc flag = thisArg.substring(1);
127                     flags.add(flag);
128                     if (VERBOSE)
129                     {
130                         System.out.println("Flag " + flag);
131                     }
132                 }
133             }
134             else
135             {
136                 if (VERBOSE)
137                 {
138                     System.out.println("Name " + thisArg);
139                 }
140                 names.add(thisArg);
141             }
142         }
143     }
144
145     public void addCommand(char command)
146     {
147         commands.add(new Character JavaDoc(command));
148     }
149
150     public boolean hasValue(String JavaDoc key)
151     {
152         return values.containsKey(key);
153     }
154
155     public String JavaDoc getValue(String JavaDoc key)
156     {
157         return (String JavaDoc) values.get(key);
158     }
159
160     public boolean getFlag(String JavaDoc key)
161     {
162         return flags.contains(key);
163     }
164
165     public int getNameCount()
166     {
167         return names.size();
168     }
169
170     public String JavaDoc getName(int index)
171     {
172         return (String JavaDoc) names.get(index);
173     }
174
175     public static void main(String JavaDoc[] args)
176     {
177         CommandLine cmd = new CommandLine("f", args);
178     }
179 }
180
Popular Tags