XmlHierarchyData.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.Web.UI.WebControls.XmlHierarchyData
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. #if NET_1_2
  10. using System.Collections;
  11. using System.Collections.Specialized;
  12. using System.Text;
  13. using System.Xml;
  14. using System.ComponentModel;
  15. using AC = System.ComponentModel.AttributeCollection;
  16. namespace System.Web.UI.WebControls {
  17. public class XmlHierarchyData : IHierarchyData, ICustomTypeDescriptor {
  18. internal XmlHierarchyData (XmlNode item)
  19. {
  20. this.item = item;
  21. }
  22. public override string ToString ()
  23. {
  24. return item.Name;
  25. }
  26. #region ICustomTypeDescriptor
  27. AC ICustomTypeDescriptor.GetAttributes ()
  28. {
  29. return AC.Empty;
  30. }
  31. string ICustomTypeDescriptor.GetClassName ()
  32. {
  33. return "XmlHierarchyData";
  34. }
  35. string ICustomTypeDescriptor.GetComponentName ()
  36. {
  37. return null;
  38. }
  39. TypeConverter ICustomTypeDescriptor.GetConverter ()
  40. {
  41. return null;
  42. }
  43. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  44. {
  45. return null;
  46. }
  47. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  48. {
  49. return new XmlHierarchyDataPropertyDescriptor (item, "##Name##");
  50. }
  51. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  52. {
  53. return null;
  54. }
  55. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  56. {
  57. return null;
  58. }
  59. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attrs)
  60. {
  61. return null;
  62. }
  63. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  64. {
  65. return ((ICustomTypeDescriptor)this).GetProperties (null);
  66. }
  67. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attrFilter)
  68. {
  69. ArrayList ret = new ArrayList ();
  70. ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Name##"));
  71. ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Value##"));
  72. ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##InnerText##"));
  73. if (item.Attributes != null)
  74. foreach (XmlAttribute a in item.Attributes)
  75. ret.Add (new XmlHierarchyDataPropertyDescriptor (item, a.Name));
  76. return new PropertyDescriptorCollection ((PropertyDescriptor[]) ret.ToArray (typeof (PropertyDescriptor)));
  77. }
  78. object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  79. {
  80. if (pd is XmlHierarchyDataPropertyDescriptor)
  81. return this;
  82. return null;
  83. }
  84. #endregion
  85. #region IHierarchyData
  86. IHierarchicalEnumerable IHierarchyData.GetChildren ()
  87. {
  88. return new XmlHierarchicalEnumerable (item.ChildNodes);
  89. }
  90. IHierarchicalEnumerable IHierarchyData.GetParent ()
  91. {
  92. if (item.ParentNode == null)
  93. return null;
  94. return new XmlHierarchicalEnumerable (item.ParentNode.ChildNodes);
  95. }
  96. bool IHierarchyData.HasChildren {
  97. get { return item.HasChildNodes; }
  98. }
  99. object IHierarchyData.Item {
  100. get { return item; }
  101. }
  102. [MonoTODO]
  103. string IHierarchyData.Path {
  104. get { throw new NotImplementedException (); }
  105. }
  106. string IHierarchyData.Type {
  107. get { return item.Name; }
  108. }
  109. #endregion
  110. XmlNode item;
  111. class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
  112. public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
  113. {
  114. this.xmlNode = xmlNode;
  115. this.name = name;
  116. }
  117. public override bool CanResetValue (object o)
  118. {
  119. return false;
  120. }
  121. public override void ResetValue (object o)
  122. {
  123. }
  124. public override object GetValue (object o)
  125. {
  126. if (o is XmlHierarchyData) {
  127. switch (name) {
  128. case "##Name##": return xmlNode.Name;
  129. case "##Value##": return xmlNode.Value;
  130. case "##InnerText##": return xmlNode.InnerText;
  131. case null: return String.Empty;
  132. default:
  133. if (xmlNode.Attributes != null) {
  134. XmlAttribute a = xmlNode.Attributes [name];
  135. if (a != null)
  136. return a.Value;
  137. }
  138. break;
  139. }
  140. }
  141. return String.Empty;
  142. }
  143. public override void SetValue (object o, object value)
  144. {
  145. if (o is XmlHierarchyData) {
  146. switch (name) {
  147. case "##Name##": break;
  148. case "##Value##": xmlNode.Value = value.ToString (); break;
  149. case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
  150. case null: break;
  151. default:
  152. if (xmlNode.Attributes != null) {
  153. XmlAttribute a = xmlNode.Attributes [name];
  154. if (a != null)
  155. a.Value = value.ToString ();
  156. }
  157. break;
  158. }
  159. }
  160. }
  161. public override bool ShouldSerializeValue (object o)
  162. {
  163. return o is XmlNode;
  164. }
  165. public override Type ComponentType {
  166. get { return typeof (XmlHierarchyData); }
  167. }
  168. public override bool IsReadOnly {
  169. get { return xmlNode.IsReadOnly; }
  170. }
  171. public override Type PropertyType {
  172. get { return typeof (string); }
  173. }
  174. string name;
  175. XmlNode xmlNode;
  176. }
  177. }
  178. }
  179. #endif