PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/UiException/CodeFormatters/CodeFormatterCollection.cs

#
C# | 203 lines | 99 code | 31 blank | 73 comment | 11 complexity | 6966995971bb54a6e176bac57d8b7217 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org
  4// ****************************************************************
  5
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using System.Collections;
 10using System.Collections.Specialized;
 11
 12namespace NUnit.UiException.CodeFormatters
 13{
 14    /// <summary>
 15    /// Makes the link between a file language and an ICodeFormatter.
 16    /// This class is used to know which formatter need to be call
 17    /// when displaying an ErrorItem.
 18    /// </summary>
 19    public class CodeFormatterCollection :
 20        IEnumerable
 21    {
 22        /// <summary>
 23        /// Maps language files to ICodeFormatters.
 24        /// </summary>
 25        private Dictionary<string, ICodeFormatter> _toFormatter;
 26
 27        /// <summary>
 28        /// Builds an empty CodeFormatterCollection.
 29        /// </summary>
 30        public CodeFormatterCollection()
 31        {
 32            _toFormatter = new Dictionary<string, ICodeFormatter>();
 33
 34            return;
 35        }
 36
 37        /// <summary>
 38        /// Gets the size of the collection.
 39        /// </summary>
 40        public int Count
 41        {
 42            get { return (_toFormatter.Count); }
 43        }
 44
 45        /// <summary>
 46        /// Returns the ICodeFormatter that fit the given language.
 47        /// </summary>
 48        /// <param name="language">
 49        /// A language name, such as: "C#" or "Java".
 50        /// This parameter cannot be null.
 51        /// </param>
 52        /// <returns>
 53        /// The ICodeFormatter that fit this language.
 54        /// </returns>
 55        /// <see cref="HasExtension"/>
 56        public ICodeFormatter this[string language]
 57        {
 58            get
 59            {
 60                UiExceptionHelper.CheckNotNull(language, "language");
 61
 62                foreach (ICodeFormatter item in _toFormatter.Values)
 63                    if (item.Language == language)
 64                        return (item);
 65
 66                throw new ArgumentException("unknown language: '" + language + "'");
 67            }
 68        }
 69        
 70        /// <summary>
 71        /// Checks whether there is a formatter that has been
 72        /// assigned to the given file extension.
 73        /// </summary>
 74        /// <param name="language">A file extension such as: "cs".</param>
 75        /// <returns>True if there is such formatter, false otherwise.</returns>
 76        public bool HasExtension(string extension)
 77        {
 78            if (extension == null)
 79                return (false);
 80
 81            extension = extension.ToLower();
 82
 83            return (_toFormatter.ContainsKey(extension));
 84        }
 85
 86        /// <summary>
 87        /// Tests whether the collection contains a formatter for the given language.
 88        /// </summary>
 89        /// <param name="language">
 90        /// A language name. Ex: "C#", "Java"</param>
 91        /// <returns>True if such formatter exists.</returns>
 92        public bool HasLanguage(string languageName)
 93        {
 94            if (languageName == null)
 95                return (false);
 96
 97            foreach (ICodeFormatter item in _toFormatter.Values)
 98                if (item.Language == languageName)
 99                    return (true);
100
101            return (false);
102        }
103
104        /// <summary>
105        /// Gets the ICodeFormatter that has been assigned to this extension.
106        /// </summary>
107        /// <param name="extension">The file extension. This parameter
108        /// cannot be null.</param>
109        /// <returns>The ICodeFormatter assigned to.</returns>
110        public ICodeFormatter GetFromExtension(string extension)
111        {
112            UiExceptionHelper.CheckNotNull(extension, "extension");
113            extension = extension.ToLower();
114            return (_toFormatter[extension]);
115        }
116       
117        /// <summary>
118        /// Registers an ICodeFormatter for the given language. The system
119        /// is not case sensitive.
120        /// </summary>
121        /// <param name="formatter">
122        /// A non null formatter.
123        /// </param>
124        /// <param name="language">
125        /// A non null file language.
126        /// The value must not be empty nor contain '.' and
127        /// must not have been already registered.
128        /// </param>
129        public void Register(ICodeFormatter formatter, string extension)
130        {
131            UiExceptionHelper.CheckNotNull(formatter, "formatter");
132            UiExceptionHelper.CheckNotNull(extension, "language");
133
134            extension = extension.ToLower();
135
136            UiExceptionHelper.CheckTrue(extension.Length > 0,
137                "language cannot be empty", "language");
138            UiExceptionHelper.CheckTrue(extension.LastIndexOf('.') == -1,
139                "language cannot contain '.'", "language");
140            UiExceptionHelper.CheckFalse(_toFormatter.ContainsKey(extension),
141                "language '" + extension + "' has already an handler. Remove handler first.",
142                "language");
143
144            _toFormatter.Add(extension, formatter);
145
146            return;
147        }
148
149        /// <summary>
150        /// Removes the formatter for the given file language.
151        /// The language is not case sensitive.
152        /// </summary>
153        /// <param name="language">A file language.</param>
154        public void Remove(string extension)
155        {
156            if (extension == null)
157                return;
158
159            extension = extension.ToLower();
160
161            _toFormatter.Remove(extension);
162
163            return;
164        }
165
166        /// <summary>
167        /// Removes all formatters.
168        /// </summary>
169        public void Clear()
170        {
171            _toFormatter.Clear();
172        }
173
174        /// <summary>
175        /// Returns a string collection with all registered extensions.
176        /// </summary>
177        public StringCollection Extensions
178        {
179            get
180            {
181                StringCollection res;
182
183                res = new StringCollection();
184                foreach (string extension in _toFormatter.Keys)
185                    res.Add(extension);
186
187                return (res);
188            }
189        }
190
191        #region IEnumerable Membres
192
193        /// <summary>
194        /// Returns an IEnumerator on all registered ICodeFormatter.
195        /// </summary>
196        public IEnumerator GetEnumerator()
197        {
198            return (_toFormatter.Values.GetEnumerator());
199        }
200
201        #endregion
202    }
203}