2 // MSXslScriptManager.cs
5 // Atsushi Enomoto (atsushi@ximian.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.
32 using System.CodeDom.Compiler;
33 using System.Diagnostics;
34 using System.Collections;
35 using System.Globalization;
37 using System.Reflection;
38 using System.Security;
39 using System.Security.Policy;
41 using System.Xml.Schema;
42 using System.Xml.XPath;
44 using Microsoft.CSharp;
45 using Microsoft.VisualBasic;
47 namespace Mono.Xml.Xsl
49 internal abstract class ScriptCompilerInfo
51 string compilerCommand;
52 string defaultCompilerOptions;
54 public virtual string CompilerCommand {
55 get { return compilerCommand; }
56 set { compilerCommand = value; }
59 public virtual string DefaultCompilerOptions {
60 get { return defaultCompilerOptions; }
61 set { defaultCompilerOptions = value; }
64 public abstract CodeDomProvider CodeDomProvider { get; }
66 public abstract string Extension { get; }
68 public abstract string SourceTemplate { get; }
70 public virtual string GetCompilerArguments (string targetFileName)
72 return String.Concat (DefaultCompilerOptions, " ", targetFileName);
76 public virtual Type GetScriptClass (string code, string classSuffix, XPathNavigator scriptNode, Evidence evidence)
78 PermissionSet ps = SecurityManager.ResolvePolicy (evidence);
82 ICodeCompiler compiler = CodeDomProvider.CreateCompiler ();
83 CompilerParameters parameters = new CompilerParameters ();
84 parameters.CompilerOptions = DefaultCompilerOptions;
86 // get source filename
87 string filename = String.Empty;
89 if (scriptNode.BaseURI != String.Empty)
90 filename = new Uri (scriptNode.BaseURI).LocalPath;
91 } catch (FormatException) {
93 if (filename == String.Empty)
94 filename = "__baseURI_not_supplied__";
96 // get source location
97 IXmlLineInfo li = scriptNode as IXmlLineInfo;
99 li != null && li.LineNumber > 0 ?
100 String.Format (CultureInfo.InvariantCulture, "\n#line {0} \"{1}\"", li.LineNumber, filename) :
103 string source = SourceTemplate.Replace ("{0}", DateTime.Now.ToString ()).Replace ("{1}", classSuffix).Replace ("{2}", lineInfoLine + code);
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 // Actually it should be XsltCompileException,
109 // but to match with silly MS implementation...
110 throw new XsltException ("Stylesheet script compile error: \n" + FormatErrorMessage (res) /*+ "Code :\n" + source*/, null, scriptNode);
111 if (res.CompiledAssembly == null)
112 throw new XsltCompileException ("Cannot compile stylesheet script", null, scriptNode);
113 return res.CompiledAssembly.GetType ("GeneratedAssembly.Script" + classSuffix);
116 private string FormatErrorMessage (CompilerResults res)
118 string s = String.Empty;
119 foreach (CompilerError e in res.Errors) {
120 object [] parameters = new object [] {"\n",
122 e.Line > 0 ? " line " + e.Line : String.Empty,
123 e.IsWarning ? " WARNING: " : " ERROR: ",
127 s += String.Concat (parameters);
133 internal class CSharpCompilerInfo : ScriptCompilerInfo
135 public CSharpCompilerInfo ()
137 this.CompilerCommand = "mcs";
139 this.CompilerCommand = "csc.exe";
141 this.DefaultCompilerOptions = "/t:library /r:System.dll /r:System.Xml.dll /r:Microsoft.VisualBasic.dll";
144 public override CodeDomProvider CodeDomProvider {
\r
145 get { return new CSharpCodeProvider (); }
\r
148 public override string Extension {
149 get { return ".cs"; }
152 public override string SourceTemplate {
154 return @"// This file is automatically created by Mono managed XSLT engine.
157 using System.Collections;
159 using System.Text.RegularExpressions;
161 using System.Xml.XPath;
162 using System.Xml.Xsl;
163 using Microsoft.VisualBasic;
165 namespace GeneratedAssembly
167 public class Script{1}
176 internal class VBCompilerInfo : ScriptCompilerInfo
178 public VBCompilerInfo ()
180 this.CompilerCommand = "mbas";
182 this.CompilerCommand = "vbc.exe";
184 this.DefaultCompilerOptions = "/t:library /r:System.dll /r:System.XML.dll /r:Microsoft.VisualBasic.dll";
187 public override CodeDomProvider CodeDomProvider {
\r
188 get { return new VBCodeProvider (); }
\r
191 public override string Extension {
192 get { return ".vb"; }
195 public override string SourceTemplate {
197 return @"' This file is automatically created by Mono managed XSLT engine.
200 imports System.Collections
202 imports System.Text.RegularExpressions
204 imports System.Xml.XPath
205 imports System.Xml.Xsl
206 imports Microsoft.VisualBasic
208 namespace GeneratedAssembly
209 public Class Script{1}
218 internal class JScriptCompilerInfo : ScriptCompilerInfo
220 static Type providerType;
222 public JScriptCompilerInfo ()
224 this.CompilerCommand = "mjs";
226 this.CompilerCommand = "jsc.exe";
228 this.DefaultCompilerOptions = "/t:library /r:Microsoft.VisualBasic.dll";
231 public override CodeDomProvider CodeDomProvider {
233 // no need for locking
234 if (providerType == null) {
235 Assembly jsasm = Assembly.LoadWithPartialName ("Microsoft.JScript", null);
237 providerType = jsasm.GetType ("Microsoft.JScript.JScriptCodeProvider");
239 return (CodeDomProvider) Activator.CreateInstance (providerType);
243 public override string Extension {
244 get { return ".js"; }
247 public override string SourceTemplate {
249 return @"// This file is automatically created by Mono managed XSLT engine.
252 import System.Collections;
254 import System.Text.RegularExpressions;
256 import System.Xml.XPath;
257 import System.Xml.Xsl;
258 import Microsoft.VisualBasic;
260 package GeneratedAssembly