PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net/Configuration/ClientConfigurationCollection.cs

#
C# | 199 lines | 103 code | 17 blank | 79 comment | 11 complexity | b81f5605a22456d4133d57da225112dc MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Mercurial.Configuration
  6. {
  7. /// <summary>
  8. /// This class encapsulates the Mercurial command line client configuration information.
  9. /// </summary>
  10. public sealed class ClientConfigurationCollection : IEnumerable<ConfigurationEntry>
  11. {
  12. /// <summary>
  13. /// This is the backing field for this <see cref="ClientConfigurationCollection"/>.
  14. /// </summary>
  15. private readonly Dictionary<string, Dictionary<string, string>> _Configuration = new Dictionary<string, Dictionary<string, string>>();
  16. /// <summary>
  17. /// Gets a collection of section names from the configuration.
  18. /// </summary>
  19. public IEnumerable<string> Sections
  20. {
  21. get
  22. {
  23. lock (_Configuration)
  24. return _Configuration.Keys.ToArray();
  25. }
  26. }
  27. #region IEnumerable<ConfigurationEntry> Members
  28. /// <summary>
  29. /// Returns an enumerator that iterates through the collection.
  30. /// </summary>
  31. /// <returns>
  32. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  33. /// </returns>
  34. /// <filterpriority>1</filterpriority>
  35. public IEnumerator<ConfigurationEntry> GetEnumerator()
  36. {
  37. ConfigurationEntry[] result;
  38. lock (_Configuration)
  39. {
  40. result = (from kvp1 in _Configuration
  41. from kvp2 in kvp1.Value
  42. select new ConfigurationEntry(kvp1.Key, kvp2.Key, kvp2.Value)).ToArray();
  43. }
  44. return ((IEnumerable<ConfigurationEntry>)result).GetEnumerator();
  45. }
  46. /// <summary>
  47. /// Returns an enumerator that iterates through a collection.
  48. /// </summary>
  49. /// <returns>
  50. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  51. /// </returns>
  52. /// <filterpriority>2</filterpriority>
  53. IEnumerator IEnumerable.GetEnumerator()
  54. {
  55. return GetEnumerator();
  56. }
  57. #endregion
  58. /// <summary>
  59. /// Gets the value of the specified configuration entry from
  60. /// the specified section.
  61. /// </summary>
  62. /// <param name="sectionName">
  63. /// The name of the section to get the value of a configuration entry from.
  64. /// </param>
  65. /// <param name="name">
  66. /// The name of the configuration entry to retrieve the value for.
  67. /// </param>
  68. /// <returns>
  69. /// The value of the configuration entry, or <see cref="string.Empty"/>
  70. /// if no such value exists.
  71. /// </returns>
  72. /// <exception cref="ArgumentNullException">
  73. /// <para><paramref name="sectionName"/> is <c>null</c> or empty.</para>
  74. /// <para>- or -</para>
  75. /// <para><paramref name="name"/> is <c>null</c> or empty.</para>
  76. /// </exception>
  77. public string GetValue(string sectionName, string name)
  78. {
  79. if (StringEx.IsNullOrWhiteSpace(sectionName))
  80. throw new ArgumentNullException("sectionName");
  81. if (StringEx.IsNullOrWhiteSpace(name))
  82. throw new ArgumentNullException("name");
  83. Dictionary<string, string> section;
  84. lock (_Configuration)
  85. {
  86. if (_Configuration.TryGetValue(sectionName, out section))
  87. {
  88. string value;
  89. if (section.TryGetValue(name, out value))
  90. return value;
  91. }
  92. }
  93. return string.Empty;
  94. }
  95. /// <summary>
  96. /// Refreshes the client configuration information by calling the Mercurial command line
  97. /// client and asking it to report the current configuration.
  98. /// </summary>
  99. public void Refresh()
  100. {
  101. lock (_Configuration)
  102. {
  103. _Configuration.Clear();
  104. var command = new ShowConfigCommand();
  105. NonPersistentClient.Execute(command);
  106. foreach (ConfigurationEntry entry in command.Result.ToArray())
  107. {
  108. Dictionary<string, string> section;
  109. if (!_Configuration.TryGetValue(entry.Section, out section))
  110. {
  111. section = new Dictionary<string, string>();
  112. _Configuration[entry.Section] = section;
  113. }
  114. section[entry.Name] = entry.Value;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Gets a collection of configuration entry names for the given
  120. /// section, or an empty collection if no such section exists.
  121. /// </summary>
  122. /// <param name="sectionName">
  123. /// The name of the section to get configuration entry names for.
  124. /// </param>
  125. /// <returns>
  126. /// A collection of configuration entry names for the given section.
  127. /// </returns>
  128. /// <exception cref="ArgumentNullException">
  129. /// <para><paramref name="sectionName"/> is <c>null</c> or empty.</para>
  130. /// </exception>
  131. public IEnumerable<string> GetNamesForSection(string sectionName)
  132. {
  133. if (StringEx.IsNullOrWhiteSpace(sectionName))
  134. throw new ArgumentNullException("sectionName");
  135. Dictionary<string, string> section;
  136. lock (_Configuration)
  137. {
  138. if (_Configuration.TryGetValue(sectionName, out section))
  139. return section.Keys.ToArray();
  140. }
  141. return new string[0];
  142. }
  143. /// <summary>
  144. /// Gets whether a specific value exists in the configuration file. Note that the value
  145. /// of the configuration entry can be empty (ie. <see cref="string.Empty"/>), all this method
  146. /// checks is whether there was a line with "section.name=" present.
  147. /// </summary>
  148. /// <param name="sectionName">
  149. /// The name of the section to get the value of a configuration entry from.
  150. /// </param>
  151. /// <param name="name">
  152. /// The name of the configuration entry to retrieve the value for.
  153. /// </param>
  154. /// <returns>
  155. /// <c>true</c> if the value was specified;
  156. /// otherwise, <c>false</c>.
  157. /// </returns>
  158. /// <exception cref="ArgumentNullException">
  159. /// <para><paramref name="sectionName"/> is <c>null</c> or empty.</para>
  160. /// <para>- or -</para>
  161. /// <para><paramref name="name"/> is <c>null</c> or empty.</para>
  162. /// </exception>
  163. public bool ValueExists(string sectionName, string name)
  164. {
  165. if (StringEx.IsNullOrWhiteSpace(sectionName))
  166. throw new ArgumentNullException("sectionName");
  167. if (StringEx.IsNullOrWhiteSpace(name))
  168. throw new ArgumentNullException("name");
  169. Dictionary<string, string> section;
  170. lock (_Configuration)
  171. {
  172. if (_Configuration.TryGetValue(sectionName, out section))
  173. {
  174. string value;
  175. if (section.TryGetValue(name, out value))
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. }
  182. }