|  |  | 1 |  | // Copyright (c) Microsoft. All rights reserved. | 
|  |  | 2 |  | // Licensed under the MIT license. See LICENSE file in the project root for full license information. | 
|  |  | 3 |  |  | 
|  |  | 4 |  | namespace Microsoft.Azure.ServiceBus.Filters | 
|  |  | 5 |  | { | 
|  |  | 6 |  |     using System; | 
|  |  | 7 |  |     using System.Xml; | 
|  |  | 8 |  |     using System.Xml.Linq; | 
|  |  | 9 |  |     using Microsoft.Azure.ServiceBus.Management; | 
|  |  | 10 |  |  | 
|  |  | 11 |  |     internal class XmlObjectConvertor | 
|  |  | 12 |  |     { | 
|  |  | 13 |  |         internal static object ParseValueObject(XElement element) | 
|  |  | 14 |  |         { | 
|  | 0 | 15 |  |             var serializedPrefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.SerializationNa | 
|  | 0 | 16 |  |             var type = element.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)).Value; | 
|  |  | 17 |  |  | 
|  | 0 | 18 |  |             if (!string.IsNullOrEmpty(serializedPrefix)) | 
|  |  | 19 |  |             { | 
|  | 0 | 20 |  |                 if (type.Substring(serializedPrefix.Length + 1) == "duration") | 
|  |  | 21 |  |                 { | 
|  | 0 | 22 |  |                     return XmlConvert.ToTimeSpan(element.Value); | 
|  |  | 23 |  |                 } | 
|  |  | 24 |  |             } | 
|  |  | 25 |  |  | 
|  | 0 | 26 |  |             var prefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.XmlSchemaNamespace)); | 
|  | 0 | 27 |  |             if (string.IsNullOrWhiteSpace(prefix)) | 
|  |  | 28 |  |             { | 
|  | 0 | 29 |  |                 return element.Value; | 
|  |  | 30 |  |             } | 
|  |  | 31 |  |  | 
|  | 0 | 32 |  |             switch (type.Substring(prefix.Length + 1)) | 
|  |  | 33 |  |             { | 
|  |  | 34 |  |                 case "string": | 
|  | 0 | 35 |  |                     return element.Value; | 
|  |  | 36 |  |                 case "int": | 
|  | 0 | 37 |  |                     return XmlConvert.ToInt32(element.Value); | 
|  |  | 38 |  |                 case "long": | 
|  | 0 | 39 |  |                     return XmlConvert.ToInt64(element.Value); | 
|  |  | 40 |  |                 case "boolean": | 
|  | 0 | 41 |  |                     return XmlConvert.ToBoolean(element.Value); | 
|  |  | 42 |  |                 case "double": | 
|  | 0 | 43 |  |                     return XmlConvert.ToDouble(element.Value); | 
|  |  | 44 |  |                 case "dateTime": | 
|  | 0 | 45 |  |                     return XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc); | 
|  |  | 46 |  |                 case "duration": | 
|  | 0 | 47 |  |                     return XmlConvert.ToTimeSpan(element.Value); | 
|  |  | 48 |  |                 default: | 
|  | 0 | 49 |  |                     MessagingEventSource.Log.ManagementSerializationException( | 
|  | 0 | 50 |  |                             $"{nameof(XmlObjectConvertor)}_{nameof(ParseValueObject)}", | 
|  | 0 | 51 |  |                             element.ToString()); | 
|  | 0 | 52 |  |                     return element.Value; | 
|  |  | 53 |  |             } | 
|  |  | 54 |  |         } | 
|  |  | 55 |  |  | 
|  |  | 56 |  |         internal static XElement SerializeObject(object value) | 
|  |  | 57 |  |         { | 
|  | 0 | 58 |  |             var prefix = "l28"; | 
|  | 0 | 59 |  |             string type = prefix + ':'; | 
|  | 0 | 60 |  |             if (value is string) | 
|  |  | 61 |  |             { | 
|  | 0 | 62 |  |                 type += "string"; | 
|  |  | 63 |  |             } | 
|  | 0 | 64 |  |             else if (value is int) | 
|  |  | 65 |  |             { | 
|  | 0 | 66 |  |                 type += "int"; | 
|  |  | 67 |  |             } | 
|  | 0 | 68 |  |             else if (value is long) | 
|  |  | 69 |  |             { | 
|  | 0 | 70 |  |                 type += "long"; | 
|  |  | 71 |  |             } | 
|  | 0 | 72 |  |             else if (value is bool) | 
|  |  | 73 |  |             { | 
|  | 0 | 74 |  |                 type += "boolean"; | 
|  |  | 75 |  |             } | 
|  | 0 | 76 |  |             else if (value is double) | 
|  |  | 77 |  |             { | 
|  | 0 | 78 |  |                 type += "double"; | 
|  |  | 79 |  |             } | 
|  | 0 | 80 |  |             else if (value is DateTime) | 
|  |  | 81 |  |             { | 
|  | 0 | 82 |  |                 type += "dateTime"; | 
|  |  | 83 |  |             } | 
|  | 0 | 84 |  |             else if (value is TimeSpan) | 
|  |  | 85 |  |             { | 
|  | 0 | 86 |  |                 type += "duration"; | 
|  |  | 87 |  |             } | 
|  |  | 88 |  |             else | 
|  |  | 89 |  |             { | 
|  | 0 | 90 |  |                 var unknownType = value.GetType().Name; | 
|  | 0 | 91 |  |                 MessagingEventSource.Log.ManagementSerializationException( | 
|  | 0 | 92 |  |                             $"{nameof(XmlObjectConvertor)}_{nameof(SerializeObject)}", | 
|  | 0 | 93 |  |                             unknownType); | 
|  |  | 94 |  |  | 
|  | 0 | 95 |  |                 throw new ServiceBusException(false, "Object is not of supported type: " + unknownType + ". " + | 
|  | 0 | 96 |  |                     "Only following types are supported through HTTP: string,int,long,bool,double,DateTime"); | 
|  |  | 97 |  |             } | 
|  |  | 98 |  |  | 
|  | 0 | 99 |  |             var element = new XElement(XName.Get("Value", ManagementClientConstants.ServiceBusNamespace), | 
|  | 0 | 100 |  |                 new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), type), | 
|  | 0 | 101 |  |                 new XAttribute(XNamespace.Xmlns + prefix, ManagementClientConstants.XmlSchemaNamespace), | 
|  | 0 | 102 |  |                 value); | 
|  |  | 103 |  |  | 
|  | 0 | 104 |  |             return element; | 
|  |  | 105 |  |         } | 
|  |  | 106 |  |     } | 
|  |  | 107 |  | } |