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.
31 using System.Collections;
32 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using AC = System.ComponentModel.AttributeCollection;
38 namespace System.Web.UI.WebControls {
39 internal class XmlHierarchyData : IHierarchyData, ICustomTypeDescriptor {
40 internal XmlHierarchyData (XmlNode item)
45 public override string ToString ()
50 #region ICustomTypeDescriptor
51 AC ICustomTypeDescriptor.GetAttributes ()
56 string ICustomTypeDescriptor.GetClassName ()
58 return "XmlHierarchyData";
61 string ICustomTypeDescriptor.GetComponentName ()
66 TypeConverter ICustomTypeDescriptor.GetConverter ()
71 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
76 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
78 return new XmlHierarchyDataPropertyDescriptor (item, "##Name##");
81 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
86 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
91 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attrs)
96 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
98 return ((ICustomTypeDescriptor)this).GetProperties (null);
101 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attrFilter)
103 ArrayList ret = new ArrayList ();
104 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Name##"));
105 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##Value##"));
106 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, "##InnerText##"));
108 if (item.Attributes != null)
109 foreach (XmlAttribute a in item.Attributes)
110 ret.Add (new XmlHierarchyDataPropertyDescriptor (item, a.Name));
112 return new PropertyDescriptorCollection ((PropertyDescriptor[]) ret.ToArray (typeof (PropertyDescriptor)));
115 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
117 if (pd is XmlHierarchyDataPropertyDescriptor)
123 #region IHierarchyData
124 IHierarchicalEnumerable IHierarchyData.GetChildren ()
126 return new XmlHierarchicalEnumerable (item.ChildNodes);
129 IHierarchyData IHierarchyData.GetParent ()
131 return new XmlHierarchyData (item.ParentNode);
134 bool IHierarchyData.HasChildren {
135 get { return item.HasChildNodes; }
138 object IHierarchyData.Item {
142 string IHierarchyData.Path {
144 System.Text.StringBuilder sb = new System.Text.StringBuilder();
148 XmlNode prev = nod.PreviousSibling;
149 while (prev != null) {
150 prev = prev.PreviousSibling;
154 sb.Insert (0, "/*[position()=" + n + "]");
155 nod = nod.ParentNode;
156 } while (nod != null && !(nod is XmlDocument));
158 return sb.ToString ();
162 string IHierarchyData.Type {
163 get { return item.Name; }
169 class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
170 public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
172 this.xmlNode = xmlNode;
176 public override bool CanResetValue (object o)
181 public override void ResetValue (object o)
185 public override object GetValue (object o)
187 if (o is XmlHierarchyData) {
189 case "##Name##": return xmlNode.Name;
190 case "##Value##": return xmlNode.Value;
191 case "##InnerText##": return xmlNode.InnerText;
192 case null: return String.Empty;
194 if (xmlNode.Attributes != null) {
195 XmlAttribute a = xmlNode.Attributes [name];
205 public override void SetValue (object o, object value)
207 if (o is XmlHierarchyData) {
209 case "##Name##": break;
210 case "##Value##": xmlNode.Value = value.ToString (); break;
211 case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
214 if (xmlNode.Attributes != null) {
215 XmlAttribute a = xmlNode.Attributes [name];
217 a.Value = value.ToString ();
224 public override bool ShouldSerializeValue (object o)
229 public override Type ComponentType {
230 get { return typeof (XmlHierarchyData); }
233 public override bool IsReadOnly {
234 get { return xmlNode.IsReadOnly; }
237 public override Type PropertyType {
238 get { return typeof (string); }