Copied remotely
[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 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Xml;
36 using System.ComponentModel;
37 using AC = System.ComponentModel.AttributeCollection;
38
39 namespace System.Web.UI.WebControls {
40         internal class XmlHierarchyData : IHierarchyData, ICustomTypeDescriptor {
41                 internal XmlHierarchyData (XmlNode item)
42                 {
43                         this.item = item;
44                 }
45                 
46                 public override string ToString ()
47                 {
48                         return item.Name;
49                 }
50                 
51                 #region ICustomTypeDescriptor
52                 AC ICustomTypeDescriptor.GetAttributes ()
53                 {
54                         return AC.Empty;
55                 }
56                 
57                 string ICustomTypeDescriptor.GetClassName ()
58                 {
59                         return "XmlHierarchyData";
60                 }
61                 
62                 string ICustomTypeDescriptor.GetComponentName ()
63                 {
64                         return null;
65                 }
66                 
67                 TypeConverter ICustomTypeDescriptor.GetConverter ()
68                 {
69                         return null;
70                 }
71                 
72                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
73                 {
74                         return null;
75                 }
76                 
77                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
78                 {
79                         return new XmlHierarchyDataPropertyDescriptor (item, "##Name##");
80                 }
81                 
82                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
83                 {
84                         return null;
85                 }
86                 
87                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
88                 {
89                         return null;
90                 }
91                 
92                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attrs)
93                 {
94                         return null;
95                 }
96                 
97                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
98                 {
99                         return ((ICustomTypeDescriptor)this).GetProperties (null);
100                 }
101                 
102                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attrFilter)
103                 {
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##"));
108                         
109                         if (item.Attributes != null)
110                                 foreach (XmlAttribute a in item.Attributes)
111                                         ret.Add (new XmlHierarchyDataPropertyDescriptor (item, a.Name));
112                         
113                         return new PropertyDescriptorCollection ((PropertyDescriptor[]) ret.ToArray (typeof (PropertyDescriptor)));
114                 }
115                 
116                 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
117                 {
118                         if (pd is XmlHierarchyDataPropertyDescriptor)
119                                 return this;
120                         return null;
121                 }
122                 #endregion
123                 
124                 #region IHierarchyData
125                 IHierarchicalEnumerable IHierarchyData.GetChildren ()
126                 {
127                         return new XmlHierarchicalEnumerable (item.ChildNodes);
128                 }
129                 
130                 IHierarchicalEnumerable IHierarchyData.GetParent ()
131                 {
132                         if (item.ParentNode == null)
133                                 return null;
134                         return new XmlHierarchicalEnumerable (item.ParentNode.ChildNodes);
135                 }
136                 
137                 bool IHierarchyData.HasChildren {
138                         get { return item.HasChildNodes; }
139                 }
140                 
141                 object IHierarchyData.Item {
142                         get { return item; }
143                 }
144                 
145                 [MonoTODO]
146                 string IHierarchyData.Path {
147                         get { throw new NotImplementedException (); }
148                 }
149                 
150                 string IHierarchyData.Type {
151                         get { return item.Name; }
152                 }
153                 #endregion
154                         
155                 XmlNode item;
156                 
157                 class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
158                         public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
159                         {
160                                 this.xmlNode = xmlNode;
161                                 this.name = name;
162                         }
163                         
164                         public override bool CanResetValue (object o)
165                         {
166                                 return false;
167                         }
168                         
169                         public override void ResetValue (object o)
170                         {
171                         }
172                         
173                         public override object GetValue (object o)
174                         {
175                                 if (o is XmlHierarchyData) {
176                                         switch (name) {
177                                                 case "##Name##": return xmlNode.Name;
178                                                 case "##Value##": return xmlNode.Value;
179                                                 case "##InnerText##": return xmlNode.InnerText;
180                                                 case null: return String.Empty;
181                                                 default:
182                                                         if (xmlNode.Attributes != null) {
183                                                                 XmlAttribute a = xmlNode.Attributes [name];
184                                                                 if (a != null)
185                                                                         return a.Value;
186                                                         }
187                                                         break;
188                                         }
189                                 }
190                                 return String.Empty;
191                         }
192                         
193                         public override void SetValue (object o, object value)
194                         {
195                                 if (o is XmlHierarchyData) {
196                                         switch (name) {
197                                                 case "##Name##": break;
198                                                 case "##Value##": xmlNode.Value = value.ToString (); break;
199                                                 case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
200                                                 case null: break;
201                                                 default:
202                                                         if (xmlNode.Attributes != null) {
203                                                                 XmlAttribute a = xmlNode.Attributes [name];
204                                                                 if (a != null)
205                                                                         a.Value = value.ToString ();
206                                                         }
207                                                         break;
208                                         }
209                                 }
210                         }
211                         
212                         public override bool ShouldSerializeValue (object o)
213                         {
214                                 return o is XmlNode;
215                         }
216                         
217                         public override Type ComponentType {
218                                 get { return typeof (XmlHierarchyData); }
219                         }
220                         
221                         public override bool IsReadOnly {
222                                 get { return xmlNode.IsReadOnly; }
223                         }
224                         
225                         public override Type PropertyType {
226                                 get { return typeof (string); }
227                         }
228                         
229                         string name;
230                         XmlNode xmlNode;
231                 }
232         
233         }
234         
235 }
236 #endif
237