PageCompiler.cs 2.5 KB

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