TemplateControlParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Web.UI.TemplateControlParser
  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.Web.Compilation;
  14. using System.Web.Util;
  15. namespace System.Web.UI
  16. {
  17. public abstract class TemplateControlParser : TemplateParser
  18. {
  19. bool autoEventWireup = true;
  20. bool enableViewState = true;
  21. internal override void ProcessMainAttributes (Hashtable atts)
  22. {
  23. autoEventWireup = GetBool (atts, "AutoEventWireup", true);
  24. enableViewState = GetBool (atts, "EnableViewState", true);
  25. atts.Remove ("TargetSchema"); // Ignored
  26. base.ProcessMainAttributes (atts);
  27. }
  28. internal object GetCompiledInstance (string virtualPath, string inputFile, HttpContext context)
  29. {
  30. Context = context;
  31. InputFile = MapPath (UrlUtils.Combine (virtualPath, inputFile));
  32. Type type = CompileIntoType ();
  33. if (type == null)
  34. return null;
  35. object ctrl = Activator.CreateInstance (type);
  36. if (ctrl == null)
  37. return null;
  38. HandleOptions (ctrl);
  39. return ctrl;
  40. }
  41. internal override void AddDirective (string directive, Hashtable atts)
  42. {
  43. int cmp = String.Compare ("Register", directive, true);
  44. if (cmp == 0) {
  45. string tagprefix = GetString (atts, "TagPrefix", null);
  46. if (tagprefix == null || tagprefix.Trim () == "")
  47. ThrowParseException ("No TagPrefix attribute found.");
  48. string ns = GetString (atts, "Namespace", null);
  49. string assembly = GetString (atts, "Assembly", null);
  50. if (ns != null && assembly == null)
  51. ThrowParseException ("Need an Assembly attribute with Namespace.");
  52. if (ns == null && assembly != null)
  53. ThrowParseException ("Need a Namespace attribute with Assembly.");
  54. if (ns != null) {
  55. if (atts.Count != 0)
  56. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  57. AddImport (ns);
  58. Assembly ass = AddAssemblyByName (assembly);
  59. AddDependency (ass.Location);
  60. RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
  61. return;
  62. }
  63. string tagname = GetString (atts, "TagName", null);
  64. string src = GetString (atts, "Src", null);
  65. if (tagname == null && src != null)
  66. ThrowParseException ("Need a TagName attribute with Src.");
  67. if (tagname != null && src == null)
  68. ThrowParseException ("Need a Src attribute with TagName.");
  69. if (!src.EndsWith (".ascx"))
  70. ThrowParseException ("Source file extension for controls must be .ascx");
  71. try {
  72. AddDependency (MapPath (src));
  73. } catch (Exception e) {
  74. throw new ParseException (Location, e.Message);
  75. }
  76. Type type = UserControlParser.GetCompiledType (BaseVirtualDir, src, Context);
  77. AddAssembly (type.Assembly, true);
  78. RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
  79. return;
  80. }
  81. cmp = String.Compare ("Reference", directive, true);
  82. if (cmp == 0) {
  83. string page = GetString (atts, "Page", null);
  84. string control = GetString (atts, "Control", null);
  85. //TODO: compile and store control/page
  86. Console.WriteLine ("WARNING: Reference is not supported yet!");
  87. if (atts.Count != 0)
  88. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  89. return;
  90. }
  91. atts.Remove ("OutputCache"); // ignored
  92. base.AddDirective (directive, atts);
  93. }
  94. internal override void HandleOptions (object obj)
  95. {
  96. Control ctrl = obj as Control;
  97. ctrl.AutoEventWireup = autoEventWireup;
  98. ctrl.EnableViewState = enableViewState;
  99. }
  100. internal bool AutoEventWireup {
  101. get { return autoEventWireup; }
  102. }
  103. internal bool EnableViewState {
  104. get { return enableViewState; }
  105. }
  106. }
  107. }