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