You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultTypedConfigGetter.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.text.MessageFormat;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import java.util.concurrent.TimeUnit;
  48. import java.util.regex.Matcher;
  49. import java.util.regex.Pattern;
  50. import org.eclipse.jgit.annotations.NonNull;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.lib.Config.ConfigEnum;
  53. import org.eclipse.jgit.transport.RefSpec;
  54. import org.eclipse.jgit.util.StringUtils;
  55. /**
  56. * An {@link org.eclipse.jgit.lib.TypedConfigGetter} that throws
  57. * {@link java.lang.IllegalArgumentException} on invalid values.
  58. *
  59. * @since 4.9
  60. */
  61. public class DefaultTypedConfigGetter implements TypedConfigGetter {
  62. /** {@inheritDoc} */
  63. @Override
  64. public boolean getBoolean(Config config, String section, String subsection,
  65. String name, boolean defaultValue) {
  66. String n = config.getRawString(section, subsection, name);
  67. if (n == null) {
  68. return defaultValue;
  69. }
  70. if (Config.MAGIC_EMPTY_VALUE == n) {
  71. return true;
  72. }
  73. try {
  74. return StringUtils.toBoolean(n);
  75. } catch (IllegalArgumentException err) {
  76. throw new IllegalArgumentException(MessageFormat.format(
  77. JGitText.get().invalidBooleanValue, section, name, n));
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public <T extends Enum<?>> T getEnum(Config config, T[] all, String section,
  83. String subsection, String name, T defaultValue) {
  84. String value = config.getString(section, subsection, name);
  85. if (value == null) {
  86. return defaultValue;
  87. }
  88. if (all[0] instanceof ConfigEnum) {
  89. for (T t : all) {
  90. if (((ConfigEnum) t).matchConfigValue(value)) {
  91. return t;
  92. }
  93. }
  94. }
  95. String n = value.replace(' ', '_');
  96. // Because of c98abc9c0586c73ef7df4172644b7dd21c979e9d being used in
  97. // the real world before its breakage was fully understood, we must
  98. // also accept '-' as though it were ' '.
  99. n = n.replace('-', '_');
  100. T trueState = null;
  101. T falseState = null;
  102. for (T e : all) {
  103. if (StringUtils.equalsIgnoreCase(e.name(), n)) {
  104. return e;
  105. } else if (StringUtils.equalsIgnoreCase(e.name(), "TRUE")) { //$NON-NLS-1$
  106. trueState = e;
  107. } else if (StringUtils.equalsIgnoreCase(e.name(), "FALSE")) { //$NON-NLS-1$
  108. falseState = e;
  109. }
  110. }
  111. // This is an odd little fallback. C Git sometimes allows boolean
  112. // values in a tri-state with other things. If we have both a true
  113. // and a false value in our enumeration, assume its one of those.
  114. //
  115. if (trueState != null && falseState != null) {
  116. try {
  117. return StringUtils.toBoolean(n) ? trueState : falseState;
  118. } catch (IllegalArgumentException err) {
  119. // Fall through and use our custom error below.
  120. }
  121. }
  122. if (subsection != null) {
  123. throw new IllegalArgumentException(
  124. MessageFormat.format(JGitText.get().enumValueNotSupported3,
  125. section, subsection, name, value));
  126. } else {
  127. throw new IllegalArgumentException(
  128. MessageFormat.format(JGitText.get().enumValueNotSupported2,
  129. section, name, value));
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public int getInt(Config config, String section, String subsection,
  135. String name, int defaultValue) {
  136. long val = config.getLong(section, subsection, name, defaultValue);
  137. if (Integer.MIN_VALUE <= val && val <= Integer.MAX_VALUE) {
  138. return (int) val;
  139. }
  140. throw new IllegalArgumentException(MessageFormat
  141. .format(JGitText.get().integerValueOutOfRange, section, name));
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public long getLong(Config config, String section, String subsection,
  146. String name, long defaultValue) {
  147. final String str = config.getString(section, subsection, name);
  148. if (str == null) {
  149. return defaultValue;
  150. }
  151. String n = str.trim();
  152. if (n.length() == 0) {
  153. return defaultValue;
  154. }
  155. long mul = 1;
  156. switch (StringUtils.toLowerCase(n.charAt(n.length() - 1))) {
  157. case 'g':
  158. mul = Config.GiB;
  159. break;
  160. case 'm':
  161. mul = Config.MiB;
  162. break;
  163. case 'k':
  164. mul = Config.KiB;
  165. break;
  166. }
  167. if (mul > 1) {
  168. n = n.substring(0, n.length() - 1).trim();
  169. }
  170. if (n.length() == 0) {
  171. return defaultValue;
  172. }
  173. try {
  174. return mul * Long.parseLong(n);
  175. } catch (NumberFormatException nfe) {
  176. throw new IllegalArgumentException(MessageFormat.format(
  177. JGitText.get().invalidIntegerValue, section, name, str));
  178. }
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public long getTimeUnit(Config config, String section, String subsection,
  183. String name, long defaultValue, TimeUnit wantUnit) {
  184. String valueString = config.getString(section, subsection, name);
  185. if (valueString == null) {
  186. return defaultValue;
  187. }
  188. String s = valueString.trim();
  189. if (s.length() == 0) {
  190. return defaultValue;
  191. }
  192. if (s.startsWith("-")/* negative */) { //$NON-NLS-1$
  193. throw notTimeUnit(section, subsection, name, valueString);
  194. }
  195. Matcher m = Pattern.compile("^(0|[1-9][0-9]*)\\s*(.*)$") //$NON-NLS-1$
  196. .matcher(valueString);
  197. if (!m.matches()) {
  198. return defaultValue;
  199. }
  200. String digits = m.group(1);
  201. String unitName = m.group(2).trim();
  202. TimeUnit inputUnit;
  203. int inputMul;
  204. if (unitName.isEmpty()) {
  205. inputUnit = wantUnit;
  206. inputMul = 1;
  207. } else if (match(unitName, "ns", "nanoseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  208. inputUnit = TimeUnit.NANOSECONDS;
  209. inputMul = 1;
  210. } else if (match(unitName, "us", "microseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  211. inputUnit = TimeUnit.MICROSECONDS;
  212. inputMul = 1;
  213. } else if (match(unitName, "ms", "milliseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  214. inputUnit = TimeUnit.MILLISECONDS;
  215. inputMul = 1;
  216. } else if (match(unitName, "s", "sec", "second", "seconds")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  217. inputUnit = TimeUnit.SECONDS;
  218. inputMul = 1;
  219. } else if (match(unitName, "m", "min", "minute", "minutes")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  220. inputUnit = TimeUnit.MINUTES;
  221. inputMul = 1;
  222. } else if (match(unitName, "h", "hr", "hour", "hours")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  223. inputUnit = TimeUnit.HOURS;
  224. inputMul = 1;
  225. } else if (match(unitName, "d", "day", "days")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  226. inputUnit = TimeUnit.DAYS;
  227. inputMul = 1;
  228. } else if (match(unitName, "w", "week", "weeks")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  229. inputUnit = TimeUnit.DAYS;
  230. inputMul = 7;
  231. } else if (match(unitName, "mon", "month", "months")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  232. inputUnit = TimeUnit.DAYS;
  233. inputMul = 30;
  234. } else if (match(unitName, "y", "year", "years")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  235. inputUnit = TimeUnit.DAYS;
  236. inputMul = 365;
  237. } else {
  238. throw notTimeUnit(section, subsection, name, valueString);
  239. }
  240. try {
  241. return wantUnit.convert(Long.parseLong(digits) * inputMul,
  242. inputUnit);
  243. } catch (NumberFormatException nfe) {
  244. throw notTimeUnit(section, subsection, unitName, valueString);
  245. }
  246. }
  247. private static boolean match(String a, String... cases) {
  248. for (String b : cases) {
  249. if (b != null && b.equalsIgnoreCase(a)) {
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. private static IllegalArgumentException notTimeUnit(String section,
  256. String subsection, String name, String valueString) {
  257. if (subsection != null) {
  258. return new IllegalArgumentException(
  259. MessageFormat.format(JGitText.get().invalidTimeUnitValue3,
  260. section, subsection, name, valueString));
  261. }
  262. return new IllegalArgumentException(
  263. MessageFormat.format(JGitText.get().invalidTimeUnitValue2,
  264. section, name, valueString));
  265. }
  266. /** {@inheritDoc} */
  267. @Override
  268. @NonNull
  269. public List<RefSpec> getRefSpecs(Config config, String section,
  270. String subsection, String name) {
  271. String[] values = config.getStringList(section, subsection, name);
  272. List<RefSpec> result = new ArrayList<>(values.length);
  273. for (String spec : values) {
  274. result.add(new RefSpec(spec));
  275. }
  276. return result;
  277. }
  278. }