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