Merge pull request #2310 from lambdageek/dev/bug-36305
[mono.git] / mcs / class / System / System.CodeDom.Compiler / CompilerInfo.cs
1 //
2 // System.CodeDom.Compiler CompilerInfo class
3 //
4 // Author:
5 //      Marek Safar (marek.safar@seznam.cz)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // Copyright (c) 2004,2005 Novell, Inc. (http://www.novell.com)
9 //
10
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
32 using System.Configuration;
33 using System.Collections.Generic;
34 using System.Reflection;
35 using System.Security.Permissions;
36
37 namespace System.CodeDom.Compiler {
38
39         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
40         public sealed class CompilerInfo
41         {
42                 internal string Languages;
43                 internal string Extensions;
44                 internal string TypeName;
45                 internal int WarningLevel;
46                 internal string CompilerOptions;
47                 internal Dictionary <string, string> ProviderOptions;
48                 
49                 bool inited;
50                 Type type;
51
52                 internal CompilerInfo ()
53                 {
54                 }
55
56                 internal void Init ()
57                 {
58                         if (inited)
59                                 return;
60
61                         inited = true;
62                         type = Type.GetType (TypeName);
63                         if (type == null)
64                                 return;
65
66                         if (!typeof (CodeDomProvider).IsAssignableFrom (type))
67                                 type = null;
68                 }
69
70                 public Type CodeDomProviderType {
71                         get {
72                                 if (type == null) {
73                                         type = Type.GetType (TypeName, false);
74 #if CONFIGURATION_DEP
75                                         if (type == null)
76                                                 throw new ConfigurationErrorsException ("Unable to locate compiler type '" + TypeName + "'");
77 #endif
78                                 }
79                                 
80                                 return type;
81                         }
82                 }
83
84                 
85                 public bool IsCodeDomProviderTypeValid {
86                         get { return type != null; }
87                 }
88
89                 public CompilerParameters CreateDefaultCompilerParameters ()
90                 {
91                         CompilerParameters cparams = new CompilerParameters ();
92                         if (CompilerOptions == null)
93                                 cparams.CompilerOptions = String.Empty;
94                         else
95                                 cparams.CompilerOptions = CompilerOptions;
96                         cparams.WarningLevel = WarningLevel;
97
98                         return cparams;
99                 }
100
101                 public CodeDomProvider CreateProvider ()
102                 {
103                         return CreateProvider (ProviderOptions);
104                 }
105
106                 public          
107                 CodeDomProvider CreateProvider (IDictionary<string, string> providerOptions)
108                 {
109                         Type providerType = CodeDomProviderType;
110                         if (providerOptions != null && providerOptions.Count > 0) {
111                                 ConstructorInfo ctor = providerType.GetConstructor (new [] { typeof (IDictionary <string, string>) });
112                                 if (ctor != null)
113                                         return (CodeDomProvider) ctor.Invoke (new object[] { providerOptions });
114                         }
115                         
116                         return (CodeDomProvider) Activator.CreateInstance (providerType);
117                 }
118
119                 public override bool Equals (object o)
120                 {
121                         if (!(o is CompilerInfo))
122                                 return false;
123
124                         CompilerInfo c = (CompilerInfo) o;
125                         return c.TypeName == TypeName;
126                 }
127
128                 public override int GetHashCode ()
129                 {
130                         return TypeName.GetHashCode ();
131                 }
132
133                 public string [] GetExtensions ()
134                 {
135                         return Extensions.Split (';');
136                 }
137
138                 public string [] GetLanguages ()
139                 {
140                         return Languages.Split (';');
141                 }
142         }
143 }
144