001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package modelutils;
043:
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.Hashtable;
047:
048: /**
049: *
050: * @author ak119685
051: */
052: public class Config {
053: char[] argsTemplate = null;
054: ArrayList<String> arguments = new ArrayList<String>();
055: HashMap<String, ArrayList<String>> options = new HashMap<String, ArrayList<String>>();
056: ArrayList<String> flags = new ArrayList<String>();
057:
058: boolean optionRequiresArgument = false;
059:
060: public Config(String template, String[] args) {
061: argsTemplate = template.toCharArray();
062: parseArguments(args);
063: }
064:
065: private boolean parseArguments(String[] args) {
066: int i = 0;
067: String param;
068:
069: while (i < args.length) {
070: param = args[i++];
071:
072: if (isOption(param)) {
073: if (optionRequiresArgument) {
074: if (param.length() == 2) {
075: if (i < args.length) {
076: addParam(param, args[i++]);
077: } else {
078: throw new RuntimeException("Option "
079: + param + " requires argument"); // NOI18N
080: }
081: } else {
082: addParam(param.substring(0, 2), param
083: .substring(2));
084: }
085: } else {
086: flags.add(param);
087: }
088: } else {
089: arguments.add(param);
090: }
091: }
092:
093: return false;
094: }
095:
096: private void addParam(String opt, String param) {
097: ArrayList<String> allParams = options.get(opt);
098:
099: if (allParams == null) {
100: allParams = new ArrayList<String>();
101: options.put(opt, allParams);
102: }
103:
104: allParams.add(param);
105: }
106:
107: private boolean isOption(String arg) {
108: char option;
109:
110: if (arg.startsWith("-")) { // NOI18N
111: option = arg.charAt(1);
112: for (int i = 0; i < argsTemplate.length; i++) {
113: if (option == argsTemplate[i]) {
114: optionRequiresArgument = (i < argsTemplate.length - 1) ? (argsTemplate[i + 1] == ':')
115: : false;
116: return true;
117: }
118: }
119:
120: throw new RuntimeException("Invalid option " + arg); // NOI18N
121: }
122:
123: return false;
124: }
125:
126: public boolean flagSet(String f) {
127: return flags.contains(f);
128: }
129:
130: public ArrayList<String> getParametersFor(String option) {
131: return options.get(option);
132: }
133:
134: public String getParameterFor(String option, String defaultValue) {
135: String result = getParameterFor(option);
136: if (result == null) {
137: result = defaultValue;
138: }
139: return result;
140: }
141:
142: public String getParameterFor(String option) {
143: ArrayList<String> allParams = options.get(option);
144:
145: if (allParams == null) {
146: return null;
147: }
148:
149: return allParams.get(0);
150: }
151:
152: public String getArgument() {
153: return arguments.get(0);
154: }
155:
156: public ArrayList<String> getArguments() {
157: return arguments;
158: }
159: }
|