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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.isMissing(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. }
  127. throw new IllegalArgumentException(MessageFormat.format(
  128. JGitText.get().enumValueNotSupported2, section, name, value));
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public int getInt(Config config, String section, String subsection,
  133. String name, int defaultValue) {
  134. long val = config.getLong(section, subsection, name, defaultValue);
  135. if (Integer.MIN_VALUE <= val && val <= Integer.MAX_VALUE) {
  136. return (int) val;
  137. }
  138. throw new IllegalArgumentException(MessageFormat
  139. .format(JGitText.get().integerValueOutOfRange, section, name));
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public long getLong(Config config, String section, String subsection,
  144. String name, long defaultValue) {
  145. final String str = config.getString(section, subsection, name);
  146. if (str == null) {
  147. return defaultValue;
  148. }
  149. String n = str.trim();
  150. if (n.length() == 0) {
  151. return defaultValue;
  152. }
  153. long mul = 1;
  154. switch (StringUtils.toLowerCase(n.charAt(n.length() - 1))) {
  155. case 'g':
  156. mul = Config.GiB;
  157. break;
  158. case 'm':
  159. mul = Config.MiB;
  160. break;
  161. case 'k':
  162. mul = Config.KiB;
  163. break;
  164. }
  165. if (mul > 1) {
  166. n = n.substring(0, n.length() - 1).trim();
  167. }
  168. if (n.length() == 0) {
  169. return defaultValue;
  170. }
  171. try {
  172. return mul * Long.parseLong(n);
  173. } catch (NumberFormatException nfe) {
  174. throw new IllegalArgumentException(MessageFormat.format(
  175. JGitText.get().invalidIntegerValue, section, name, str));
  176. }
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public long getTimeUnit(Config config, String section, String subsection,
  181. String name, long defaultValue, TimeUnit wantUnit) {
  182. String valueString = config.getString(section, subsection, name);
  183. if (valueString == null) {
  184. return defaultValue;
  185. }
  186. String s = valueString.trim();
  187. if (s.length() == 0) {
  188. return defaultValue;
  189. }
  190. if (s.startsWith("-")/* negative */) { //$NON-NLS-1$
  191. throw notTimeUnit(section, subsection, name, valueString);
  192. }
  193. Matcher m = Pattern.compile("^(0|[1-9][0-9]*)\\s*(.*)$") //$NON-NLS-1$
  194. .matcher(valueString);
  195. if (!m.matches()) {
  196. return defaultValue;
  197. }
  198. String digits = m.group(1);
  199. String unitName = m.group(2).trim();
  200. TimeUnit inputUnit;
  201. int inputMul;
  202. if (unitName.isEmpty()) {
  203. inputUnit = wantUnit;
  204. inputMul = 1;
  205. } else if (match(unitName, "ns", "nanoseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  206. inputUnit = TimeUnit.NANOSECONDS;
  207. inputMul = 1;
  208. } else if (match(unitName, "us", "microseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  209. inputUnit = TimeUnit.MICROSECONDS;
  210. inputMul = 1;
  211. } else if (match(unitName, "ms", "milliseconds")) { //$NON-NLS-1$ //$NON-NLS-2$
  212. inputUnit = TimeUnit.MILLISECONDS;
  213. inputMul = 1;
  214. } else if (match(unitName, "s", "sec", "second", "seconds")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  215. inputUnit = TimeUnit.SECONDS;
  216. inputMul = 1;
  217. } else if (match(unitName, "m", "min", "minute", "minutes")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  218. inputUnit = TimeUnit.MINUTES;
  219. inputMul = 1;
  220. } else if (match(unitName, "h", "hr", "hour", "hours")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  221. inputUnit = TimeUnit.HOURS;
  222. inputMul = 1;
  223. } else if (match(unitName, "d", "day", "days")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  224. inputUnit = TimeUnit.DAYS;
  225. inputMul = 1;
  226. } else if (match(unitName, "w", "week", "weeks")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  227. inputUnit = TimeUnit.DAYS;
  228. inputMul = 7;
  229. } else if (match(unitName, "mon", "month", "months")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  230. inputUnit = TimeUnit.DAYS;
  231. inputMul = 30;
  232. } else if (match(unitName, "y", "year", "years")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  233. inputUnit = TimeUnit.DAYS;
  234. inputMul = 365;
  235. } else {
  236. throw notTimeUnit(section, subsection, name, valueString);
  237. }
  238. try {
  239. return wantUnit.convert(Long.parseLong(digits) * inputMul,
  240. inputUnit);
  241. } catch (NumberFormatException nfe) {
  242. throw notTimeUnit(section, subsection, unitName, valueString);
  243. }
  244. }
  245. private static boolean match(String a, String... cases) {
  246. for (String b : cases) {
  247. if (b != null && b.equalsIgnoreCase(a)) {
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. private static IllegalArgumentException notTimeUnit(String section,
  254. String subsection, String name, String valueString) {
  255. if (subsection != null) {
  256. return new IllegalArgumentException(
  257. MessageFormat.format(JGitText.get().invalidTimeUnitValue3,
  258. section, subsection, name, valueString));
  259. }
  260. return new IllegalArgumentException(
  261. MessageFormat.format(JGitText.get().invalidTimeUnitValue2,
  262. section, name, valueString));
  263. }
  264. /** {@inheritDoc} */
  265. @Override
  266. @NonNull
  267. public List<RefSpec> getRefSpecs(Config config, String section,
  268. String subsection, String name) {
  269. String[] values = config.getStringList(section, subsection, name);
  270. List<RefSpec> result = new ArrayList<>(values.length);
  271. for (String spec : values) {
  272. result.add(new RefSpec(spec));
  273. }
  274. return result;
  275. }
  276. }