PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/Mercurial.Net/Configuration/ClientConfigurationCollection.cs

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