2003-03-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / PageCompiler.cs
1 //
2 // System.Web.Compilation.PageCompiler
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.Reflection;
12 using System.Text;
13 using System.Web.UI;
14 using System.Web.Util;
15
16 namespace System.Web.Compilation
17 {
18         class PageCompiler : BaseCompiler
19         {
20                 PageParser pageParser;
21                 string sourceFile;
22
23                 private PageCompiler (PageParser pageParser)
24                 {
25                         this.pageParser = pageParser;
26                 }
27
28                 public override Type GetCompiledType ()
29                 {
30                         string inputFile = pageParser.InputFile;
31                         sourceFile = GenerateSourceFile ();
32
33                         CachingCompiler compiler = new CachingCompiler (this);
34                         CompilationResult result = new CompilationResult (sourceFile);
35                         result.Options = options;
36                         if (compiler.Compile (result) == false)
37                                 throw new CompilationException (result);
38                         
39                         result.Dependencies = dependencies;
40                         if (result.Data is Type)
41                                 return (Type) result.Data;
42
43                         Assembly assembly = Assembly.LoadFrom (result.OutputFile);
44                         Type [] types = assembly.GetTypes ();
45                         foreach (Type t in types) {
46                                 if (t.IsSubclassOf (typeof (Page))) {
47                                         if (result.Data != null)
48                                                 throw new CompilationException ("More that 1 page!!!", result);
49                                         result.Data = t;
50                                 }
51                         }
52
53                         return result.Data as Type;
54                 }
55
56                 public override string Key {
57                         get {
58                                 return pageParser.InputFile;
59                         }
60                 }
61
62                 public override string SourceFile {
63                         get {
64                                 return sourceFile;
65                         }
66                 }
67
68                 public static Type CompilePageType (PageParser pageParser)
69                 {
70                         CompilationCacheItem item = CachingCompiler.GetCached (pageParser.InputFile);
71                         if (item != null && item.Result != null) {
72                                 if (item.Result != null) {
73                                         pageParser.Options = item.Result.Options;
74                                         return item.Result.Data as Type;
75                                 }
76
77                                 throw new CompilationException (item.Result);
78                         }
79
80                         PageCompiler pc = new PageCompiler (pageParser);
81                         return pc.GetCompiledType ();
82                 }
83
84                 string GenerateSourceFile ()
85                 {
86                         AspGenerator generator = new AspGenerator (pageParser.InputFile);
87                         generator.Context = pageParser.Context;
88                         generator.BaseType = pageParser.BaseType.ToString ();
89                         generator.ProcessElements ();
90                         pageParser.Text = generator.GetCode ().ReadToEnd ();
91                         dependencies = generator.Dependencies;
92                         options = generator.Options;
93
94                         //FIXME: should get Tmp dir for this application
95                         string csName = Path.GetTempFileName () + ".cs";
96                         WebTrace.WriteLine ("Writing {0}", csName);
97                         StreamWriter output = new StreamWriter (File.OpenWrite (csName));
98                         output.Write (pageParser.Text);
99                         output.Close ();
100                         return csName;
101                 }
102         }
103 }
104