2 // System.Web.Compilation.PageCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 using System.Reflection;
14 using System.Web.Util;
16 namespace System.Web.Compilation
18 class PageCompiler : BaseCompiler
20 PageParser pageParser;
23 private PageCompiler (PageParser pageParser)
25 this.pageParser = pageParser;
28 public override Type GetCompiledType ()
30 string inputFile = pageParser.InputFile;
31 sourceFile = GenerateSourceFile ();
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);
39 result.Dependencies = dependencies;
40 if (result.Data is Type)
41 return (Type) result.Data;
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);
53 return result.Data as Type;
56 public override string Key {
58 return pageParser.InputFile;
62 public override string SourceFile {
68 public static Type CompilePageType (PageParser pageParser)
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;
77 throw new CompilationException (item.Result);
80 PageCompiler pc = new PageCompiler (pageParser);
81 return pc.GetCompiledType ();
84 string GenerateSourceFile ()
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;
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);