001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: /**
023: * General purpose command line options parser.
024: * If this is used outside of Axis just remove the Axis specific sections.
025: */
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import java.net.MalformedURLException;
031: import java.util.ArrayList;
032: import java.util.Vector;
033:
034: public class OptionsParser {
035: private static final Log log = LogFactory
036: .getLog(OptionsParser.class);
037: String args[] = null;
038: Vector usedArgs = null;
039:
040: /**
041: * Constructor - just pass in the <b>args</b> from the command line.
042: */
043: public OptionsParser(String _args[]) throws MalformedURLException {
044: if (_args == null) {
045: _args = new String[] {};
046: }
047:
048: args = _args;
049: usedArgs = null;
050: getUser();
051: getPassword();
052: }
053:
054: public String getPassword() {
055: return (isValueSet('w'));
056: }
057:
058: /**
059: * This returns an array of unused args - these are the non-option
060: * args from the command line.
061: */
062: public String[] getRemainingArgs() {
063: ArrayList al = null;
064: int loop;
065:
066: for (loop = 0; loop < args.length; loop++) {
067: if ((args[loop] == null) || (args[loop].length() == 0)) {
068: continue;
069: }
070:
071: if (args[loop].charAt(0) == '-') {
072: continue;
073: }
074:
075: if (al == null) {
076: al = new ArrayList();
077: }
078:
079: al.add(args[loop]);
080: }
081:
082: if (al == null) {
083: return (null);
084: }
085:
086: String a[] = new String[al.size()];
087:
088: for (loop = 0; loop < al.size(); loop++) {
089: a[loop] = (String) al.get(loop);
090: }
091:
092: return (a);
093: }
094:
095: /**
096: * This just returns a string with the unprocessed flags - mainly
097: * for error reporting - so you can report the unknown flags.
098: */
099: public String getRemainingFlags() {
100: StringBuffer sb = null;
101: int loop;
102:
103: for (loop = 0; loop < args.length; loop++) {
104: if ((args[loop] == null) || (args[loop].length() == 0)) {
105: continue;
106: }
107:
108: if (args[loop].charAt(0) != '-') {
109: continue;
110: }
111:
112: if (sb == null) {
113: sb = new StringBuffer();
114: }
115:
116: sb.append(args[loop].substring(1));
117: }
118:
119: return ((sb == null) ? null : sb.toString());
120: }
121:
122: public String getUser() {
123: return (isValueSet('u'));
124: }
125:
126: /**
127: * Returns an int specifying the number of times that the flag was
128: * specified on the command line. Once this flag is looked for you
129: * must save the result because if you call it again for the same
130: * flag you'll get zero.
131: */
132: public int isFlagSet(char optChar) {
133: int value = 0;
134: int loop;
135: int i;
136:
137: for (loop = 0; (usedArgs != null) && (loop < usedArgs.size()); loop++) {
138: String arg = (String) usedArgs.elementAt(loop);
139:
140: if (arg.charAt(0) != '-') {
141: continue;
142: }
143:
144: for (i = 0; i < arg.length(); i++) {
145: if (arg.charAt(i) == optChar) {
146: value++;
147: }
148: }
149: }
150:
151: for (loop = 0; loop < args.length; loop++) {
152: if ((args[loop] == null) || (args[loop].length() == 0)) {
153: continue;
154: }
155:
156: if (args[loop].charAt(0) != '-') {
157: continue;
158: }
159:
160: while ((args[loop] != null)
161: && (i = args[loop].indexOf(optChar)) != -1) {
162: args[loop] = args[loop].substring(0, i)
163: + args[loop].substring(i + 1);
164:
165: if (args[loop].length() == 1) {
166: args[loop] = null;
167: }
168:
169: value++;
170:
171: if (usedArgs == null) {
172: usedArgs = new Vector();
173: }
174:
175: usedArgs.add("-" + optChar);
176: }
177: }
178:
179: return (value);
180: }
181:
182: /**
183: * Returns a string (or null) specifying the value for the passed
184: * option. If the option isn't there then null is returned. The
185: * option's value can be specified one of two ways:
186: * -x value
187: * -xvalue
188: * Note that: -ax value
189: * is not value (meaning flag 'a' followed by option 'x'.
190: * Options with values must be the first char after the '-'.
191: * If the option is specified more than once then the last one wins.
192: */
193: public String isValueSet(char optChar) {
194: String value = null;
195: int loop;
196: int i;
197:
198: for (loop = 0; (usedArgs != null) && (loop < usedArgs.size()); loop++) {
199: String arg = (String) usedArgs.elementAt(loop);
200:
201: if ((arg.charAt(0) != '-') || (arg.charAt(1) != optChar)) {
202: continue;
203: }
204:
205: value = arg.substring(2);
206:
207: if (loop + 1 < usedArgs.size()) {
208: value = (String) usedArgs.elementAt(++loop);
209: }
210: }
211:
212: for (loop = 0; loop < args.length; loop++) {
213: if ((args[loop] == null) || (args[loop].length() == 0)) {
214: continue;
215: }
216:
217: if (args[loop].charAt(0) != '-') {
218: continue;
219: }
220:
221: i = args[loop].indexOf(optChar);
222:
223: if (i != 1) {
224: continue;
225: }
226:
227: if (i != args[loop].length() - 1) {
228:
229: // Not at end of arg, so use rest of arg as value
230: value = args[loop].substring(i + 1);
231: args[loop] = args[loop].substring(0, i);
232: } else {
233:
234: // Remove the char from the current arg
235: args[loop] = args[loop].substring(0, i);
236:
237: // Nothing after char so use next arg
238: if ((loop + 1 < args.length)
239: && (args[loop + 1] != null)) {
240:
241: // Next arg is there and non-null
242: if (args[loop + 1].charAt(0) != '-') {
243: value = args[loop + 1];
244: args[loop + 1] = null;
245: }
246: } else {
247:
248: // Next is null or not there - do nothing
249: // value = null ;
250: }
251: }
252:
253: if (args[loop].length() == 1) {
254: args[loop] = null;
255: }
256:
257: // For now, keep looping to get that last on there
258: // break ;
259: }
260:
261: if (value != null) {
262: if (usedArgs == null) {
263: usedArgs = new Vector();
264: }
265:
266: usedArgs.add("-" + optChar);
267:
268: if (value.length() > 0) {
269: usedArgs.add(value);
270: }
271: }
272:
273: return (value);
274: }
275: }
|