Merge pull request #2310 from lambdageek/dev/bug-36305
[mono.git] / mcs / class / System / System.CodeDom.Compiler / CompilerCollection.cs
1 //
2 // System.Web.Configuration.CompilerCollection
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if CONFIGURATION_DEP
32
33 using System;
34 using System.Collections.Generic;
35 using System.Configuration;
36
37 namespace System.CodeDom.Compiler
38 {
39         [ConfigurationCollection (typeof (Compiler), AddItemName = "compiler", CollectionType = ConfigurationElementCollectionType.BasicMap)]
40         internal sealed class CompilerCollection : ConfigurationElementCollection
41         {
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;
47                 
48                 static CompilerCollection ()
49                 {
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);
54                                 
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);
62
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);
70
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);
78
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);
86
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);
94                 }
95
96                 public CompilerCollection ()
97                 {
98                 }
99
100                 static void AddCompilerInfo (CompilerInfo ci)
101                 {
102                         ci.Init ();
103                         compiler_infos.Add (ci);
104
105                         string[] languages = ci.GetLanguages ();
106                         if (languages != null)
107                                 foreach (string language in languages)
108                                         compiler_languages [language] = ci;
109                         
110                         string[] extensions = ci.GetExtensions ();
111                         if (extensions != null)
112                                 foreach (string extension in extensions)
113                                         compiler_extensions [extension] = ci;
114                 }
115
116                 static void AddCompilerInfo (Compiler compiler)
117                 {
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);
126                 }
127                 
128                 protected override void BaseAdd (ConfigurationElement element)
129                 {
130                         Compiler compiler = element as Compiler;
131                         if (compiler != null)
132                                 AddCompilerInfo (compiler);
133                         base.BaseAdd (element);
134                 }
135                 
136                 protected override bool ThrowOnDuplicate {
137                         get { return false; }
138                 }
139                 
140                 protected override ConfigurationElement CreateNewElement ()
141                 {
142                         return new Compiler ();
143                 }
144
145                 public CompilerInfo GetCompilerInfoForLanguage (string language)
146                 {
147                         if (compiler_languages.Count == 0)
148                                 return null;
149                         
150                         CompilerInfo ci;
151                         if (compiler_languages.TryGetValue (language, out ci))
152                                 return ci;
153                         
154                         return null;
155                 }
156
157                 public CompilerInfo GetCompilerInfoForExtension (string extension)
158                 {
159                         if (compiler_extensions.Count == 0)
160                                 return null;
161                         
162                         CompilerInfo ci;
163                         if (compiler_extensions.TryGetValue (extension, out ci))
164                                 return ci;
165                         
166                         return null;
167                 }
168
169                 public string GetLanguageFromExtension (string extension)
170                 {
171                         CompilerInfo ci = GetCompilerInfoForExtension (extension);
172                         if (ci == null)
173                                 return null;
174                         string[] languages = ci.GetLanguages ();
175                         if (languages != null && languages.Length > 0)
176                                 return languages [0];
177                         return null;
178                 }
179                 
180                 public Compiler Get (int index)
181                 {
182                         return (Compiler) BaseGet (index);
183                 }
184
185                 public Compiler Get (string language)
186                 {
187                         return (Compiler) BaseGet (language);
188                 }
189
190                 protected override object GetElementKey (ConfigurationElement element)
191                 {
192                         return ((Compiler)element).Language;
193                 }
194
195                 public string GetKey (int index)
196                 {
197                         return (string)BaseGetKey (index);
198                 }
199
200                 public string[ ] AllKeys {
201                         get {
202                                 string[] keys = new string[compiler_infos.Count];
203                                 for (int i = 0; i < Count; i++)
204                                         keys[i] = compiler_infos[i].Languages;
205                                 return keys;
206                         }
207                 }
208                 
209                 public override ConfigurationElementCollectionType CollectionType {
210                         get { return ConfigurationElementCollectionType.BasicMap; }
211                 }
212
213                 protected override string ElementName {
214                         get { return "compiler"; }
215                 }
216
217                 protected override ConfigurationPropertyCollection Properties {
218                         get { return properties; }
219                 }
220
221                 public Compiler this[int index] {
222                         get { return (Compiler) BaseGet (index); }
223                 }
224
225                 public new CompilerInfo this[string language] {
226                         get {
227                                 return GetCompilerInfoForLanguage (language);
228                         }
229                 }
230
231                 public CompilerInfo[] CompilerInfos {
232                         get {
233                                 return compiler_infos.ToArray ();
234                         }
235                 }
236         }
237 }
238 #endif