diff options
Diffstat (limited to 'OpenSim/Server/Base/ServerUtils.cs')
-rw-r--r-- | OpenSim/Server/Base/ServerUtils.cs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/OpenSim/Server/Base/ServerUtils.cs b/OpenSim/Server/Base/ServerUtils.cs index 0a36bbe..6c2b3ed 100644 --- a/OpenSim/Server/Base/ServerUtils.cs +++ b/OpenSim/Server/Base/ServerUtils.cs | |||
@@ -183,5 +183,119 @@ namespace OpenSim.Server.Base | |||
183 | 183 | ||
184 | return result; | 184 | return result; |
185 | } | 185 | } |
186 | |||
187 | public static string BuildQueryString(Dictionary<string, string> data) | ||
188 | { | ||
189 | string qstring = String.Empty; | ||
190 | |||
191 | foreach(KeyValuePair<string, string> kvp in data) | ||
192 | { | ||
193 | string part; | ||
194 | if (kvp.Value != String.Empty) | ||
195 | { | ||
196 | part = System.Web.HttpUtility.UrlEncode(kvp.Key) + | ||
197 | "=" + System.Web.HttpUtility.UrlEncode(kvp.Value); | ||
198 | } | ||
199 | else | ||
200 | { | ||
201 | part = System.Web.HttpUtility.UrlEncode(kvp.Key); | ||
202 | } | ||
203 | |||
204 | if (qstring != String.Empty) | ||
205 | qstring += "&"; | ||
206 | |||
207 | qstring += part; | ||
208 | } | ||
209 | |||
210 | return qstring; | ||
211 | } | ||
212 | |||
213 | public static string BuildXmlResponse(Dictionary<string, object> data) | ||
214 | { | ||
215 | XmlDocument doc = new XmlDocument(); | ||
216 | |||
217 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
218 | "", ""); | ||
219 | |||
220 | doc.AppendChild(xmlnode); | ||
221 | |||
222 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
223 | ""); | ||
224 | |||
225 | doc.AppendChild(rootElement); | ||
226 | |||
227 | BuildXmlData(rootElement, data); | ||
228 | |||
229 | return doc.InnerXml; | ||
230 | } | ||
231 | |||
232 | private static void BuildXmlData(XmlElement parent, Dictionary<string, object> data) | ||
233 | { | ||
234 | foreach (KeyValuePair<string, object> kvp in data) | ||
235 | { | ||
236 | XmlElement elem = parent.OwnerDocument.CreateElement("", | ||
237 | kvp.Key, ""); | ||
238 | |||
239 | if (kvp.Value is Dictionary<string, object>) | ||
240 | { | ||
241 | XmlAttribute type = parent.OwnerDocument.CreateAttribute("", | ||
242 | "type", ""); | ||
243 | type.Value = "List"; | ||
244 | |||
245 | elem.Attributes.Append(type); | ||
246 | |||
247 | BuildXmlData(elem, (Dictionary<string, object>)kvp.Value); | ||
248 | } | ||
249 | else | ||
250 | { | ||
251 | elem.AppendChild(parent.OwnerDocument.CreateTextNode( | ||
252 | kvp.Value.ToString())); | ||
253 | } | ||
254 | |||
255 | parent.AppendChild(elem); | ||
256 | } | ||
257 | } | ||
258 | |||
259 | public static Dictionary<string, object> ParseXmlResponse(string data) | ||
260 | { | ||
261 | Dictionary<string, object> ret = new Dictionary<string, object>(); | ||
262 | |||
263 | XmlDocument doc = new XmlDocument(); | ||
264 | |||
265 | doc.LoadXml(data); | ||
266 | |||
267 | XmlNodeList rootL = doc.GetElementsByTagName("ServerResponse"); | ||
268 | |||
269 | if (rootL.Count != 1) | ||
270 | return ret; | ||
271 | |||
272 | XmlNode rootNode = rootL[0]; | ||
273 | |||
274 | ret = ParseElement(rootNode); | ||
275 | |||
276 | return ret; | ||
277 | } | ||
278 | |||
279 | private static Dictionary<string, object> ParseElement(XmlNode element) | ||
280 | { | ||
281 | Dictionary<string, object> ret = new Dictionary<string, object>(); | ||
282 | |||
283 | XmlNodeList partL = element.ChildNodes; | ||
284 | |||
285 | foreach (XmlNode part in partL) | ||
286 | { | ||
287 | XmlNode type = part.Attributes.GetNamedItem("Type"); | ||
288 | if (type == null || type.Value != "List") | ||
289 | { | ||
290 | ret[part.Name] = part.InnerText; | ||
291 | } | ||
292 | else | ||
293 | { | ||
294 | ret[part.Name] = ParseElement(part); | ||
295 | } | ||
296 | } | ||
297 | |||
298 | return ret; | ||
299 | } | ||
186 | } | 300 | } |
187 | } | 301 | } |