001: package de.java2html.commandline;
002:
003: import java.util.HashMap;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Set;
008:
009: /**
010: * @author Markus Gebhard
011: */
012: public class CommandlineArguments {
013: private Map valueByParameterName = new HashMap();
014: private Set flags = new HashSet();
015:
016: public CommandlineArguments(String[] args)
017: throws IllegalCommandlineParametersException {
018: for (int i = 0; i < args.length; i++) {
019: if (isValidFlagName(args[i])) {
020: String flagName = args[i].substring(1).toLowerCase();
021: String value = null;
022: if (i + 1 < args.length) {
023: if (!isValidFlagName(args[i + 1])) {
024: value = args[i + 1];
025: ++i;
026: }
027: }
028: if (flags.contains(flagName)
029: || valueByParameterName.containsKey(flagName)) {
030: throw new IllegalCommandlineParametersException(
031: "Argument '"
032: + flagName
033: + "' has been defined more often than once.");
034: }
035: if (value == null) {
036: flags.add(flagName);
037: } else {
038: valueByParameterName.put(flagName, value);
039: }
040: } else {
041: throw new IllegalCommandlineParametersException(
042: "Unexpected parameter '" + args[i] + "'");
043: }
044: }
045: }
046:
047: private boolean isValidFlagName(String name) {
048: if (!name.startsWith("-")) {
049: return false;
050: }
051: try {
052: Double.parseDouble(name);
053: return false;
054: } catch (NumberFormatException e) {
055: return true;
056: }
057: }
058:
059: public String getParameterStringValue(String parameterName) {
060: return (String) valueByParameterName.get(parameterName
061: .toLowerCase());
062: }
063:
064: public String getParameterStringValue(String parameterName,
065: String defaultValue) {
066: String value = getParameterStringValue(parameterName);
067: return value != null ? value : defaultValue;
068: }
069:
070: public int getParameterPositiveIntValue(String parameterName,
071: int defaultValue)
072: throws IllegalCommandlineParametersException {
073: String value = getParameterStringValue(parameterName);
074: if (value == null) {
075: return defaultValue;
076: }
077:
078: int i = parseInt(value);
079: if (i < 0) {
080: throw new IllegalCommandlineParametersException(
081: "Positive value for int argument expected, was '"
082: + value + "'");
083: }
084: return i;
085: }
086:
087: private int parseInt(String value)
088: throws IllegalCommandlineParametersException {
089: try {
090: return Integer.parseInt(value);
091: } catch (NumberFormatException e) {
092: throw new IllegalCommandlineParametersException(
093: "Illegal format for a int value '" + value + "'");
094: }
095: }
096:
097: public boolean isFlagSet(String flagName) {
098: return flags.contains(flagName.toLowerCase());
099: }
100:
101: public boolean isParameterSet(String flagName) {
102: return valueByParameterName.containsKey(flagName.toLowerCase());
103: }
104:
105: public String getRequiredParameterStringValue(String parameterName)
106: throws IllegalCommandlineParametersException {
107: String value = getParameterStringValue(parameterName);
108: if (value == null) {
109: throw new IllegalCommandlineParametersException(
110: "Missing required parameter value for '"
111: + parameterName + "'");
112: }
113: return value;
114: }
115:
116: public void assertContainsNoUnsupportedFlags(
117: String[] supportedFlagNames)
118: throws IllegalCommandlineParametersException {
119: Set supportedFlagNameSet = createLowerCaseNameSet(supportedFlagNames);
120: Iterator iterator = flags.iterator();
121: while (iterator.hasNext()) {
122: String flagName = (String) iterator.next();
123: if (!supportedFlagNameSet.contains(flagName)) {
124: throw new IllegalCommandlineParametersException(
125: "The flag '"
126: + flagName
127: + "' is not supported (or missing required parameter value).");
128: }
129: }
130: }
131:
132: public void assertContainsNoUnsupportedParameters(
133: String[] supportedParameterNames)
134: throws IllegalCommandlineParametersException {
135: Set supportedParameterNameSet = createLowerCaseNameSet(supportedParameterNames);
136: Iterator iterator = valueByParameterName.keySet().iterator();
137: while (iterator.hasNext()) {
138: String flagName = (String) iterator.next();
139: if (!supportedParameterNameSet.contains(flagName)) {
140: throw new IllegalCommandlineParametersException(
141: "The parameter '" + flagName
142: + "' is not supported.");
143: }
144: }
145: }
146:
147: private Set createLowerCaseNameSet(String[] names) {
148: Set supportedFlagNameSet = new HashSet();
149: for (int i = 0; i < names.length; i++) {
150: supportedFlagNameSet.add(names[i].toLowerCase());
151: }
152: return supportedFlagNameSet;
153: }
154: }
|