PageCompiler.cs 2.5 KB

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