Copied remotely
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / ScriptCompilerInfo.cs
1 //
2 // MSXslScriptManager.cs
3 //
4 // Author:
5 //      Atsushi Enomoto (atsushi@ximian.com)
6 //
7 // (C)2003 Novell inc.
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 using System;
31 using System.CodeDom;
32 using System.CodeDom.Compiler;
33 using System.Diagnostics;
34 using System.Collections;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 using System.Security;
39 using System.Security.Policy;
40 using System.Xml;
41 using System.Xml.Schema;
42 using System.Xml.XPath;
43 using System.Xml.Xsl;
44 using Microsoft.CSharp;
45 using Microsoft.VisualBasic;
46
47 namespace Mono.Xml.Xsl
48 {
49         internal abstract class ScriptCompilerInfo
50         {
51                 string compilerCommand;
52                 string defaultCompilerOptions;
53
54                 public virtual string CompilerCommand {
55                         get { return compilerCommand; }
56                         set { compilerCommand = value; }
57                 }
58
59                 public virtual string DefaultCompilerOptions {
60                         get { return defaultCompilerOptions; }
61                         set { defaultCompilerOptions = value; }
62                 }
63
64                 public abstract CodeDomProvider CodeDomProvider { get; }
65
66                 public abstract string Extension { get; }
67
68                 public abstract string SourceTemplate { get; }
69
70                 public virtual string GetCompilerArguments (string targetFileName)
71                 {
72                         return String.Concat (DefaultCompilerOptions, " ", targetFileName);
73                 }
74
75
76                 public virtual Type GetScriptClass (string code, string classSuffix, XPathNavigator scriptNode, Evidence evidence)
77                 {
78                         PermissionSet ps = SecurityManager.ResolvePolicy (evidence);
79                         if (ps != null)
80                                 ps.Demand ();
81
82                         ICodeCompiler compiler = CodeDomProvider.CreateCompiler ();
83                         CompilerParameters parameters = new CompilerParameters ();
84                         parameters.CompilerOptions = DefaultCompilerOptions;
85
86                         // get source filename
87                         string filename = String.Empty;
88                         try {
89                                 if (scriptNode.BaseURI != String.Empty)
90                                         filename = new Uri (scriptNode.BaseURI).LocalPath;
91                         } catch (FormatException) {
92                         }
93                         if (filename == String.Empty)
94                                 filename = "__baseURI_not_supplied__";
95
96                         // get source location
97                         IXmlLineInfo li = scriptNode as IXmlLineInfo;
98                         string lineInfoLine = 
99                                 li != null && li.LineNumber > 0 ?
100                                 String.Format (CultureInfo.InvariantCulture, "\n#line {0} \"{1}\"", li.LineNumber, filename) :
101                                 String.Empty;
102
103                         string source = SourceTemplate.Replace ("{0}", DateTime.Now.ToString ()).Replace ("{1}", classSuffix).Replace ("{2}", lineInfoLine + code);
104
105                         CompilerResults res = compiler.CompileAssemblyFromSource (parameters, source);
106                         if (res.Errors.Count != 0)
107                                 throw new XsltCompileException ("Stylesheet script compile error: \n" + FormatErrorMessage (res) /*+ "Code :\n" + source*/, null, scriptNode);
108                         if (res.CompiledAssembly == null)
109                                 throw new XsltCompileException ("Cannot compile stylesheet script", null, scriptNode);
110                         return res.CompiledAssembly.GetType ("GeneratedAssembly.Script" + classSuffix);
111                 }
112
113                 private string FormatErrorMessage (CompilerResults res)
114                 {
115                         string s = String.Empty;
116                         foreach (CompilerError e in res.Errors) {
117                                 object [] parameters = new object [] {"\n",
118                                         e.FileName,
119                                         e.Line > 0 ? " line " + e.Line : String.Empty,
120                                         e.IsWarning ? " WARNING: " : " ERROR: ",
121                                         e.ErrorNumber,
122                                         ": ",
123                                         e.ErrorText};
124                                 s += String.Concat (parameters);
125                         }
126                         return s;
127                 }
128         }
129
130         internal class CSharpCompilerInfo : ScriptCompilerInfo
131         {
132                 public CSharpCompilerInfo ()
133                 {
134                         this.CompilerCommand = "mcs";
135 #if MS_NET
136                         this.CompilerCommand = "csc.exe";
137 #endif
138                         this.DefaultCompilerOptions = "/t:library /r:System.dll /r:System.Xml.dll /r:Microsoft.VisualBasic.dll";
139                 }
140
141                 public override CodeDomProvider CodeDomProvider {\r
142                         get { return new CSharpCodeProvider (); }\r
143                 }\r
144
145                 public override string Extension {
146                         get { return ".cs"; }
147                 }
148
149                 public override string SourceTemplate {
150                         get {
151                                 return @"// This file is automatically created by Mono managed XSLT engine.
152 // Created time: {0}
153 using System;
154 using System.Collections;
155 using System.Text;
156 using System.Text.RegularExpressions;
157 using System.Xml;
158 using System.Xml.XPath;
159 using System.Xml.Xsl;
160 using Microsoft.VisualBasic;
161
162 namespace GeneratedAssembly
163 {
164 public class Script{1}
165 {
166         {2}
167 }
168 }";
169                         }
170                 }
171         }
172
173         internal class VBCompilerInfo : ScriptCompilerInfo
174         {
175                 public VBCompilerInfo ()
176                 {
177                         this.CompilerCommand = "mbas";
178 #if MS_NET
179                         this.CompilerCommand = "vbc.exe";
180 #endif
181                         this.DefaultCompilerOptions = "/t:library  /r:System.dll /r:System.XML.dll /r:Microsoft.VisualBasic.dll";
182                 }
183
184                 public override CodeDomProvider CodeDomProvider {\r
185                         get { return new VBCodeProvider (); }\r
186                 }\r
187
188                 public override string Extension {
189                         get { return ".vb"; }
190                 }
191
192                 public override string SourceTemplate {
193                         get {
194                                 return @"' This file is automatically created by Mono managed XSLT engine.
195 ' Created time: {0}
196 imports System
197 imports System.Collections
198 imports System.Text
199 imports System.Text.RegularExpressions
200 imports System.Xml
201 imports System.Xml.XPath
202 imports System.Xml.Xsl
203 imports Microsoft.VisualBasic
204
205 namespace GeneratedAssembly
206 public Class Script{1}
207         {2}
208 end Class
209 end namespace
210 ";
211                         }
212                 }
213         }
214
215         internal class JScriptCompilerInfo : ScriptCompilerInfo
216         {
217                 static Type providerType;
218
219                 public JScriptCompilerInfo ()
220                 {
221                         this.CompilerCommand = "mjs";
222 #if MS_NET
223                         this.CompilerCommand = "jsc.exe";
224 #endif
225                         this.DefaultCompilerOptions = "/t:library /r:Microsoft.VisualBasic.dll";
226                 }
227
228                 public override CodeDomProvider CodeDomProvider {
229                         get {
230                                 // no need for locking
231                                 if (providerType == null) {
232                                         Assembly jsasm = Assembly.LoadWithPartialName ("Microsoft.JScript", null);
233                                         if (jsasm != null)
234                                                 providerType = jsasm.GetType ("Microsoft.JScript.JScriptCodeProvider");
235                                 }
236                                 return (CodeDomProvider) Activator.CreateInstance (providerType); 
237                         }
238                 }
239
240                 public override string Extension {
241                         get { return ".js"; }
242                 }
243
244                 public override string SourceTemplate {
245                         get {
246                                 return @"// This file is automatically created by Mono managed XSLT engine.
247 // Created time: {0}
248 import System;
249 import System.Collections;
250 import System.Text;
251 import System.Text.RegularExpressions;
252 import System.Xml;
253 import System.Xml.XPath;
254 import System.Xml.Xsl;
255 import Microsoft.VisualBasic;
256
257 package GeneratedAssembly
258 {
259 class Script{1} {
260         {2}
261 }
262 }
263 ";
264                         }
265                 }
266         }
267 }
268