GlobalAsaxCompiler.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // System.Web.Compilation.GlobalAsaxCompiler
  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;
  14. using System.Web.Util;
  15. namespace System.Web.Compilation
  16. {
  17. class GlobalAsaxCompiler : BaseCompiler
  18. {
  19. string filename;
  20. string sourceFile;
  21. private GlobalAsaxCompiler (string filename)
  22. {
  23. this.filename = filename;
  24. }
  25. public override Type GetCompiledType ()
  26. {
  27. sourceFile = GenerateSourceFile ();
  28. CachingCompiler compiler = new CachingCompiler (this);
  29. CompilationResult result = new CompilationResult ();
  30. result.Options = options;
  31. if (compiler.Compile (result) == false)
  32. throw new CompilationException (result);
  33. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  34. Type [] types = assembly.GetTypes ();
  35. foreach (Type t in types) {
  36. if (t.IsSubclassOf (typeof (HttpApplication))) {
  37. if (result.Data != null)
  38. throw new CompilationException ("More that 1 app!!!", result);
  39. result.Data = t;
  40. }
  41. }
  42. return result.Data as Type;
  43. }
  44. public override string Key {
  45. get {
  46. return filename;
  47. }
  48. }
  49. public override string SourceFile {
  50. get {
  51. return sourceFile;
  52. }
  53. }
  54. public static Type CompileApplicationType (string filename)
  55. {
  56. CompilationCacheItem item = CachingCompiler.GetCached (filename);
  57. if (item != null && item.Result != null) {
  58. if (item.Result != null)
  59. return item.Result.Data as Type;
  60. throw new CompilationException (item.Result);
  61. }
  62. GlobalAsaxCompiler gac = new GlobalAsaxCompiler (filename);
  63. return gac.GetCompiledType ();
  64. }
  65. string GenerateSourceFile ()
  66. {
  67. Stream input = File.OpenRead (filename);
  68. AspParser parser = new AspParser (filename, input);
  69. parser.Parse ();
  70. AspGenerator generator = new AspGenerator (filename, parser.Elements);
  71. generator.BaseType = typeof (HttpApplication).ToString ();
  72. generator.ProcessElements ();
  73. string generated = generator.GetCode ().ReadToEnd ();
  74. options = generator.Options;
  75. //FIXME: should get Tmp dir for this application
  76. string csName = Path.GetTempFileName () + ".cs";
  77. WebTrace.WriteLine ("Writing {0}", csName);
  78. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  79. output.Write (generated);
  80. output.Close ();
  81. return csName;
  82. }
  83. }
  84. }