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 if (res.CompiledAssembly == null)
109 throw new XsltCompileException ("Cannot compile stylesheet script", null, scriptNode);
110 return res.CompiledAssembly.GetType ("GeneratedAssembly.Script" + classSuffix);
113 private string FormatErrorMessage (CompilerResults res)
115 string s = String.Empty;
116 foreach (CompilerError e in res.Errors) {
117 object [] parameters = new object [] {"\n",
119 e.Line > 0 ? " line " + e.Line : String.Empty,
120 e.IsWarning ? " WARNING: " : " ERROR: ",
124 s += String.Concat (parameters);
130 internal class CSharpCompilerInfo : ScriptCompilerInfo
132 public CSharpCompilerInfo ()
134 this.CompilerCommand = "mcs";
136 this.CompilerCommand = "csc.exe";
138 this.DefaultCompilerOptions = "/t:library /r:System.dll /r:System.Xml.dll /r:Microsoft.VisualBasic.dll";
141 public override CodeDomProvider CodeDomProvider {
\r
142 get { return new CSharpCodeProvider (); }
\r
145 public override string Extension {
146 get { return ".cs"; }
149 public override string SourceTemplate {
151 return @"// This file is automatically created by Mono managed XSLT engine.
154 using System.Collections;
156 using System.Text.RegularExpressions;
158 using System.Xml.XPath;
159 using System.Xml.Xsl;
160 using Microsoft.VisualBasic;
162 namespace GeneratedAssembly
164 public class Script{1}
173 internal class VBCompilerInfo : ScriptCompilerInfo
175 public VBCompilerInfo ()
177 this.CompilerCommand = "mbas";
179 this.CompilerCommand = "vbc.exe";
181 this.DefaultCompilerOptions = "/t:library /r:System.dll /r:System.XML.dll /r:Microsoft.VisualBasic.dll";
184 public override CodeDomProvider CodeDomProvider {
\r
185 get { return new VBCodeProvider (); }
\r
188 public override string Extension {
189 get { return ".vb"; }
192 public override string SourceTemplate {
194 return @"' This file is automatically created by Mono managed XSLT engine.
197 imports System.Collections
199 imports System.Text.RegularExpressions
201 imports System.Xml.XPath
202 imports System.Xml.Xsl
203 imports Microsoft.VisualBasic
205 namespace GeneratedAssembly
206 public Class Script{1}
215 internal class JScriptCompilerInfo : ScriptCompilerInfo
217 static Type providerType;
219 public JScriptCompilerInfo ()
221 this.CompilerCommand = "mjs";
223 this.CompilerCommand = "jsc.exe";
225 this.DefaultCompilerOptions = "/t:library /r:Microsoft.VisualBasic.dll";
228 public override CodeDomProvider CodeDomProvider {
230 // no need for locking
231 if (providerType == null) {
232 Assembly jsasm = Assembly.LoadWithPartialName ("Microsoft.JScript", null);
234 providerType = jsasm.GetType ("Microsoft.JScript.JScriptCodeProvider");
236 return (CodeDomProvider) Activator.CreateInstance (providerType);
240 public override string Extension {
241 get { return ".js"; }
244 public override string SourceTemplate {
246 return @"// This file is automatically created by Mono managed XSLT engine.
249 import System.Collections;
251 import System.Text.RegularExpressions;
253 import System.Xml.XPath;
254 import System.Xml.Xsl;
255 import Microsoft.VisualBasic;
257 package GeneratedAssembly