XmlHierarchyData.cs 5.9 KB

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