merge -r 61110:61111
[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                 IHierarchyData IHierarchyData.GetParent ()
131                 {
132                         return new XmlHierarchyData (item.ParentNode);
133                 }
134                 
135                 bool IHierarchyData.HasChildren {
136                         get { return item.HasChildNodes; }
137                 }
138                 
139                 object IHierarchyData.Item {
140                         get { return item; }
141                 }
142                 
143                 string IHierarchyData.Path {
144                         get { 
145                                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
146                                 XmlNode nod = item;
147                                 do {
148                                         int n=1;
149                                         XmlNode prev = nod.PreviousSibling;
150                                         while (prev != null) {
151                                                 prev = prev.PreviousSibling;
152                                                 n++;
153                                         }
154                                         
155                                         sb.Insert (0, "/*[position()=" + n);
156                                         nod = nod.ParentNode;
157                                 } while (nod != null && !(nod is XmlDocument));
158                                 
159                                 return sb.ToString ();
160                         }
161                 }
162                 
163                 string IHierarchyData.Type {
164                         get { return item.Name; }
165                 }
166                 #endregion
167                         
168                 XmlNode item;
169                 
170                 class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
171                         public XmlHierarchyDataPropertyDescriptor (XmlNode xmlNode, string name) : base (name, null)
172                         {
173                                 this.xmlNode = xmlNode;
174                                 this.name = name;
175                         }
176                         
177                         public override bool CanResetValue (object o)
178                         {
179                                 return false;
180                         }
181                         
182                         public override void ResetValue (object o)
183                         {
184                         }
185                         
186                         public override object GetValue (object o)
187                         {
188                                 if (o is XmlHierarchyData) {
189                                         switch (name) {
190                                                 case "##Name##": return xmlNode.Name;
191                                                 case "##Value##": return xmlNode.Value;
192                                                 case "##InnerText##": return xmlNode.InnerText;
193                                                 case null: return String.Empty;
194                                                 default:
195                                                         if (xmlNode.Attributes != null) {
196                                                                 XmlAttribute a = xmlNode.Attributes [name];
197                                                                 if (a != null)
198                                                                         return a.Value;
199                                                         }
200                                                         break;
201                                         }
202                                 }
203                                 return String.Empty;
204                         }
205                         
206                         public override void SetValue (object o, object value)
207                         {
208                                 if (o is XmlHierarchyData) {
209                                         switch (name) {
210                                                 case "##Name##": break;
211                                                 case "##Value##": xmlNode.Value = value.ToString (); break;
212                                                 case "##InnerText##": xmlNode.InnerText = value.ToString (); break;
213                                                 case null: break;
214                                                 default:
215                                                         if (xmlNode.Attributes != null) {
216                                                                 XmlAttribute a = xmlNode.Attributes [name];
217                                                                 if (a != null)
218                                                                         a.Value = value.ToString ();
219                                                         }
220                                                         break;
221                                         }
222                                 }
223                         }
224                         
225                         public override bool ShouldSerializeValue (object o)
226                         {
227                                 return o is XmlNode;
228                         }
229                         
230                         public override Type ComponentType {
231                                 get { return typeof (XmlHierarchyData); }
232                         }
233                         
234                         public override bool IsReadOnly {
235                                 get { return xmlNode.IsReadOnly; }
236                         }
237                         
238                         public override Type PropertyType {
239                                 get { return typeof (string); }
240                         }
241                         
242                         string name;
243                         XmlNode xmlNode;
244                 }
245         
246         }
247         
248 }
249 #endif
250