UserControlCompiler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // System.Web.Compilation.UserControlCompiler
  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.Web.UI;
  13. using System.Web.Util;
  14. namespace System.Web.Compilation
  15. {
  16. class UserControlCompiler : BaseCompiler
  17. {
  18. UserControlParser userControlParser;
  19. string sourceFile;
  20. string targetFile;
  21. internal UserControlCompiler (UserControlParser userControlParser)
  22. {
  23. this.userControlParser = userControlParser;
  24. }
  25. public override Type GetCompiledType ()
  26. {
  27. string inputFile = userControlParser.InputFile;
  28. sourceFile = GenerateSourceFile (userControlParser);
  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. if (result.Data is Type) {
  35. targetFile = result.OutputFile;
  36. return (Type) result.Data;
  37. }
  38. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  39. Type [] types = assembly.GetTypes ();
  40. foreach (Type t in types) {
  41. if (t.IsSubclassOf (typeof (UserControl))) {
  42. if (result.Data != null)
  43. throw new CompilationException ("More that 1 user control!!!", result);
  44. result.Data = t;
  45. }
  46. }
  47. return result.Data as Type;
  48. }
  49. public override string Key {
  50. get {
  51. return userControlParser.InputFile;
  52. }
  53. }
  54. public override string SourceFile {
  55. get {
  56. return sourceFile;
  57. }
  58. }
  59. public override string TargetFile {
  60. get {
  61. if (targetFile == null)
  62. targetFile = Path.ChangeExtension (sourceFile, ".dll");
  63. return targetFile;
  64. }
  65. }
  66. public static Type CompileUserControlType (UserControlParser userControlParser)
  67. {
  68. CompilationCacheItem item = CachingCompiler.GetCached (userControlParser.InputFile);
  69. if (item != null && item.Result != null) {
  70. if (item.Result != null) {
  71. userControlParser.Options = item.Result.Options;
  72. return item.Result.Data as Type;
  73. }
  74. throw new CompilationException (item.Result);
  75. }
  76. UserControlCompiler pc = new UserControlCompiler (userControlParser);
  77. return pc.GetCompiledType ();
  78. }
  79. string GenerateSourceFile (UserControlParser userControlParser)
  80. {
  81. string inputFile = userControlParser.InputFile;
  82. Stream input = File.OpenRead (inputFile);
  83. AspParser parser = new AspParser (inputFile, input);
  84. parser.Parse ();
  85. AspGenerator generator = new AspGenerator (inputFile, parser.Elements);
  86. generator.BaseType = userControlParser.BaseType.ToString ();
  87. generator.ProcessElements ();
  88. userControlParser.Text = generator.GetCode ().ReadToEnd ();
  89. options = generator.Options;
  90. //FIXME: should get Tmp dir for this application
  91. string csName = Path.GetTempFileName () + ".cs";
  92. WebTrace.WriteLine ("Writing {0}", csName);
  93. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  94. output.Write (userControlParser.Text);
  95. output.Close ();
  96. return csName;
  97. }
  98. }
  99. }