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: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: public class CommandLineOptionParser implements
029: CommandLineOptionConstants {
030:
031: //states
032: private static int STARTED = 0;
033: private static int NEW_OPTION = 1;
034: private static int SUB_PARAM_OF_OPTION = 2;
035:
036: private Map commandLineOptions;
037:
038: public CommandLineOptionParser(Map commandLineOptions) {
039: this .commandLineOptions = commandLineOptions;
040: }
041:
042: public CommandLineOptionParser(String[] args) {
043: this .commandLineOptions = this .parse(args);
044:
045: }
046:
047: /**
048: * Return a list with <code>CommandLineOption</code> objects
049: *
050: * @param args
051: * @return CommandLineOption List
052: */
053: private Map parse(String[] args) {
054: Map commandLineOptions = new HashMap();
055:
056: if (0 == args.length) {
057: return commandLineOptions;
058: }
059:
060: //State 0 means started
061: //State 1 means earlier one was a new -option
062: //State 2 means earlier one was a sub param of a -option
063:
064: int state = STARTED;
065: ArrayList optionBundle = null;
066: String optionType = null;
067: CommandLineOption commandLineOption;
068:
069: for (int i = 0; i < args.length; i++) {
070:
071: if (args[i].startsWith("-")) {
072: if (STARTED == state) {
073: // fresh one
074: state = NEW_OPTION;
075: optionType = args[i];
076: } else if (SUB_PARAM_OF_OPTION == state
077: || NEW_OPTION == state) {
078: // new one but old one should be saved
079: commandLineOption = new CommandLineOption(
080: optionType, optionBundle);
081: commandLineOptions.put(commandLineOption
082: .getOptionType(), commandLineOption);
083: state = NEW_OPTION;
084: optionType = args[i];
085: optionBundle = null;
086:
087: }
088: } else {
089: if (STARTED == state) {
090: commandLineOption = new CommandLineOption(
091: CommandLineOptionConstants.SOLE_INPUT, args);
092: commandLineOptions.put(commandLineOption
093: .getOptionType(), commandLineOption);
094: return commandLineOptions;
095:
096: } else if (NEW_OPTION == state) {
097: optionBundle = new ArrayList();
098: optionBundle.add(args[i]);
099: state = SUB_PARAM_OF_OPTION;
100:
101: } else if (SUB_PARAM_OF_OPTION == state) {
102: optionBundle.add(args[i]);
103: }
104:
105: }
106:
107: }
108:
109: commandLineOption = new CommandLineOption(optionType,
110: optionBundle);
111: commandLineOptions.put(commandLineOption.getOptionType(),
112: commandLineOption);
113: return commandLineOptions;
114: }
115:
116: public Map getAllOptions() {
117: return this .commandLineOptions;
118: }
119:
120: public List getInvalidOptions(OptionsValidator validator) {
121: List faultList = new ArrayList();
122: Iterator iterator = this .commandLineOptions.values().iterator();
123: while (iterator.hasNext()) {
124: CommandLineOption commandLineOption = ((CommandLineOption) (iterator
125: .next()));
126: if (validator.isInvalid(commandLineOption)) {
127: faultList.add(commandLineOption);
128: }
129: }
130:
131: return faultList;
132: }
133:
134: }
|