1 | /* |
---|
2 | XML-RPC.NET library |
---|
3 | Copyright (c) 2001-2009, Charles Cook <charlescook@cookcomputing.com> |
---|
4 | |
---|
5 | Permission is hereby granted, free of charge, to any person |
---|
6 | obtaining a copy of this software and associated documentation |
---|
7 | files (the "Software"), to deal in the Software without restriction, |
---|
8 | including without limitation the rights to use, copy, modify, merge, |
---|
9 | publish, distribute, sublicense, and/or sell copies of the Software, |
---|
10 | and to permit persons to whom the Software is furnished to do so, |
---|
11 | subject to the following conditions: |
---|
12 | |
---|
13 | The above copyright notice and this permission notice shall be |
---|
14 | included in all copies or substantial portions of the Software. |
---|
15 | |
---|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
---|
18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
23 | DEALINGS IN THE SOFTWARE. |
---|
24 | */ |
---|
25 | using System; |
---|
26 | using System.Collections; |
---|
27 | |
---|
28 | namespace CookComputing.XmlRpc |
---|
29 | { |
---|
30 | public class XmlRpcStruct : Hashtable |
---|
31 | { |
---|
32 | private ArrayList _keys = new ArrayList(); |
---|
33 | private ArrayList _values = new ArrayList(); |
---|
34 | |
---|
35 | public override void Add(object key, object value) |
---|
36 | { |
---|
37 | if (!(key is string)) |
---|
38 | { |
---|
39 | throw new ArgumentException("XmlRpcStruct key must be a string."); |
---|
40 | } |
---|
41 | if (XmlRpcServiceInfo.GetXmlRpcType(value.GetType()) |
---|
42 | == XmlRpcType.tInvalid) |
---|
43 | { |
---|
44 | throw new ArgumentException(String.Format( |
---|
45 | "Type {0} cannot be mapped to an XML-RPC type", value.GetType())); |
---|
46 | } |
---|
47 | base.Add(key, value); |
---|
48 | _keys.Add(key); |
---|
49 | _values.Add(value); |
---|
50 | } |
---|
51 | |
---|
52 | public override object this[object key] |
---|
53 | { |
---|
54 | get |
---|
55 | { |
---|
56 | return base[key]; |
---|
57 | } |
---|
58 | set |
---|
59 | { |
---|
60 | if (!(key is string)) |
---|
61 | { |
---|
62 | throw new ArgumentException("XmlRpcStruct key must be a string."); |
---|
63 | } |
---|
64 | if (XmlRpcServiceInfo.GetXmlRpcType(value.GetType()) |
---|
65 | == XmlRpcType.tInvalid) |
---|
66 | { |
---|
67 | throw new ArgumentException(String.Format( |
---|
68 | "Type {0} cannot be mapped to an XML-RPC type", value.GetType())); |
---|
69 | } |
---|
70 | base[key] = value; |
---|
71 | _keys.Add(key); |
---|
72 | _values.Add(value); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | public override bool Equals(Object obj) |
---|
77 | { |
---|
78 | if (obj.GetType() != typeof(XmlRpcStruct)) |
---|
79 | return false; |
---|
80 | XmlRpcStruct xmlRpcStruct = (XmlRpcStruct)obj; |
---|
81 | if (this.Keys.Count != xmlRpcStruct.Count) |
---|
82 | return false; |
---|
83 | foreach (String key in this.Keys) |
---|
84 | { |
---|
85 | if (!xmlRpcStruct.ContainsKey(key)) |
---|
86 | return false; |
---|
87 | if (!this[key].Equals(xmlRpcStruct[key])) |
---|
88 | return false; |
---|
89 | } |
---|
90 | return true; |
---|
91 | } |
---|
92 | |
---|
93 | public override int GetHashCode() |
---|
94 | { |
---|
95 | int hash = 0; |
---|
96 | foreach (object obj in Values) |
---|
97 | { |
---|
98 | hash ^= obj.GetHashCode(); |
---|
99 | } |
---|
100 | return hash; |
---|
101 | } |
---|
102 | |
---|
103 | public override void Clear() |
---|
104 | { |
---|
105 | base.Clear(); |
---|
106 | _keys.Clear(); |
---|
107 | _values.Clear(); |
---|
108 | } |
---|
109 | |
---|
110 | public new IDictionaryEnumerator GetEnumerator() |
---|
111 | { |
---|
112 | return new XmlRpcStruct.Enumerator(_keys, _values); |
---|
113 | } |
---|
114 | |
---|
115 | public override ICollection Keys |
---|
116 | { |
---|
117 | get |
---|
118 | { |
---|
119 | return _keys; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | public override void Remove(object key) |
---|
124 | { |
---|
125 | base.Remove(key); |
---|
126 | int idx = _keys.IndexOf(key); |
---|
127 | if (idx >= 0) |
---|
128 | { |
---|
129 | _keys.RemoveAt(idx); |
---|
130 | _values.RemoveAt(idx); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | public override ICollection Values |
---|
135 | { |
---|
136 | get |
---|
137 | { |
---|
138 | return _values; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | private class Enumerator : IDictionaryEnumerator |
---|
143 | { |
---|
144 | private ArrayList _keys; |
---|
145 | private ArrayList _values; |
---|
146 | private int _index; |
---|
147 | |
---|
148 | public Enumerator(ArrayList keys, ArrayList values) |
---|
149 | { |
---|
150 | _keys = keys; |
---|
151 | _values = values; |
---|
152 | _index = -1; |
---|
153 | } |
---|
154 | |
---|
155 | public void Reset() |
---|
156 | { |
---|
157 | _index = -1; |
---|
158 | } |
---|
159 | |
---|
160 | public object Current |
---|
161 | { |
---|
162 | get |
---|
163 | { |
---|
164 | CheckIndex(); |
---|
165 | return new DictionaryEntry(_keys[_index], _values[_index]); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | public bool MoveNext() |
---|
170 | { |
---|
171 | _index++; |
---|
172 | if (_index >= _keys.Count) |
---|
173 | return false; |
---|
174 | else |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | public DictionaryEntry Entry |
---|
179 | { |
---|
180 | get |
---|
181 | { |
---|
182 | CheckIndex(); |
---|
183 | return new DictionaryEntry(_keys[_index], _values[_index]); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | public object Key |
---|
188 | { |
---|
189 | get |
---|
190 | { |
---|
191 | CheckIndex(); |
---|
192 | return _keys[_index]; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | public object Value |
---|
197 | { |
---|
198 | get |
---|
199 | { |
---|
200 | CheckIndex(); |
---|
201 | return _values[_index]; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | private void CheckIndex() |
---|
206 | { |
---|
207 | if (_index < 0 || _index >= _keys.Count) |
---|
208 | throw new InvalidOperationException( |
---|
209 | "Enumeration has either not started or has already finished."); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|