2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / ScriptCompilerInfo.cs
index 08c5844f4d3b4c4812378260930891c012108ca2..87438fa2bb0b218fb77e334ca631b7756a45da82 100755 (executable)
@@ -6,6 +6,27 @@
 //
 // (C)2003 Novell inc.
 //
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;
 using System.CodeDom;
 using System.CodeDom.Compiler;
@@ -52,7 +73,6 @@ namespace Mono.Xml.Xsl
                }
 
 
-#if true // Use CodeDom
                public virtual Type GetScriptClass (string code, string classSuffix, XPathNavigator scriptNode, Evidence evidence)
                {
                        PermissionSet ps = SecurityManager.ResolvePolicy (evidence);
@@ -77,7 +97,7 @@ namespace Mono.Xml.Xsl
                        IXmlLineInfo li = scriptNode as IXmlLineInfo;
                        string lineInfoLine = 
                                li != null && li.LineNumber > 0 ?
-                               "\n#line " + li.LineNumber + " \"" + filename + "\"" :
+                               String.Format (CultureInfo.InvariantCulture, "\n#line {0} \"{1}\"", li.LineNumber, filename) :
                                String.Empty;
 
                        string source = SourceTemplate.Replace ("{0}", DateTime.Now.ToString ()).Replace ("{1}", classSuffix).Replace ("{2}", lineInfoLine + code);
@@ -89,56 +109,6 @@ namespace Mono.Xml.Xsl
                                throw new XsltCompileException ("Cannot compile stylesheet script", null, scriptNode);
                        return res.CompiledAssembly.GetType ("GeneratedAssembly.Script" + classSuffix);
                }
-#else // obsolete code - uses external process
-               [MonoTODO ("Should use Assembly.LoadFile() instead of LoadFrom() after its implementation has finished.")]
-               public virtual Type GetScriptClass (string code, string classSuffix, XPathNavigator scriptNode, Evidence evidence)
-               {
-                       string tmpPath = Path.GetTempPath ();
-                       if (!tmpPath.EndsWith (Path.DirectorySeparatorChar.ToString ()))
-                               tmpPath += Path.DirectorySeparatorChar;
-                       string tmpbase = tmpPath + Guid.NewGuid ();
-                       ProcessStartInfo psi = new ProcessStartInfo ();
-                       psi.UseShellExecute = false;
-                       psi.RedirectStandardError = true;
-                       psi.RedirectStandardOutput = true;
-                       Process proc = new Process ();
-                       proc.StartInfo = psi;
-                       StreamWriter sw = null;
-                       try {
-                               PermissionSet ps = SecurityManager.ResolvePolicy (evidence);
-                               if (ps != null)
-                                       ps.Demand ();
-                               sw = File.CreateText (tmpbase + Extension);
-                               IXmlLineInfo li = scriptNode as IXmlLineInfo;
-                               string lineInfoLine = li != null ? "\n#line " + li.LineNumber + " \"" + scriptNode.BaseURI + "\"\n" : String.Empty;
-                               sw.WriteLine (SourceTemplate.Replace ("{0}", DateTime.Now.ToString ()).Replace ("{1}", classSuffix).Replace ("{2}", lineInfoLine + code));
-
-                               sw.Close ();
-                               psi.FileName = CompilerCommand;
-                               psi.Arguments = String.Concat (GetCompilerArguments (tmpbase + Extension));
-                               psi.WorkingDirectory = tmpPath;
-                               proc.Start ();
-//                             Console.WriteLine (proc.StandardOutput.ReadToEnd ());
-//                             Console.WriteLine (proc.StandardError.ReadToEnd ());
-                               proc.WaitForExit (); // should we configure timeout?
-                               Assembly generated = Assembly.LoadFrom (tmpbase + ".dll");
-
-                               if (generated == null)
-                                       throw new XsltCompileException ("Could not load script assembly", null, scriptNode);
-                               return generated.GetType ("GeneratedAssembly.Script" + classSuffix);
-                       } catch (Exception ex) {
-                               throw new XsltCompileException ("Script compilation error: " + ex.Message, ex, scriptNode);
-                       } finally {
-                               try {
-                                       File.Delete (tmpbase + Extension);
-                                       File.Delete (tmpbase + ".dll");
-                                       if (sw != null)
-                                               sw.Close ();
-                               } catch (Exception) {
-                               }
-                       }
-               }
-#endif
 
                private string FormatErrorMessage (CompilerResults res)
                {
@@ -246,12 +216,6 @@ end namespace
        {
                static Type providerType;
 
-               static JScriptCompilerInfo ()
-               {
-                       Assembly jsasm = Assembly.LoadWithPartialName ("Microsoft.JScript", null);
-                       providerType = jsasm.GetType ("Microsoft.JScript.JScriptCodeProvider");
-               }
-
                public JScriptCompilerInfo ()
                {
                        this.CompilerCommand = "mjs";
@@ -261,9 +225,17 @@ end namespace
                        this.DefaultCompilerOptions = "/t:library /r:Microsoft.VisualBasic.dll";
                }
 
-               public override CodeDomProvider CodeDomProvider {\r
-                       get { return (CodeDomProvider) Activator.CreateInstance (providerType); }\r
-               }\r
+               public override CodeDomProvider CodeDomProvider {
+                       get {
+                               // no need for locking
+                               if (providerType == null) {
+                                       Assembly jsasm = Assembly.LoadWithPartialName ("Microsoft.JScript", null);
+                                       if (jsasm != null)
+                                               providerType = jsasm.GetType ("Microsoft.JScript.JScriptCodeProvider");
+                               }
+                               return (CodeDomProvider) Activator.CreateInstance (providerType); 
+                       }
+               }
 
                public override string Extension {
                        get { return ".js"; }