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.

ConfigSource.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config;
  23. import com.dmdirc.ui.messages.ColourManager;
  24. import com.dmdirc.util.validators.ColourValidator;
  25. import com.dmdirc.util.validators.DisabledOptionValidator;
  26. import com.dmdirc.util.validators.NumericalValidator;
  27. import com.dmdirc.util.validators.OptionalValidator;
  28. import com.dmdirc.util.validators.PermissiveValidator;
  29. import com.dmdirc.util.validators.Validator;
  30. import com.dmdirc.util.validators.ValidatorChain;
  31. import java.awt.Color;
  32. import java.util.ArrayList;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. /**
  36. * Defines methods to get options from a config source in various forms.
  37. * <p>
  38. * This implementation supports the idea of optional/disabled settings.
  39. * This is where values may be prefixed with the strings <code>true:</code>
  40. * or <code>false:</code>, where the former has no effect on the value of
  41. * the setting, and the latter indicates that the user wishes to disable
  42. * the setting.
  43. * <p>
  44. * Disabled settings allow for optional settings to have default values; a
  45. * value can be added with the <code>false:</code> prefix which effectively
  46. * disables the default value and makes the setting act as though it does not
  47. * have a value. Note that for this sort of behaviour to make sense, it requires
  48. * an implementation that takes values from multiple sources, such as a
  49. * {@link ConfigManager}.
  50. */
  51. public abstract class ConfigSource {
  52. /** A permissive validator to use when callers don't specify one. */
  53. private static final Validator<String> PERMISSIVE_VALIDATOR
  54. = new PermissiveValidator<String>();
  55. /** A validator for integer settings. */
  56. private static final Validator<String> INT_VALIDATOR
  57. = new OptionalValidator(new NumericalValidator(-1, -1));
  58. /** A validator for colour settings. */
  59. private static final Validator<String> COLOUR_VALIDATOR
  60. = new OptionalValidator(new ColourValidator());
  61. /**
  62. * Retrieves the specified option from this config source.
  63. *
  64. * @param domain The domain of the option
  65. * @param option The name of the option
  66. * @return The value of the option, or null if it doesn't exist
  67. */
  68. public String getOption(final String domain, final String option) {
  69. return getOption(domain, option, PERMISSIVE_VALIDATOR);
  70. }
  71. /**
  72. * Retrieves the first value for the specified option that matches the
  73. * specified validator.
  74. *
  75. * @since 0.6.5
  76. * @param domain The domain of the option
  77. * @param option The name of the option
  78. * @param validator The validator to use to check legal values
  79. * @return The value of the option, or null if no matching values exist
  80. */
  81. protected abstract String getOption(final String domain,
  82. final String option, final Validator<String> validator);
  83. /**
  84. * Determines if this source has a value for the specified option which
  85. * matches the specified validator.
  86. *
  87. * @since 0.6.5
  88. * @param domain The domain of the option
  89. * @param option The name of the option
  90. * @param validator The validator to use to check legal values
  91. * @return True iff a matching option exists, false otherwise.
  92. */
  93. protected abstract boolean hasOption(final String domain,
  94. final String option, final Validator<String> validator);
  95. /**
  96. * Determines if this source has the specified String option.
  97. * <p>
  98. * A String option is considered to exist if:
  99. * <ul>
  100. * <li>there is a value for the specified option,
  101. * <li>that value is not empty, and
  102. * <li>that value is not disabled (i.e., it doesn't begin with "false:")
  103. * </ul>
  104. *
  105. * @param domain The domain of the option
  106. * @param option The name of the option
  107. * @since 0.6.3m1
  108. * @return True iff the option exists and is not empty, false otherwise
  109. */
  110. public boolean hasOptionString(final String domain, final String option) {
  111. return hasOptionString(domain, option, PERMISSIVE_VALIDATOR);
  112. }
  113. /**
  114. * Determines if this source has the specified String option.
  115. * <p>
  116. * A String option is considered to exist if:
  117. * <ul>
  118. * <li>there is a value for the specified option that
  119. * satisfies the specified validator,
  120. * <li>that value is not empty, and
  121. * <li>that value is not disabled (i.e., it doesn't begin with "false:")
  122. * </ul>
  123. *
  124. * @param domain The domain of the option
  125. * @param option The name of the option
  126. * @param validator The validator to use to check legal values
  127. * @since 0.6.5
  128. * @return True iff the option exists and is not empty, false otherwise
  129. */
  130. public boolean hasOptionString(final String domain, final String option,
  131. final Validator<String> validator) {
  132. String value;
  133. return hasOption(domain, option, validator)
  134. && !(value = getOption(domain, option, validator)).isEmpty()
  135. && !value.startsWith("false:");
  136. }
  137. /**
  138. * Determines if this source has the specified integer option.
  139. *
  140. * @param domain The domain of the option
  141. * @param option The name of the option
  142. * @since 0.6.3m1
  143. * @return True iff the option exists and is parsable as an integer,
  144. * false otherwise.
  145. */
  146. public boolean hasOptionInt(final String domain, final String option) {
  147. return hasOptionString(domain, option, INT_VALIDATOR);
  148. }
  149. /**
  150. * Determines if this source has the specified character option.
  151. *
  152. * @param domain The domain of the option
  153. * @param option The name of the option
  154. * @since 0.6.3m1
  155. * @return True iff the option exists and is parsable as a char,
  156. * false otherwise.
  157. */
  158. public boolean hasOptionChar(final String domain, final String option) {
  159. return hasOptionString(domain, option);
  160. }
  161. /**
  162. * Determines if this source has the specified colour option.
  163. *
  164. * @param domain The domain of the option
  165. * @param option The name of the option
  166. * @since 0.6.3m1
  167. * @return True iff the option exists and is parsable as a colour,
  168. * false otherwise.
  169. */
  170. public boolean hasOptionColour(final String domain, final String option) {
  171. return hasOptionString(domain, option, COLOUR_VALIDATOR);
  172. }
  173. /**
  174. * Retrieves a boolean representation of the specified option.
  175. *
  176. * @param domain The domain of the option
  177. * @param option The name of the option
  178. * @return The boolean representation of the option
  179. */
  180. public boolean hasOptionBool(final String domain, final String option) {
  181. return hasOption(domain, option, PERMISSIVE_VALIDATOR);
  182. }
  183. /**
  184. * Retrieves the specified option as a String, if it exists and satisfies
  185. * the specified validator.
  186. * <p>
  187. * If no fallback settings are supplied (or if fallback settings are
  188. * supplied and execution reaches the last fallback setting) AND if the
  189. * 'required' parameter is true, then all disabled values (i.e., those
  190. * starting with <code>false:</code>) will be ignored. To check if a valid
  191. * non-disabled value exists, call
  192. * {@link #hasOptionString(java.lang.String, java.lang.String, com.dmdirc.util.validators.Validator)}.
  193. *
  194. * @param domain The domain of the option
  195. * @param option The name of the option
  196. * @param required Whether the specified option is required or not
  197. * @param validator The validator to use to check the value
  198. * @param fallbacks An ordered array of further domains and options
  199. * (in pairs) to try if the specified domain/option isn't found
  200. * @return The string representation of the option or null if optional
  201. * setting is not specified
  202. * @since 0.6.5
  203. */
  204. public String getOptionString(final String domain, final String option,
  205. final boolean required, final Validator<String> validator,
  206. final String ... fallbacks) {
  207. String value;
  208. @SuppressWarnings("unchecked")
  209. final Validator<String> newValidator = required && fallbacks.length == 0
  210. ? new ValidatorChain<String>(new DisabledOptionValidator(), validator)
  211. : validator;
  212. if (!hasOption(domain, option, newValidator)
  213. || (value = getOption(domain, option, newValidator)).startsWith("false:")) {
  214. return fallbacks.length >= 2 ? getOptionString(fallbacks[0],
  215. fallbacks[1], required, validator,
  216. Arrays.copyOfRange(fallbacks, 2, fallbacks.length)) : null;
  217. } else if (value.startsWith("true:")) {
  218. return value.substring(5);
  219. } else {
  220. return value;
  221. }
  222. }
  223. /**
  224. * Retrieves the specified option as a String, if it exists.
  225. *
  226. * @param domain The domain of the option
  227. * @param option The name of the option
  228. * @param fallbacks An ordered array of further domains and options
  229. * (in pairs) to try if the specified domain/option isn't found
  230. * @return The string representation of the option or null if optional
  231. * setting is not specified
  232. * @since 0.6.3
  233. */
  234. public String getOptionString(final String domain, final String option,
  235. final String ... fallbacks) {
  236. return getOptionString(domain, option, true, PERMISSIVE_VALIDATOR, fallbacks);
  237. }
  238. /**
  239. * Retrieves the specified option as a character.
  240. *
  241. * @param domain The domain of the option
  242. * @param option The name of the option
  243. * @return The value of the option
  244. */
  245. public char getOptionChar(final String domain, final String option) {
  246. return getOption(domain, option).charAt(0);
  247. }
  248. /**
  249. * Retrieves a colour representation of the specified option.
  250. *
  251. * @param domain The domain of the option
  252. * @param option The name of the option
  253. * @param fallbacks An ordered array of further domains and options
  254. * (in pairs) to try if the specified domain/option isn't found
  255. * @return The colour representation of the option
  256. * @since 0.6.3m1
  257. */
  258. public Color getOptionColour(final String domain, final String option,
  259. final String ... fallbacks) {
  260. final String value = getOptionString(domain, option, true, COLOUR_VALIDATOR, fallbacks);
  261. return value == null ? null : ColourManager.parseColour(value, null);
  262. }
  263. /**
  264. * Retrieves a boolean representation of the specified option.
  265. *
  266. * @param domain The domain of the option
  267. * @param option The name of the option
  268. * @return The boolean representation of the option
  269. */
  270. public boolean getOptionBool(final String domain, final String option) {
  271. return Boolean.parseBoolean(getOption(domain, option));
  272. }
  273. /**
  274. * Retrieves a list representation of the specified option.
  275. *
  276. * @param domain The domain of the option
  277. * @param option The name of the option
  278. * @param trimEmpty Whether or not to trim empty lines
  279. * @return The list representation of the option
  280. */
  281. public List<String> getOptionList(final String domain, final String option,
  282. final boolean trimEmpty) {
  283. final List<String> res = new ArrayList<String>();
  284. if (hasOption(domain, option, PERMISSIVE_VALIDATOR)) {
  285. for (String line : getOption(domain, option).split("\n")) {
  286. if (!line.isEmpty() || !trimEmpty) {
  287. res.add(line);
  288. }
  289. }
  290. }
  291. return res;
  292. }
  293. /**
  294. * Retrieves a list representation of the specified option, trimming empty
  295. * lines by default.
  296. *
  297. * @param domain The domain of the option
  298. * @param option The name of the option
  299. * @return The list representation of the option
  300. */
  301. public List<String> getOptionList(final String domain, final String option) {
  302. return getOptionList(domain, option, true);
  303. }
  304. /**
  305. * Retrieves an integral representation of the specified option.
  306. *
  307. * @param domain The domain of the option
  308. * @param option The name of the option
  309. * @param fallbacks An ordered array of further domains and options
  310. * (in pairs) to try if the specified domain/option isn't found
  311. * @return The integer representation of the option
  312. */
  313. public Integer getOptionInt(final String domain, final String option,
  314. final String ... fallbacks) {
  315. return getOptionInt(domain, option, true, fallbacks);
  316. }
  317. /**
  318. * Retrieves an integral representation of the specified option.
  319. *
  320. * @since 0.6.5
  321. * @param domain The domain of the option
  322. * @param option The name of the option
  323. * @param required Whether this option is required or optional
  324. * @param fallbacks An ordered array of further domains and options
  325. * (in pairs) to try if the specified domain/option isn't found
  326. * @return The integer representation of the option
  327. */
  328. public Integer getOptionInt(final String domain, final String option,
  329. final boolean required, final String ... fallbacks) {
  330. final String value = getOptionString(domain, option, required, INT_VALIDATOR, fallbacks);
  331. return value == null ? null : Integer.parseInt(value.trim());
  332. }
  333. }