PageCompiler.cs 2.5 KB

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