New tests.
[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 #if NET_2_0
33
34 using System.Configuration;
35 using System.Collections.Generic;
36 using System.Reflection;
37 using System.Security.Permissions;
38
39 namespace System.CodeDom.Compiler {
40
41         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
42         public sealed class CompilerInfo
43         {
44                 internal string Languages;
45                 internal string Extensions;
46                 internal string TypeName;
47                 internal int WarningLevel;
48                 internal string CompilerOptions;
49                 internal Dictionary <string, string> ProviderOptions;
50                 
51                 bool inited;
52                 Type type;
53
54                 internal CompilerInfo ()
55                 {
56                 }
57
58                 internal void Init ()
59                 {
60                         if (inited)
61                                 return;
62
63                         inited = true;
64                         type = Type.GetType (TypeName);
65                         if (type == null)
66                                 return;
67
68                         if (!typeof (CodeDomProvider).IsAssignableFrom (type))
69                                 type = null;
70                 }
71
72                 public Type CodeDomProviderType {
73                         get {
74                                 if (type == null) {
75                                         type = Type.GetType (TypeName, false);
76 #if CONFIGURATION_DEP
77                                         if (type == null)
78                                                 throw new ConfigurationErrorsException ("Unable to locate compiler type '" + TypeName + "'");
79 #endif
80                                 }
81                                 
82                                 return type;
83                         }
84                 }
85
86                 
87                 public bool IsCodeDomProviderTypeValid {
88                         get { return type != null; }
89                 }
90
91                 public CompilerParameters CreateDefaultCompilerParameters ()
92                 {
93                         CompilerParameters cparams = new CompilerParameters ();
94                         if (CompilerOptions == null)
95                                 cparams.CompilerOptions = String.Empty;
96                         else
97                                 cparams.CompilerOptions = CompilerOptions;
98                         cparams.WarningLevel = WarningLevel;
99
100                         return cparams;
101                 }
102
103                 public CodeDomProvider CreateProvider ()
104                 {
105                         return CreateProvider (ProviderOptions);
106                 }
107
108 #if NET_4_0
109                 public          
110 #endif
111                 CodeDomProvider CreateProvider (IDictionary<string, string> providerOptions)
112                 {
113                         Type providerType = CodeDomProviderType;
114                         if (providerOptions != null && providerOptions.Count > 0) {
115                                 ConstructorInfo ctor = providerType.GetConstructor (new [] { typeof (IDictionary <string, string>) });
116                                 if (ctor != null)
117                                         return (CodeDomProvider) ctor.Invoke (new object[] { providerOptions });
118                         }
119                         
120                         return (CodeDomProvider) Activator.CreateInstance (providerType);
121                 }
122
123                 public override bool Equals (object o)
124                 {
125                         if (!(o is CompilerInfo))
126                                 return false;
127
128                         CompilerInfo c = (CompilerInfo) o;
129                         return c.TypeName == TypeName;
130                 }
131
132                 public override int GetHashCode ()
133                 {
134                         return TypeName.GetHashCode ();
135                 }
136
137                 public string [] GetExtensions ()
138                 {
139                         return Extensions.Split (';');
140                 }
141
142                 public string [] GetLanguages ()
143                 {
144                         return Languages.Split (';');
145                 }
146         }
147 }
148 #endif
149