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 (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. targetFile = result.OutputFile;
  37. return (Type) result.Data;
  38. }
  39. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  40. Type [] types = assembly.GetTypes ();
  41. foreach (Type t in types) {
  42. if (t.IsSubclassOf (typeof (UserControl))) {
  43. if (result.Data != null)
  44. throw new CompilationException ("More that 1 user control!!!", result);
  45. result.Data = t;
  46. }
  47. }
  48. return result.Data as Type;
  49. }
  50. public override string Key {
  51. get {
  52. return userControlParser.InputFile;
  53. }
  54. }
  55. public override string SourceFile {
  56. get {
  57. return sourceFile;
  58. }
  59. }
  60. public override string TargetFile {
  61. get {
  62. if (targetFile == null)
  63. targetFile = Path.ChangeExtension (sourceFile, ".dll");
  64. return targetFile;
  65. }
  66. }
  67. public static Type CompileUserControlType (UserControlParser userControlParser)
  68. {
  69. CompilationCacheItem item = CachingCompiler.GetCached (userControlParser.InputFile);
  70. if (item != null && item.Result != null) {
  71. if (item.Result != null) {
  72. userControlParser.Options = item.Result.Options;
  73. return item.Result.Data as Type;
  74. }
  75. throw new CompilationException (item.Result);
  76. }
  77. UserControlCompiler pc = new UserControlCompiler (userControlParser);
  78. return pc.GetCompiledType ();
  79. }
  80. string GenerateSourceFile (UserControlParser userControlParser)
  81. {
  82. string inputFile = userControlParser.InputFile;
  83. AspGenerator generator = new AspGenerator (inputFile);
  84. generator.Context = userControlParser.Context;
  85. generator.BaseType = userControlParser.BaseType.ToString ();
  86. generator.ProcessElements ();
  87. userControlParser.Text = generator.GetCode ().ReadToEnd ();
  88. options = generator.Options;
  89. dependencies = generator.Dependencies;
  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. }