1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.lib;
45
46 import java.text.MessageFormat;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.concurrent.TimeUnit;
50 import java.util.regex.Matcher;
51 import java.util.regex.Pattern;
52
53 import org.eclipse.jgit.annotations.NonNull;
54 import org.eclipse.jgit.internal.JGitText;
55 import org.eclipse.jgit.lib.Config.ConfigEnum;
56 import org.eclipse.jgit.transport.RefSpec;
57 import org.eclipse.jgit.util.StringUtils;
58
59
60
61
62
63
64
65 public class DefaultTypedConfigGetter implements TypedConfigGetter {
66
67
68 @Override
69 public boolean getBoolean(Config config, String section, String subsection,
70 String name, boolean defaultValue) {
71 String n = config.getRawString(section, subsection, name);
72 if (n == null) {
73 return defaultValue;
74 }
75 if (Config.isMissing(n)) {
76 return true;
77 }
78 try {
79 return StringUtils.toBoolean(n);
80 } catch (IllegalArgumentException err) {
81 throw new IllegalArgumentException(MessageFormat.format(
82 JGitText.get().invalidBooleanValue, section, name, n));
83 }
84 }
85
86
87 @Override
88 public <T extends Enum<?>> T getEnum(Config config, T[] all, String section,
89 String subsection, String name, T defaultValue) {
90 String value = config.getString(section, subsection, name);
91 if (value == null) {
92 return defaultValue;
93 }
94 if (all[0] instanceof ConfigEnum) {
95 for (T t : all) {
96 if (((ConfigEnum) t).matchConfigValue(value)) {
97 return t;
98 }
99 }
100 }
101
102 String n = value.replace(' ', '_');
103
104
105
106
107 n = n.replace('-', '_');
108
109 T trueState = null;
110 T falseState = null;
111 for (T e : all) {
112 if (StringUtils.equalsIgnoreCase(e.name(), n)) {
113 return e;
114 } else if (StringUtils.equalsIgnoreCase(e.name(), "TRUE")) {
115 trueState = e;
116 } else if (StringUtils.equalsIgnoreCase(e.name(), "FALSE")) {
117 falseState = e;
118 }
119 }
120
121
122
123
124
125 if (trueState != null && falseState != null) {
126 try {
127 return StringUtils.toBoolean(n) ? trueState : falseState;
128 } catch (IllegalArgumentException err) {
129
130 }
131 }
132
133 if (subsection != null) {
134 throw new IllegalArgumentException(
135 MessageFormat.format(JGitText.get().enumValueNotSupported3,
136 section, subsection, name, value));
137 }
138 throw new IllegalArgumentException(MessageFormat.format(
139 JGitText.get().enumValueNotSupported2, section, name, value));
140 }
141
142
143 @Override
144 public int getInt(Config config, String section, String subsection,
145 String name, int defaultValue) {
146 long val = config.getLong(section, subsection, name, defaultValue);
147 if (Integer.MIN_VALUE <= val && val <= Integer.MAX_VALUE) {
148 return (int) val;
149 }
150 throw new IllegalArgumentException(MessageFormat
151 .format(JGitText.get().integerValueOutOfRange, section, name));
152 }
153
154
155 @Override
156 public long getLong(Config config, String section, String subsection,
157 String name, long defaultValue) {
158 final String str = config.getString(section, subsection, name);
159 if (str == null) {
160 return defaultValue;
161 }
162 String n = str.trim();
163 if (n.length() == 0) {
164 return defaultValue;
165 }
166 long mul = 1;
167 switch (StringUtils.toLowerCase(n.charAt(n.length() - 1))) {
168 case 'g':
169 mul = Config.GiB;
170 break;
171 case 'm':
172 mul = Config.MiB;
173 break;
174 case 'k':
175 mul = Config.KiB;
176 break;
177 }
178 if (mul > 1) {
179 n = n.substring(0, n.length() - 1).trim();
180 }
181 if (n.length() == 0) {
182 return defaultValue;
183 }
184 try {
185 return mul * Long.parseLong(n);
186 } catch (NumberFormatException nfe) {
187 throw new IllegalArgumentException(MessageFormat.format(
188 JGitText.get().invalidIntegerValue, section, name, str));
189 }
190 }
191
192
193 @Override
194 public long getTimeUnit(Config config, String section, String subsection,
195 String name, long defaultValue, TimeUnit wantUnit) {
196 String valueString = config.getString(section, subsection, name);
197
198 if (valueString == null) {
199 return defaultValue;
200 }
201
202 String s = valueString.trim();
203 if (s.length() == 0) {
204 return defaultValue;
205 }
206
207 if (s.startsWith("-")) {
208 throw notTimeUnit(section, subsection, name, valueString);
209 }
210
211 Matcher m = Pattern.compile("^(0|[1-9][0-9]*)\\s*(.*)$")
212 .matcher(valueString);
213 if (!m.matches()) {
214 return defaultValue;
215 }
216
217 String digits = m.group(1);
218 String unitName = m.group(2).trim();
219
220 TimeUnit inputUnit;
221 int inputMul;
222
223 if (unitName.isEmpty()) {
224 inputUnit = wantUnit;
225 inputMul = 1;
226
227 } else if (match(unitName, "ns", "nanoseconds")) {
228 inputUnit = TimeUnit.NANOSECONDS;
229 inputMul = 1;
230
231 } else if (match(unitName, "us", "microseconds")) {
232 inputUnit = TimeUnit.MICROSECONDS;
233 inputMul = 1;
234
235 } else if (match(unitName, "ms", "milliseconds")) {
236 inputUnit = TimeUnit.MILLISECONDS;
237 inputMul = 1;
238
239 } else if (match(unitName, "s", "sec", "second", "seconds")) {
240 inputUnit = TimeUnit.SECONDS;
241 inputMul = 1;
242
243 } else if (match(unitName, "m", "min", "minute", "minutes")) {
244 inputUnit = TimeUnit.MINUTES;
245 inputMul = 1;
246
247 } else if (match(unitName, "h", "hr", "hour", "hours")) {
248 inputUnit = TimeUnit.HOURS;
249 inputMul = 1;
250
251 } else if (match(unitName, "d", "day", "days")) {
252 inputUnit = TimeUnit.DAYS;
253 inputMul = 1;
254
255 } else if (match(unitName, "w", "week", "weeks")) {
256 inputUnit = TimeUnit.DAYS;
257 inputMul = 7;
258
259 } else if (match(unitName, "mon", "month", "months")) {
260 inputUnit = TimeUnit.DAYS;
261 inputMul = 30;
262
263 } else if (match(unitName, "y", "year", "years")) {
264 inputUnit = TimeUnit.DAYS;
265 inputMul = 365;
266
267 } else {
268 throw notTimeUnit(section, subsection, name, valueString);
269 }
270
271 try {
272 return wantUnit.convert(Long.parseLong(digits) * inputMul,
273 inputUnit);
274 } catch (NumberFormatException nfe) {
275 throw notTimeUnit(section, subsection, unitName, valueString);
276 }
277 }
278
279 private static boolean match(String a, String... cases) {
280 for (String b : cases) {
281 if (b != null && b.equalsIgnoreCase(a)) {
282 return true;
283 }
284 }
285 return false;
286 }
287
288 private static IllegalArgumentException notTimeUnit(String section,
289 String subsection, String name, String valueString) {
290 if (subsection != null) {
291 return new IllegalArgumentException(
292 MessageFormat.format(JGitText.get().invalidTimeUnitValue3,
293 section, subsection, name, valueString));
294 }
295 return new IllegalArgumentException(
296 MessageFormat.format(JGitText.get().invalidTimeUnitValue2,
297 section, name, valueString));
298 }
299
300
301 @Override
302 @NonNull
303 public List<RefSpec> getRefSpecs(Config config, String section,
304 String subsection, String name) {
305 String[] values = config.getStringList(section, subsection, name);
306 List<RefSpec> result = new ArrayList<>(values.length);
307 for (String spec : values) {
308 result.add(new RefSpec(spec));
309 }
310 return result;
311 }
312 }