Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DefaultTypedConfigGetter.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import java.text.MessageFormat;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. import org.eclipse.jgit.annotations.NonNull;
  18. import org.eclipse.jgit.internal.JGitText;
  19. import org.eclipse.jgit.lib.Config.ConfigEnum;
  20. import org.eclipse.jgit.transport.RefSpec;
  21. import org.eclipse.jgit.util.StringUtils;
  22. /**
  23. * An {@link org.eclipse.jgit.lib.TypedConfigGetter} that throws
  24. * {@link java.lang.IllegalArgumentException} on invalid values.
  25. *
  26. * @since 4.9
  27. */
  28. public class DefaultTypedConfigGetter implements TypedConfigGetter {
  29. /** {@inheritDoc} */
  30. @Override
  31. public boolean getBoolean(Config config, String section, String subsection,
  32. String name, boolean defaultValue) {
  33. String n = config.getRawString(section, subsection, name);
  34. if (n == null) {
  35. return defaultValue;
  36. }
  37. if (Config.isMissing(n)) {
  38. return true;
  39. }
  40. try {
  41. return StringUtils.toBoolean(n);
  42. } catch (IllegalArgumentException err) {
  43. throw new IllegalArgumentException(MessageFormat.format(
  44. JGitText.get().invalidBooleanValue, section, name, n), err);
  45. }
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public <T extends Enum<?>> T getEnum(Config config, T[] all, String section,
  50. String subsection, String name, T defaultValue) {
  51. String value = config.getString(section, subsection, name);
  52. if (value == null) {
  53. return defaultValue;
  54. }
  55. if (all[0] instanceof ConfigEnum) {
  56. for (T t : all) {
  57. if (((ConfigEnum) t).matchConfigValue(value)) {
  58. return t;
  59. }
  60. }
  61. }
  62. String n = value.replace(' ', '_');
  63. // Because of c98abc9c0586c73ef7df4172644b7dd21c979e9d being used in
  64. // the real world before its breakage was fully understood, we must
  65. // also accept '-' as though it were ' '.
  66. n = n.replace('-', '_');
  67. T trueState = null;
  68. T falseState = null;
  69. for (T e : all) {
  70. if (StringUtils.equalsIgnoreCase(e.name(), n)) {
  71. return e;
  72. } else if (StringUtils.equalsIgnoreCase(e.name(), "TRUE")) { //$NON-NLS-1$
  73. trueState = e;
  74. } else if (StringUtils.equalsIgnoreCase(e.name(), "FALSE")) { //$NON-NLS-1$
  75. falseState = e;
  76. }
  77. }
  78. // This is an odd little fallback. C Git sometimes allows boolean
  79. // values in a tri-state with other things. If we have both a true
  80. // and a false value in our enumeration, assume its one of those.
  81. //
  82. if (trueState != null && falseState != null) {
  83. try {
  84. return StringUtils.toBoolean(n) ? trueState : falseState;
  85. } catch (IllegalArgumentException err) {
  86. // Fall through and use our custom error below.
  87. }
  88. }
  89. if (subsection != null) {
  90. throw new IllegalArgumentException(
  91. MessageFormat.format(JGitText.get().enumValueNotSupported3,
  92. section, subsection, name, value));
  93. }
  94. throw new IllegalArgumentException(MessageFormat.format(
  95. JGitText.get().enumValueNotSupported2, section, name, value));
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public int getInt(Config config, String section, String subsection,
  100. String name, int defaultValue) {
  101. long val = config.getLong(section, subsection, name, defaultValue);
  102. if (Integer.MIN_VALUE <= val && val <= Integer.MAX_VALUE) {
  103. return (int) val;
  104. }
  105. throw new IllegalArgumentException(MessageFormat
  106. .format(JGitText.get().integerValueOutOfRange, section, name));
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public long getLong(Config config, String section, String subsection,
  111. String name, long defaultValue) {
  112. final String str = config.getString(section, subsection, name);
  113. if (str == null) {
  114. return defaultValue;
  115. }
  116. String n = str.trim();
  117. if (n.length() == 0) {
  118. return defaultValue;
  119. }
  120. long mul = 1;
  121. switch (StringUtils.toLowerCase(n.charAt(n.length() - 1))) {
  122. case 'g':
  123. mul = Config.GiB;
  124. break;
  125. case 'm':
  126. mul = Config.MiB;
  127. break;
  128. case 'k':
  129. mul = Config.KiB;
  130. break;
  131. }
  132. if (mul > 1) {
  133. n = n.substring(0, n.length() - 1).trim();
  134. }
  135. if (n.length() == 0) {
  136. return defaultValue;
  137. }
  138. try {
  139. return mul * Long.parseLong(n);
  140. } catch (NumberFormatException nfe) {
  141. throw new IllegalArgumentException(MessageFormat.format(
  142. JGitText.get().invalidIntegerValue, section, name, str),
  143. nfe);
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. @Override
  148. public long getTimeUnit(Config config, String section, String subsection,
  149. String name, long defaultValue, TimeUnit wantUnit) {
  150. String valueString = config.getString(section, subsection, name);
  151. if (valueString == null) {
  152. return defaultValue;
  153. }
  154. String s = valueString.trim();
  155. if (s.length() == 0) {
  156. return defaultValue;
  157. }
  158. if (s.startsWith("-")/* negative */) { //$NON-NLS-1$
  159. throw notTimeUnit(section, subsection, name, valueString);
  160. }
  161. Matcher m = Pattern.compile("^(0|[1-9][0-9]*)\\s*(.*)$") //$NON-NLS-1$
  162. .matcher(valueString);
  163. if (!m.matches()) {
  164. return defaultValue;
  165. }
  166. String digits = m.group(1);
  167. String unitName = m.group(2).trim();
  168. TimeUnit inputUnit;
  169. int inputMul;
  170. if (unitName.isEmpty()) {
  171. inputUnit = wantUnit;
  172. inputMul = 1;
  173. } else if (match(unitName, "ns", "nanoseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  174. inputUnit = TimeUnit.NANOSECONDS;
  175. inputMul = 1;
  176. } else if (match(unitName, "us", "microseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  177. inputUnit = TimeUnit.MICROSECONDS;
  178. inputMul = 1;
  179. } else if (match(unitName, "ms", "milliseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  180. inputUnit = TimeUnit.MILLISECONDS;
  181. inputMul = 1;
  182. } else if (match(unitName, "s", "sec", "second", "seconds")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  183. inputUnit = TimeUnit.SECONDS;
  184. inputMul = 1;
  185. } else if (match(unitName, "m", "min", "minute", "minutes")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  186. inputUnit = TimeUnit.MINUTES;
  187. inputMul = 1;
  188. } else if (match(unitName, "h", "hr", "hour", "hours")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  189. inputUnit = TimeUnit.HOURS;
  190. inputMul = 1;
  191. } else if (match(unitName, "d", "day", "days")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  192. inputUnit = TimeUnit.DAYS;
  193. inputMul = 1;
  194. } else if (match(unitName, "w", "week", "weeks")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  195. inputUnit = TimeUnit.DAYS;
  196. inputMul = 7;
  197. } else if (match(unitName, "mon", "month", "months")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  198. inputUnit = TimeUnit.DAYS;
  199. inputMul = 30;
  200. } else if (match(unitName, "y", "year", "years")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  201. inputUnit = TimeUnit.DAYS;
  202. inputMul = 365;
  203. } else {
  204. throw notTimeUnit(section, subsection, name, valueString);
  205. }
  206. try {
  207. return wantUnit.convert(Long.parseLong(digits) * inputMul,
  208. inputUnit);
  209. } catch (NumberFormatException nfe) {
  210. IllegalArgumentException iae = notTimeUnit(section, subsection,
  211. unitName, valueString);
  212. iae.initCause(nfe);
  213. throw iae;
  214. }
  215. }
  216. private static boolean match(String a, String... cases) {
  217. for (String b : cases) {
  218. if (b != null && b.equalsIgnoreCase(a)) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. private static IllegalArgumentException notTimeUnit(String section,
  225. String subsection, String name, String valueString) {
  226. if (subsection != null) {
  227. return new IllegalArgumentException(
  228. MessageFormat.format(JGitText.get().invalidTimeUnitValue3,
  229. section, subsection, name, valueString));
  230. }
  231. return new IllegalArgumentException(
  232. MessageFormat.format(JGitText.get().invalidTimeUnitValue2,
  233. section, name, valueString));
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. @NonNull
  238. public List<RefSpec> getRefSpecs(Config config, String section,
  239. String subsection, String name) {
  240. String[] values = config.getStringList(section, subsection, name);
  241. List<RefSpec> result = new ArrayList<>(values.length);
  242. for (String spec : values) {
  243. result.add(new RefSpec(spec));
  244. }
  245. return result;
  246. }
  247. }