2 // System.Web.UI.WebControls.XmlHierarchyData
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Specialized;
36 using System.ComponentModel;
37 using AC = System.ComponentModel.AttributeCollection;
39 namespace System.Web.UI.WebControls {
40 internal class XmlHierarchyData : IHierarchyData, ICustomTypeDescriptor {
41 internal XmlHierarchyData (XmlNode item)
46 public override string ToString ()
51 #region ICustomTypeDescriptor
52 AC ICustomTypeDescriptor.GetAttributes ()
57 string ICustomTypeDescriptor.GetClassName ()
59 return "XmlHierarchyData";
62 string ICustomTypeDescriptor.GetComponentName ()
67 TypeConverter ICustomTypeDescriptor.GetConverter ()
72 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
77 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
79 return new XmlHierarchyDataPropertyDescriptor (item, "##Name##");
82 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
87 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
92 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attrs)
97 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
99 return ((ICustomTypeDescriptor)this).GetProperties (null);
102 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attrFilter)
104 ArrayList ret = new ArrayList ();
105 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Name##"));
106 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Value##"));
107 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##InnerText##"));
109 if (item.Attributes != null)
110 foreach (XmlAttribute a in item.Attributes)
111 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, a.Name));
113 return new PropertyDescriptorCollection ((PropertyDescriptor[]) ret.ToArray (typeof (PropertyDescriptor)));
116 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
118 if (pd is XmlHierarchyDataPropertyDescriptor)
124 #region IHierarchyData
125 IHierarchicalEnumerable IHierarchyData.GetChildren ()
127 return new XmlHierarchicalEnumerable (item.ChildNodes);
130 IHierarchyData IHierarchyData.GetParent ()
132 return new XmlHierarchyData (item.ParentNode);
135 bool IHierarchyData.HasChildren {
136 get { return item.HasChildNodes; }
139 object IHierarchyData.Item {
143 string IHierarchyData.Path {
145 System.Text.StringBuilder sb = new System.Text.StringBuilder();
149 XmlNode prev = nod.PreviousSibling;
150 while (prev != null) {
151 prev = prev.PreviousSibling;
155 sb.Insert (0, "/*[position()=" + n + "]");
156 nod = nod.ParentNode;
157 } while (nod != null && !(nod is XmlDocument));
159 return sb.ToString ();
163 string IHierarchyData.Type {
164 get { return item.Name; }
170 class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
171 public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
173 this.xmlNode = xmlNode;
177 public override bool CanResetValue (object o)
182 public override void ResetValue (object o)
186 public override object GetValue (object o)
188 if (o is XmlHierarchyData) {
190 case "##Name##": return xmlNode.Name;
191 case "##Value##": return xmlNode.Value;
192 case "##InnerText##": return xmlNode.InnerText;
193 case null: return String.Empty;
195 if (xmlNode.Attributes != null) {
196 XmlAttribute a = xmlNode.Attributes [name];
206 public override void SetValue (object o, object value)
208 if (o is XmlHierarchyData) {
210 case "##Name##": break;
211 case "##Value##": xmlNode.Value = value.ToString (); break;
212 case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
215 if (xmlNode.Attributes != null) {
216 XmlAttribute a = xmlNode.Attributes [name];
218 a.Value = value.ToString ();
225 public override bool ShouldSerializeValue (object o)
230 public override Type ComponentType {
231 get { return typeof (XmlHierarchyData); }
234 public override bool IsReadOnly {
235 get { return xmlNode.IsReadOnly; }
238 public override Type PropertyType {
239 get { return typeof (string); }