PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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