CodeDomProvider.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // System.CodeDom.Compiler.CodeDomProvider.cs
  3. //
  4. // Authors:
  5. // Daniel Stodden ([email protected])
  6. // Marek Safar ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. // Sebastien Pouliot <[email protected]>
  9. //
  10. // Copyright (C) 2002,2003,2004,2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.IO;
  35. using System.Runtime.InteropServices;
  36. using System.Security.Permissions;
  37. namespace System.CodeDom.Compiler {
  38. #if NET_2_0
  39. [ComVisible (true)]
  40. #endif
  41. [ToolboxItem ("")]
  42. public abstract class CodeDomProvider : Component
  43. {
  44. //
  45. // Constructors
  46. //
  47. protected CodeDomProvider()
  48. {
  49. }
  50. //
  51. // Properties
  52. //
  53. public virtual string FileExtension {
  54. get {
  55. return String.Empty;
  56. }
  57. }
  58. public virtual LanguageOptions LanguageOptions {
  59. get {
  60. return LanguageOptions.None;
  61. }
  62. }
  63. //
  64. // Methods
  65. //
  66. #if NET_2_0
  67. [Obsolete ("ICodeCompiler is obsolete")]
  68. #endif
  69. public abstract ICodeCompiler CreateCompiler();
  70. #if NET_2_0
  71. [Obsolete ("ICodeGenerator is obsolete")]
  72. #endif
  73. public abstract ICodeGenerator CreateGenerator();
  74. public virtual ICodeGenerator CreateGenerator (string fileName)
  75. {
  76. return CreateGenerator();
  77. }
  78. public virtual ICodeGenerator CreateGenerator (TextWriter output)
  79. {
  80. return CreateGenerator();
  81. }
  82. #if NET_2_0
  83. [Obsolete ("ICodeParser is obsolete")]
  84. #endif
  85. public virtual ICodeParser CreateParser()
  86. {
  87. return null;
  88. }
  89. public virtual TypeConverter GetConverter (Type type)
  90. {
  91. return TypeDescriptor.GetConverter (type);
  92. }
  93. #if NET_2_0
  94. public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
  95. {
  96. ICodeCompiler cc = CreateCompiler ();
  97. if (cc == null)
  98. throw new NotImplementedException ();
  99. return cc.CompileAssemblyFromDomBatch (options, compilationUnits);
  100. }
  101. public virtual CompilerResults CompileAssemblyFromFile (CompilerParameters options, params string[] fileNames)
  102. {
  103. ICodeCompiler cc = CreateCompiler ();
  104. if (cc == null)
  105. throw new NotImplementedException ();
  106. return cc.CompileAssemblyFromFileBatch (options, fileNames);
  107. }
  108. public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] fileNames)
  109. {
  110. ICodeCompiler cc = CreateCompiler ();
  111. if (cc == null)
  112. throw new NotImplementedException ();
  113. return cc.CompileAssemblyFromSourceBatch (options, fileNames);
  114. }
  115. public virtual string CreateEscapedIdentifier (string value)
  116. {
  117. ICodeGenerator cg = CreateGenerator ();
  118. if (cg == null)
  119. throw new NotImplementedException ();
  120. return cg.CreateEscapedIdentifier (value);
  121. }
  122. [ComVisible (false)]
  123. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  124. public static CodeDomProvider CreateProvider (string language)
  125. {
  126. CompilerInfo ci = GetCompilerInfo (language);
  127. return (ci == null) ? null : ci.CreateProvider ();
  128. }
  129. public virtual string CreateValidIdentifier (string value)
  130. {
  131. ICodeGenerator cg = CreateGenerator ();
  132. if (cg == null)
  133. throw new NotImplementedException ();
  134. return cg.CreateValidIdentifier (value);
  135. }
  136. public virtual void GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
  137. TextWriter writer, CodeGeneratorOptions options)
  138. {
  139. ICodeGenerator cg = CreateGenerator ();
  140. if (cg == null)
  141. throw new NotImplementedException ();
  142. cg.GenerateCodeFromCompileUnit (compileUnit, writer, options);
  143. }
  144. public virtual void GenerateCodeFromExpression (CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
  145. {
  146. ICodeGenerator cg = CreateGenerator ();
  147. if (cg == null)
  148. throw new NotImplementedException ();
  149. cg.GenerateCodeFromExpression (expression, writer, options);
  150. }
  151. public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
  152. {
  153. // Documented to always throw an exception (if not overriden)
  154. throw new NotImplementedException ();
  155. // Note: the pattern is different from other GenerateCodeFrom* because
  156. // ICodeGenerator doesn't have a GenerateCodeFromMember member
  157. }
  158. public virtual void GenerateCodeFromNamespace (CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
  159. {
  160. ICodeGenerator cg = CreateGenerator ();
  161. if (cg == null)
  162. throw new NotImplementedException ();
  163. cg.GenerateCodeFromNamespace (codeNamespace, writer, options);
  164. }
  165. public virtual void GenerateCodeFromStatement (CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
  166. {
  167. ICodeGenerator cg = CreateGenerator ();
  168. if (cg == null)
  169. throw new NotImplementedException ();
  170. cg.GenerateCodeFromStatement (statement, writer, options);
  171. }
  172. public virtual void GenerateCodeFromType (CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
  173. {
  174. ICodeGenerator cg = CreateGenerator ();
  175. if (cg == null)
  176. throw new NotImplementedException ();
  177. cg.GenerateCodeFromType (codeType, writer, options);
  178. }
  179. [ComVisible (false)]
  180. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  181. public static CompilerInfo[] GetAllCompilerInfo ()
  182. {
  183. int n = 0;
  184. if ((Config != null) && (Config.Compilers != null))
  185. n = Config.Compilers.Hash.Count;
  186. CompilerInfo[] ci = new CompilerInfo [n];
  187. if (n > 0)
  188. Config.Compilers.Hash.Values.CopyTo (ci, 0);
  189. return ci;
  190. }
  191. [ComVisible (false)]
  192. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  193. public static CompilerInfo GetCompilerInfo (string language)
  194. {
  195. if (language == null)
  196. throw new ArgumentNullException ("language");
  197. return (Config == null) ? null : Config.GetCompilerInfo (language);
  198. }
  199. [ComVisible (false)]
  200. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  201. public static string GetLanguageFromExtension (string extension)
  202. {
  203. if (extension == null)
  204. throw new ArgumentNullException ("extension");
  205. if (Config != null) {
  206. foreach (DictionaryEntry de in Config.Compilers.Hash) {
  207. CompilerInfo c = (CompilerInfo) de.Value;
  208. if (Array.IndexOf (c.GetExtensions (), extension) != -1)
  209. return (string) de.Key;
  210. }
  211. }
  212. return null;
  213. }
  214. public virtual string GetTypeOutput (CodeTypeReference type)
  215. {
  216. ICodeGenerator cg = CreateGenerator ();
  217. if (cg == null)
  218. throw new NotImplementedException ();
  219. return cg.GetTypeOutput (type);
  220. }
  221. [ComVisible (false)]
  222. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  223. public static bool IsDefinedExtension (string extension)
  224. {
  225. if (extension == null)
  226. throw new ArgumentNullException ("extension");
  227. if (Config != null) {
  228. foreach (CompilerInfo c in Config.Compilers.Hash.Values) {
  229. if (Array.IndexOf (c.GetExtensions (), extension) != -1)
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. [ComVisible (false)]
  236. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  237. public static bool IsDefinedLanguage (string language)
  238. {
  239. if (language == null)
  240. throw new ArgumentNullException ("language");
  241. if (Config == null)
  242. return false;
  243. return (Config.GetCompilerInfo (language) == null);
  244. }
  245. public virtual bool IsValidIdentifier (string value)
  246. {
  247. ICodeGenerator cg = CreateGenerator ();
  248. if (cg == null)
  249. throw new NotImplementedException ();
  250. return cg.IsValidIdentifier (value);
  251. }
  252. public virtual CodeCompileUnit Parse (TextReader codeStream)
  253. {
  254. ICodeParser cp = CreateParser ();
  255. if (cp == null)
  256. throw new NotImplementedException ();
  257. return cp.Parse (codeStream);
  258. }
  259. public virtual bool Supports (GeneratorSupport supports)
  260. {
  261. ICodeGenerator cg = CreateGenerator ();
  262. if (cg == null)
  263. throw new NotImplementedException ();
  264. return cg.Supports (supports);
  265. }
  266. static CompilationConfiguration Config {
  267. get { return ConfigurationSettings.GetConfig ("system.codedom") as CompilationConfiguration; }
  268. }
  269. #endif
  270. }
  271. }