2003-07-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / WebServiceCompiler.cs
1 //
2 // System.Web.Compilation.WebServiceCompiler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
9 using System;
10 using System.IO;
11 using System.Web.UI;
12 using System.CodeDom.Compiler;
13 //temp:
14 using Microsoft.CSharp;
15
16 namespace System.Web.Compilation
17 {
18         class WebServiceCompiler : BaseCompiler
19         {
20                 SimpleWebHandlerParser wService;
21                 ICodeCompiler compiler;
22
23                 public WebServiceCompiler (SimpleWebHandlerParser wService)
24                         : base (null)
25                 {
26                         this.wService = wService;
27                 }
28
29                 public static Type CompileIntoType (SimpleWebHandlerParser wService)
30                 {
31                         WebServiceCompiler wsc = new WebServiceCompiler (wService);
32                         return wsc.GetCompiledType ();
33                 }
34
35                 public override Type GetCompiledType ()
36                 {
37                         if (wService.Program.Trim () == "")
38                                 return wService.GetTypeFromBin (wService.ClassName);
39
40                         //FIXME: update when we support other languages
41                         string fname = Path.ChangeExtension (Path.GetTempFileName (), ".cs");
42                         StreamWriter sw = new StreamWriter (File.OpenWrite (fname));
43                         sw.WriteLine (wService.Program);
44                         sw.Close ();
45
46                         //TODO: get the compiler and default options from system.web/compileroptions
47                         CompilerResults results = CachingCompiler.Compile (this, fname);
48                         CheckCompilerErrors (results);
49
50                         return results.CompiledAssembly.GetType (wService.ClassName, true);
51                 }
52
53                 void CheckCompilerErrors (CompilerResults results)
54                 {
55                         if (results.NativeCompilerReturnValue == 0)
56                                 return;
57
58                         throw new CompilationException (wService.PhysicalPath, results.Errors, wService.Program);
59                 }
60
61                 internal new SimpleWebHandlerParser Parser {
62                         get { return wService; }
63                 }
64
65                 internal override ICodeCompiler Compiler {
66                         get {
67                                 if (compiler == null) {
68                                         //TODO: get the compiler and default options from system.web/compileroptions
69                                         compiler = new CSharpCodeProvider ().CreateCompiler ();
70                                 }
71
72                                 return compiler;
73                         }
74                 }
75         }
76 }
77