TemplateBuilder.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // System.Web.UI.TemplateBuilder
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc. (http://www.ximian.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.Collections;
  30. using System.Reflection;
  31. using System.Security.Permissions;
  32. #if NET_2_0
  33. using System.ComponentModel;
  34. using System.Collections.Generic;
  35. #endif
  36. namespace System.Web.UI {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. public class TemplateBuilder : ControlBuilder, ITemplate {
  41. string text;
  42. TemplateContainerAttribute containerAttribute;
  43. #if NET_2_0
  44. TemplateInstanceAttribute instanceAttribute;
  45. List <TemplateBinding> bindings;
  46. #endif
  47. public TemplateBuilder ()
  48. {
  49. }
  50. internal TemplateBuilder (ICustomAttributeProvider prov)
  51. {
  52. object[] ats = prov.GetCustomAttributes (typeof (TemplateContainerAttribute), true);
  53. if (ats.Length > 0)
  54. containerAttribute = (TemplateContainerAttribute) ats [0];
  55. #if NET_2_0
  56. ats = prov.GetCustomAttributes (typeof (TemplateInstanceAttribute), true);
  57. if (ats.Length > 0)
  58. instanceAttribute = (TemplateInstanceAttribute) ats [0];
  59. #endif
  60. }
  61. public virtual string Text {
  62. get { return text; }
  63. set { text = value; }
  64. }
  65. internal Type ContainerType {
  66. get { return containerAttribute != null ? containerAttribute.ContainerType : null; }
  67. }
  68. #if NET_2_0
  69. internal TemplateInstance? TemplateInstance {
  70. get { return instanceAttribute != null ? instanceAttribute.Instances : (TemplateInstance?)null; }
  71. }
  72. internal BindingDirection BindingDirection {
  73. get { return containerAttribute != null ? containerAttribute.BindingDirection : BindingDirection.TwoWay; }
  74. }
  75. internal void RegisterBoundProperty (Type controlType, string controlProperty, string controlId, string fieldName)
  76. {
  77. if (bindings == null)
  78. bindings = new List <TemplateBinding> ();
  79. bindings.Add (new TemplateBinding (controlType, controlProperty, controlId, fieldName));
  80. }
  81. internal ICollection Bindings {
  82. get { return bindings; }
  83. }
  84. public override object BuildObject ()
  85. {
  86. return base.BuildObject ();
  87. }
  88. #endif
  89. public override void Init (TemplateParser parser,
  90. ControlBuilder parentBuilder,
  91. Type type,
  92. string tagName,
  93. string ID,
  94. IDictionary attribs)
  95. {
  96. // enough?
  97. if (parser != null)
  98. FileName = parser.InputFile;
  99. base.Init (parser, parentBuilder, type, tagName, ID, attribs);
  100. }
  101. public virtual void InstantiateIn (Control container)
  102. {
  103. CreateChildren (container);
  104. }
  105. public override bool NeedsTagInnerText ()
  106. {
  107. return false;
  108. }
  109. public override void SetTagInnerText (string text)
  110. {
  111. this.text = text;
  112. }
  113. }
  114. #if NET_2_0
  115. internal class TemplateBinding
  116. {
  117. public Type ControlType;
  118. public string ControlProperty;
  119. public string ControlId;
  120. public string FieldName;
  121. public TemplateBinding (Type controlType, string controlProperty, string controlId, string fieldName)
  122. {
  123. ControlType = controlType;
  124. ControlProperty = controlProperty;
  125. ControlId = controlId;
  126. FieldName = fieldName;
  127. }
  128. }
  129. #endif
  130. }