1 package nl.tno.imb.mc;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8
9
10
11
12
13
14
15
16
17
18 public class StandardSettings
19 {
20
21 Map<String, String> switches = new HashMap<>();
22
23
24 List<String> arguments = new ArrayList<>();
25
26
27 private int argumentIndex = 0;
28
29
30
31
32
33 public StandardSettings(final String[] commandLineArguments)
34 {
35 for (String arg : commandLineArguments)
36 {
37 if (arg.startsWith("/") || arg.startsWith("-"))
38 {
39 String[] fields = arg.substring(1).split("[=:]");
40 String value = "";
41 if (fields.length > 1)
42 {
43 value = fields[1];
44
45 }
46 this.switches.put(fields[0].toLowerCase(), value);
47 }
48 else
49 {
50 this.arguments.add(arg);
51 }
52 }
53 }
54
55
56
57
58
59
60 public boolean testSwitch(final String switchName)
61 {
62 return this.switches.containsKey(switchName.toLowerCase());
63 }
64
65
66
67
68
69
70
71 public String getSwitch(final String switchName, final String defaultValue)
72 {
73 String result = this.switches.get(switchName.toLowerCase());
74 if (null != result)
75 {
76 return result;
77 }
78 return defaultValue;
79 }
80
81
82
83
84
85 public String firstArgument()
86 {
87 this.argumentIndex = 0;
88 return nextArgument();
89 }
90
91
92
93
94
95
96 public String nextArgument()
97 {
98 if (this.argumentIndex >= this.arguments.size())
99 {
100 return "";
101 }
102 return this.arguments.get(this.argumentIndex++);
103 }
104
105 public String getSetting(final String settingName, final String defaultValue)
106 {
107 if (!testSwitch(settingName))
108 {
109 try
110 {
111 String result = ConfigurationManager.appSettings(settingName);
112 if (null == result)
113 {
114 return defaultValue;
115 }
116 return result;
117 }
118 catch (ConfigurationErrorsException cee)
119 {
120 return defaultValue;
121 }
122 }
123 else
124 {
125 return getSwitch(settingName, defaultValue);
126 }
127 }
128
129 }