2
0

PageCompiler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Collections;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Web.UI;
  15. using System.Web.Util;
  16. namespace System.Web.Compilation
  17. {
  18. class PageCompiler : BaseCompiler
  19. {
  20. PageParser pageParser;
  21. string sourceFile;
  22. Hashtable options;
  23. private PageCompiler (PageParser pageParser)
  24. {
  25. this.pageParser = pageParser;
  26. }
  27. public override Type GetCompiledType ()
  28. {
  29. string inputFile = pageParser.InputFile;
  30. sourceFile = GenerateSourceFile ();
  31. CachingCompiler compiler = new CachingCompiler (this);
  32. CompilationResult result = new CompilationResult ();
  33. if (compiler.Compile (result) == false)
  34. throw new CompilationException (result);
  35. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  36. Type [] types = assembly.GetTypes ();
  37. if (types.Length != 1)
  38. throw new CompilationException ("More than 1 Type in a page?", result);
  39. result.Data = types [0];
  40. return types [0];
  41. }
  42. public override string Key {
  43. get {
  44. return pageParser.InputFile;
  45. }
  46. }
  47. public override string SourceFile {
  48. get {
  49. return sourceFile;
  50. }
  51. }
  52. public override string CompilerOptions {
  53. get {
  54. if (options == null)
  55. return base.CompilerOptions;
  56. StringBuilder sb = new StringBuilder (base.CompilerOptions);
  57. string compilerOptions = options ["CompilerOptions"] as string;
  58. if (compilerOptions != null) {
  59. sb.Append (' ');
  60. sb.Append (compilerOptions);
  61. }
  62. string references = options ["References"] as string;
  63. if (references == null)
  64. return sb.ToString ();
  65. string [] split = references.Split (' ');
  66. foreach (string s in split)
  67. sb.AppendFormat (" /r:{0}", s);
  68. return sb.ToString ();
  69. }
  70. }
  71. public static Type CompilePageType (PageParser pageParser)
  72. {
  73. CompilationCacheItem item = CachingCompiler.GetCached (pageParser.InputFile);
  74. if (item != null && item.Result != null) {
  75. if (item.Result != null)
  76. return item.Result.Data as Type;
  77. throw new CompilationException (item.Result);
  78. }
  79. PageCompiler pc = new PageCompiler (pageParser);
  80. return pc.GetCompiledType ();
  81. }
  82. string GenerateSourceFile ()
  83. {
  84. string inputFile = pageParser.InputFile;
  85. Stream input = File.OpenRead (inputFile);
  86. AspParser parser = new AspParser (inputFile, input);
  87. parser.Parse ();
  88. AspGenerator generator = new AspGenerator (inputFile, parser.Elements);
  89. generator.BaseType = pageParser.BaseType.ToString ();
  90. generator.ProcessElements ();
  91. pageParser.Text = generator.GetCode ().ReadToEnd ();
  92. options = generator.Options;
  93. //FIXME: should get Tmp dir for this application
  94. string csName = Path.GetTempFileName () + ".cs";
  95. WebTrace.WriteLine ("Writing {0}", csName);
  96. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  97. output.Write (pageParser.Text);
  98. output.Close ();
  99. return csName;
  100. }
  101. }
  102. }