UserControlCompiler.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, string targetFile)
  22. {
  23. this.userControlParser = userControlParser;
  24. this.targetFile = targetFile;
  25. }
  26. public override Type GetCompiledType ()
  27. {
  28. string inputFile = userControlParser.InputFile;
  29. sourceFile = GenerateSourceFile (userControlParser);
  30. CachingCompiler compiler = new CachingCompiler (this);
  31. CompilationResult result = new CompilationResult ();
  32. if (compiler.Compile (result) == false)
  33. throw new CompilationException (result);
  34. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  35. Type [] types = assembly.GetTypes ();
  36. if (types.Length != 1)
  37. throw new CompilationException ("More than 1 Type in a user control?", result);
  38. result.Data = types [0];
  39. return types [0];
  40. }
  41. public override string Key {
  42. get {
  43. return userControlParser.InputFile;
  44. }
  45. }
  46. public override string SourceFile {
  47. get {
  48. return sourceFile;
  49. }
  50. }
  51. public override string TargetFile {
  52. get {
  53. if (targetFile == null)
  54. targetFile = Path.ChangeExtension (sourceFile, ".dll");
  55. return targetFile;
  56. }
  57. }
  58. public static Type CompileUserControlType (UserControlParser userControlParser)
  59. {
  60. CompilationCacheItem item = CachingCompiler.GetCached (userControlParser.InputFile);
  61. if (item != null && item.Result != null) {
  62. if (item.Result != null)
  63. return item.Result.Data as Type;
  64. throw new CompilationException (item.Result);
  65. }
  66. UserControlCompiler pc = new UserControlCompiler (userControlParser, null);
  67. return pc.GetCompiledType ();
  68. }
  69. static string GenerateSourceFile (UserControlParser userControlParser)
  70. {
  71. string inputFile = userControlParser.InputFile;
  72. Stream input = File.OpenRead (inputFile);
  73. AspParser parser = new AspParser (inputFile, input);
  74. parser.Parse ();
  75. AspGenerator generator = new AspGenerator (inputFile, parser.Elements);
  76. generator.BaseType = userControlParser.BaseType.ToString ();
  77. generator.ProcessElements ();
  78. userControlParser.Text = generator.GetCode ().ReadToEnd ();
  79. //FIXME: should get Tmp dir for this application
  80. string csName = Path.GetTempFileName () + ".cs";
  81. WebTrace.WriteLine ("Writing {0}", csName);
  82. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  83. output.Write (userControlParser.Text);
  84. output.Close ();
  85. return csName;
  86. }
  87. }
  88. }