TemplateControlParser.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Web.Compilation;
  34. using System.Web.Util;
  35. namespace System.Web.UI
  36. {
  37. public abstract class TemplateControlParser : TemplateParser
  38. {
  39. bool autoEventWireup = true;
  40. bool enableViewState = true;
  41. protected TemplateControlParser ()
  42. {
  43. }
  44. internal override void ProcessMainAttributes (Hashtable atts)
  45. {
  46. autoEventWireup = GetBool (atts, "AutoEventWireup", PagesConfig.AutoEventWireup);
  47. enableViewState = GetBool (atts, "EnableViewState", PagesConfig.EnableViewState);
  48. atts.Remove ("TargetSchema"); // Ignored
  49. base.ProcessMainAttributes (atts);
  50. }
  51. internal object GetCompiledInstance ()
  52. {
  53. Type type = CompileIntoType ();
  54. if (type == null)
  55. return null;
  56. object ctrl = Activator.CreateInstance (type);
  57. if (ctrl == null)
  58. return null;
  59. HandleOptions (ctrl);
  60. return ctrl;
  61. }
  62. internal override void AddDirective (string directive, Hashtable atts)
  63. {
  64. int cmp = String.Compare ("Register", directive, true);
  65. if (cmp == 0) {
  66. string tagprefix = GetString (atts, "TagPrefix", null);
  67. if (tagprefix == null || tagprefix.Trim () == "")
  68. ThrowParseException ("No TagPrefix attribute found.");
  69. string ns = GetString (atts, "Namespace", null);
  70. string assembly = GetString (atts, "Assembly", null);
  71. if (ns != null && assembly == null)
  72. ThrowParseException ("Need an Assembly attribute with Namespace.");
  73. if (ns == null && assembly != null)
  74. ThrowParseException ("Need a Namespace attribute with Assembly.");
  75. if (ns != null) {
  76. if (atts.Count != 0)
  77. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  78. AddImport (ns);
  79. Assembly ass = AddAssemblyByName (assembly);
  80. AddDependency (ass.Location);
  81. RootBuilder.Foundry.RegisterFoundry (tagprefix, ass, ns);
  82. return;
  83. }
  84. string tagname = GetString (atts, "TagName", null);
  85. string src = GetString (atts, "Src", null);
  86. if (tagname == null && src != null)
  87. ThrowParseException ("Need a TagName attribute with Src.");
  88. if (tagname != null && src == null)
  89. ThrowParseException ("Need a Src attribute with TagName.");
  90. if (!src.EndsWith (".ascx"))
  91. ThrowParseException ("Source file extension for controls must be .ascx");
  92. string realpath = MapPath (src);
  93. if (!File.Exists (realpath))
  94. throw new ParseException (Location, "Could not find file \""
  95. + realpath + "\".");
  96. try {
  97. AddDependency (realpath);
  98. } catch (Exception e) {
  99. throw new ParseException (Location, e.Message);
  100. }
  101. string vpath = UrlUtils.Combine (BaseVirtualDir, src);
  102. Type type = UserControlParser.GetCompiledType (vpath, realpath, Context);
  103. AddAssembly (type.Assembly, true);
  104. RootBuilder.Foundry.RegisterFoundry (tagprefix, tagname, type);
  105. return;
  106. }
  107. cmp = String.Compare ("Reference", directive, true);
  108. if (cmp == 0) {
  109. string page = GetString (atts, "Page", null);
  110. string control = GetString (atts, "Control", null);
  111. bool is_page = (page != null);
  112. if (!is_page && control == null)
  113. ThrowParseException ("Must provide 'page' or 'control' attribute");
  114. if (is_page && control != null)
  115. ThrowParseException ("'page' and 'control' are mutually exclusive");
  116. string filepath = (!is_page) ? control : page;
  117. filepath = MapPath (filepath);
  118. AddDependency (filepath);
  119. Type ctype;
  120. if (is_page) {
  121. PageParser pp = new PageParser (page, filepath, Context);
  122. ctype = pp.CompileIntoType ();
  123. } else {
  124. ctype = UserControlParser.GetCompiledType (control, filepath, Context);
  125. }
  126. AddAssembly (ctype.Assembly, true);
  127. if (atts.Count != 0)
  128. ThrowParseException ("Unknown attribute: " + GetOneKey (atts));
  129. return;
  130. }
  131. base.AddDirective (directive, atts);
  132. }
  133. internal override void HandleOptions (object obj)
  134. {
  135. Control ctrl = obj as Control;
  136. ctrl.AutoEventWireup = autoEventWireup;
  137. ctrl.EnableViewState = enableViewState;
  138. }
  139. internal bool AutoEventWireup {
  140. get { return autoEventWireup; }
  141. }
  142. internal bool EnableViewState {
  143. get { return enableViewState; }
  144. }
  145. }
  146. }