XmlHierarchyData.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. internal 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. IHierarchyData IHierarchyData.GetParent ()
  111. {
  112. return new XmlHierarchyData (item.ParentNode);
  113. }
  114. bool IHierarchyData.HasChildren {
  115. get { return item.HasChildNodes; }
  116. }
  117. object IHierarchyData.Item {
  118. get { return item; }
  119. }
  120. string IHierarchyData.Path {
  121. get {
  122. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  123. XmlNode nod = item;
  124. do {
  125. int n=1;
  126. XmlNode prev = nod.PreviousSibling;
  127. while (prev != null) {
  128. prev = prev.PreviousSibling;
  129. n++;
  130. }
  131. sb.Insert (0, "/*[position()=" + n + "]");
  132. nod = nod.ParentNode;
  133. } while (nod != null && !(nod is XmlDocument));
  134. return sb.ToString ();
  135. }
  136. }
  137. string IHierarchyData.Type {
  138. get { return item.Name; }
  139. }
  140. #endregion
  141. XmlNode item;
  142. class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
  143. public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
  144. {
  145. this.xmlNode = xmlNode;
  146. this.name = name;
  147. }
  148. public override bool CanResetValue (object o)
  149. {
  150. return false;
  151. }
  152. public override void ResetValue (object o)
  153. {
  154. }
  155. public override object GetValue (object o)
  156. {
  157. if (o is XmlHierarchyData) {
  158. switch (name) {
  159. case "##Name##": return xmlNode.Name;
  160. case "##Value##": return xmlNode.Value;
  161. case "##InnerText##": return xmlNode.InnerText;
  162. case null: return String.Empty;
  163. default:
  164. if (xmlNode.Attributes != null) {
  165. XmlAttribute a = xmlNode.Attributes [name];
  166. if (a != null)
  167. return a.Value;
  168. }
  169. break;
  170. }
  171. }
  172. return String.Empty;
  173. }
  174. public override void SetValue (object o, object value)
  175. {
  176. if (o is XmlHierarchyData) {
  177. switch (name) {
  178. case "##Name##": break;
  179. case "##Value##": xmlNode.Value = value.ToString (); break;
  180. case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
  181. case null: break;
  182. default:
  183. if (xmlNode.Attributes != null) {
  184. XmlAttribute a = xmlNode.Attributes [name];
  185. if (a != null)
  186. a.Value = value.ToString ();
  187. }
  188. break;
  189. }
  190. }
  191. }
  192. public override bool ShouldSerializeValue (object o)
  193. {
  194. return o is XmlNode;
  195. }
  196. public override Type ComponentType {
  197. get { return typeof (XmlHierarchyData); }
  198. }
  199. public override bool IsReadOnly {
  200. get { return xmlNode.IsReadOnly; }
  201. }
  202. public override Type PropertyType {
  203. get { return typeof (string); }
  204. }
  205. string name;
  206. XmlNode xmlNode;
  207. }
  208. }
  209. }
  210. #endif