/src/NUnit/UiException/CodeFormatters/GeneralCodeFormatter.cs
C# | 168 lines | 71 code | 23 blank | 74 comment | 5 complexity | 20291a975c81a079956bb6069b4130ec 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 NUnit.UiException.CodeFormatters; 10 11namespace NUnit.UiException.CodeFormatters 12{ 13 /// <summary> 14 /// GeneralCodeFormatter is the front class responsible for making a basic 15 /// syntax coloring of a string of text for a set of specific languages. 16 /// The class encapsulates a set of algorithms where each of them addresses 17 /// a specific language formatting. The use of one or another algorithm at 18 /// analysing time is made through an language registering mechanism. 19 /// For instance C# files are covered by CSharpCodeFormatter which has 20 /// been assigned to "cs" language. 21 /// If a query is made to GeneralCodeFormatter while there is no formatter 22 /// that fit the given file language a default formatting is applyied 23 /// through the use of the formatter registered into the property 24 /// DefaultFormatter. 25 /// </summary> 26 public class GeneralCodeFormatter : 27 IFormatterCatalog 28 { 29 /// <summary> 30 /// The set of formatting algorithms. 31 /// </summary> 32 private CodeFormatterCollection _formatters; 33 34 /// <summary> 35 /// The default formatter to be used as last resort. 36 /// </summary> 37 private ICodeFormatter _default; 38 39 /// <summary> 40 /// Build and initialises GeneralCodeFormatter. 41 /// </summary> 42 public GeneralCodeFormatter() 43 { 44 _formatters = new CodeFormatterCollection(); 45 _formatters.Register(new CSharpCodeFormatter(), "cs"); 46 47 _default = new PlainTextCodeFormatter(); 48 49 return; 50 } 51 52 /// <summary> 53 /// Gets or sets the formatter to be used as last resort when 54 /// no formatter fit the given source language. 55 /// The value cannot be null. 56 /// </summary> 57 public ICodeFormatter DefaultFormatter 58 { 59 get { return (_default); } 60 set 61 { 62 UiExceptionHelper.CheckNotNull(value, "value"); 63 _default = value; 64 } 65 } 66 67 /// <summary> 68 /// Gives access to the underlying formatter collection. 69 /// </summary> 70 public CodeFormatterCollection Formatters 71 { 72 get { return (_formatters); } 73 } 74 75 /// <summary> 76 /// Gets the best formatter that fits the given language. If there 77 /// is no such formatter, a default one is returned. 78 /// </summary> 79 /// <param name="language"> 80 /// The language name. Ex: "C#", "Java. This parameter cannot be null. 81 /// </param> 82 /// <returns> 83 /// A non-null ICodeFormatter that best fits the request. 84 /// </returns> 85 public ICodeFormatter GetFormatterFromLanguage(string languageName) 86 { 87 UiExceptionHelper.CheckNotNull(languageName, "language"); 88 89 if (_formatters.HasLanguage(languageName)) 90 return (_formatters[languageName]); 91 92 return (DefaultFormatter); 93 } 94 95 /// <summary> 96 /// Gets the formatter assigned to the given extension. If there 97 /// is no such assignment, the default formatter is returned. 98 /// </summary> 99 /// <param name="extension"> 100 /// A file extension. Ex: "cs", "txt". This parameter cannot be null. 101 /// </param> 102 /// <returns>A non-null ICodeFormatter.</returns> 103 public ICodeFormatter GetFormatterFromExtension(string extension) 104 { 105 UiExceptionHelper.CheckNotNull(extension, "extension"); 106 107 if (_formatters.HasExtension(extension)) 108 return (_formatters.GetFromExtension(extension)); 109 110 return (DefaultFormatter); 111 } 112 113 /// <summary> 114 /// A convenient method to make the formatting of a piece of code when 115 /// only the file extension is known. 116 /// </summary> 117 /// <param name="code">The piece of code to be formatted. This parameter 118 /// cannot be null.</param> 119 /// <param name="extension">The file extension associated to this piece of code. 120 /// Ex: "cs", "cpp". This is used to pick the formatter assigned to. If no such 121 /// formatter exists, the default one is picked up.</param> 122 /// <returns>The FormattedCode for this piece of code.</returns> 123 public FormattedCode FormatFromExtension(string code, string extension) 124 { 125 UiExceptionHelper.CheckNotNull(code, "code"); 126 UiExceptionHelper.CheckNotNull(extension, "extension"); 127 128 if (_formatters.HasExtension(extension)) 129 return (_formatters.GetFromExtension(extension).Format(code)); 130 131 return (DefaultFormatter.Format(code)); 132 } 133 134 #region IFormatterCatalog Membres 135 136 /// <summary> 137 /// Pick the best available formatter to format the given piece of code. 138 /// </summary> 139 /// <param name="code">The code to be formatted. This parameter cannot be null.</param> 140 /// <param name="language"> 141 /// The language into which code has been written. Ex: "C#", "Java". 142 /// If no such formatter is available, a default formatting is applied. 143 /// This parameter cannot be null. 144 /// </param> 145 /// <returns> 146 /// The formatting for this piece of code. 147 /// </returns> 148 public FormattedCode Format(string code, string language) 149 { 150 UiExceptionHelper.CheckNotNull(code, "code"); 151 UiExceptionHelper.CheckNotNull(language, "language"); 152 153 if (_formatters.HasLanguage(language)) 154 return (_formatters[language].Format(code)); 155 156 return (DefaultFormatter.Format(code)); 157 } 158 159 public string LanguageFromExtension(string extension) 160 { 161 if (_formatters.HasExtension(extension)) 162 return (_formatters.GetFromExtension(extension).Language); 163 return (_default.Language); 164 } 165 166 #endregion 167 } 168}