2 // System.Web.Configuration.CompilerCollection
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections.Generic;
35 using System.Configuration;
37 namespace System.CodeDom.Compiler
39 [ConfigurationCollection (typeof (Compiler), AddItemName = "compiler", CollectionType = ConfigurationElementCollectionType.BasicMap)]
40 internal sealed class CompilerCollection : ConfigurationElementCollection
42 static readonly string defaultCompilerVersion = "3.5";
43 static ConfigurationPropertyCollection properties;
44 static List <CompilerInfo> compiler_infos;
45 static Dictionary <string, CompilerInfo> compiler_languages;
46 static Dictionary <string, CompilerInfo> compiler_extensions;
48 static CompilerCollection ()
50 properties = new ConfigurationPropertyCollection ();
51 compiler_infos = new List <CompilerInfo> ();
52 compiler_languages = new Dictionary <string, CompilerInfo> (16, StringComparer.OrdinalIgnoreCase);
53 compiler_extensions = new Dictionary <string, CompilerInfo> (6, StringComparer.OrdinalIgnoreCase);
55 CompilerInfo compiler = new CompilerInfo ();
56 compiler.Languages = "c#;cs;csharp";
57 compiler.Extensions = ".cs";
58 compiler.TypeName = "Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
59 compiler.ProviderOptions = new Dictionary <string, string> (1);
60 compiler.ProviderOptions ["CompilerVersion"] = defaultCompilerVersion;
61 AddCompilerInfo (compiler);
63 compiler = new CompilerInfo ();
64 compiler.Languages = "vb;vbs;visualbasic;vbscript";
65 compiler.Extensions = ".vb";
66 compiler.TypeName = "Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
67 compiler.ProviderOptions = new Dictionary <string, string> (1);
68 compiler.ProviderOptions ["CompilerVersion"] = defaultCompilerVersion;
69 AddCompilerInfo (compiler);
71 compiler = new CompilerInfo ();
72 compiler.Languages = "js;jscript;javascript";
73 compiler.Extensions = ".js";
74 compiler.TypeName = "Microsoft.JScript.JScriptCodeProvider, Microsoft.JScript, Version=8.0.1100.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
75 compiler.ProviderOptions = new Dictionary <string, string> (1);
76 compiler.ProviderOptions ["CompilerVersion"] = defaultCompilerVersion;
77 AddCompilerInfo (compiler);
79 compiler = new CompilerInfo ();
80 compiler.Languages = "vj#;vjs;vjsharp";
81 compiler.Extensions = ".jsl;.java";
82 compiler.TypeName = "Microsoft.VJSharp.VJSharpCodeProvider, VJSharpCodeProvider, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
83 compiler.ProviderOptions = new Dictionary <string, string> (1);
84 compiler.ProviderOptions ["CompilerVersion"] = defaultCompilerVersion;
85 AddCompilerInfo (compiler);
87 compiler = new CompilerInfo ();
88 compiler.Languages = "c++;mc;cpp";
89 compiler.Extensions = ".h";
90 compiler.TypeName = "Microsoft.VisualC.CppCodeProvider, CppCodeProvider, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
91 compiler.ProviderOptions = new Dictionary <string, string> (1);
92 compiler.ProviderOptions ["CompilerVersion"] = defaultCompilerVersion;
93 AddCompilerInfo (compiler);
96 public CompilerCollection ()
100 static void AddCompilerInfo (CompilerInfo ci)
103 compiler_infos.Add (ci);
105 string[] languages = ci.GetLanguages ();
106 if (languages != null)
107 foreach (string language in languages)
108 compiler_languages [language] = ci;
110 string[] extensions = ci.GetExtensions ();
111 if (extensions != null)
112 foreach (string extension in extensions)
113 compiler_extensions [extension] = ci;
116 static void AddCompilerInfo (Compiler compiler)
118 CompilerInfo ci = new CompilerInfo ();
119 ci.Languages = compiler.Language;
120 ci.Extensions = compiler.Extension;
121 ci.TypeName = compiler.Type;
122 ci.ProviderOptions = compiler.ProviderOptionsDictionary;
123 ci.CompilerOptions = compiler.CompilerOptions;
124 ci.WarningLevel = compiler.WarningLevel;
125 AddCompilerInfo (ci);
128 protected override void BaseAdd (ConfigurationElement element)
130 Compiler compiler = element as Compiler;
131 if (compiler != null)
132 AddCompilerInfo (compiler);
133 base.BaseAdd (element);
136 protected override bool ThrowOnDuplicate {
137 get { return false; }
140 protected override ConfigurationElement CreateNewElement ()
142 return new Compiler ();
145 public CompilerInfo GetCompilerInfoForLanguage (string language)
147 if (compiler_languages.Count == 0)
151 if (compiler_languages.TryGetValue (language, out ci))
157 public CompilerInfo GetCompilerInfoForExtension (string extension)
159 if (compiler_extensions.Count == 0)
163 if (compiler_extensions.TryGetValue (extension, out ci))
169 public string GetLanguageFromExtension (string extension)
171 CompilerInfo ci = GetCompilerInfoForExtension (extension);
174 string[] languages = ci.GetLanguages ();
175 if (languages != null && languages.Length > 0)
176 return languages [0];
180 public Compiler Get (int index)
182 return (Compiler) BaseGet (index);
185 public Compiler Get (string language)
187 return (Compiler) BaseGet (language);
190 protected override object GetElementKey (ConfigurationElement element)
192 return ((Compiler)element).Language;
195 public string GetKey (int index)
197 return (string)BaseGetKey (index);
200 public string[ ] AllKeys {
202 string[] keys = new string[compiler_infos.Count];
203 for (int i = 0; i < Count; i++)
204 keys[i] = compiler_infos[i].Languages;
209 public override ConfigurationElementCollectionType CollectionType {
210 get { return ConfigurationElementCollectionType.BasicMap; }
213 protected override string ElementName {
214 get { return "compiler"; }
217 protected override ConfigurationPropertyCollection Properties {
218 get { return properties; }
221 public Compiler this[int index] {
222 get { return (Compiler) BaseGet (index); }
225 public new CompilerInfo this[string language] {
227 return GetCompilerInfoForLanguage (language);
231 public CompilerInfo[] CompilerInfos {
233 return compiler_infos.ToArray ();