< Summary

Class:Microsoft.Azure.ServiceBus.Filters.XmlObjectConvertor
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Filters\XmlObjectConvertor.cs
Covered lines:0
Uncovered lines:47
Coverable lines:47
Total lines:107
Line coverage:0% (0 of 47)
Covered branches:0
Total branches:52
Branch coverage:0% (0 of 52)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseValueObject(...)-0%0%
SerializeObject(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Filters\XmlObjectConvertor.cs

#LineLine coverage
 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
 4namespace 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        {
 015            var serializedPrefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.SerializationNa
 016            var type = element.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)).Value;
 17
 018            if (!string.IsNullOrEmpty(serializedPrefix))
 19            {
 020                if (type.Substring(serializedPrefix.Length + 1) == "duration")
 21                {
 022                    return XmlConvert.ToTimeSpan(element.Value);
 23                }
 24            }
 25
 026            var prefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.XmlSchemaNamespace));
 027            if (string.IsNullOrWhiteSpace(prefix))
 28            {
 029                return element.Value;
 30            }
 31
 032            switch (type.Substring(prefix.Length + 1))
 33            {
 34                case "string":
 035                    return element.Value;
 36                case "int":
 037                    return XmlConvert.ToInt32(element.Value);
 38                case "long":
 039                    return XmlConvert.ToInt64(element.Value);
 40                case "boolean":
 041                    return XmlConvert.ToBoolean(element.Value);
 42                case "double":
 043                    return XmlConvert.ToDouble(element.Value);
 44                case "dateTime":
 045                    return XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
 46                case "duration":
 047                    return XmlConvert.ToTimeSpan(element.Value);
 48                default:
 049                    MessagingEventSource.Log.ManagementSerializationException(
 050                            $"{nameof(XmlObjectConvertor)}_{nameof(ParseValueObject)}",
 051                            element.ToString());
 052                    return element.Value;
 53            }
 54        }
 55
 56        internal static XElement SerializeObject(object value)
 57        {
 058            var prefix = "l28";
 059            string type = prefix + ':';
 060            if (value is string)
 61            {
 062                type += "string";
 63            }
 064            else if (value is int)
 65            {
 066                type += "int";
 67            }
 068            else if (value is long)
 69            {
 070                type += "long";
 71            }
 072            else if (value is bool)
 73            {
 074                type += "boolean";
 75            }
 076            else if (value is double)
 77            {
 078                type += "double";
 79            }
 080            else if (value is DateTime)
 81            {
 082                type += "dateTime";
 83            }
 084            else if (value is TimeSpan)
 85            {
 086                type += "duration";
 87            }
 88            else
 89            {
 090                var unknownType = value.GetType().Name;
 091                MessagingEventSource.Log.ManagementSerializationException(
 092                            $"{nameof(XmlObjectConvertor)}_{nameof(SerializeObject)}",
 093                            unknownType);
 94
 095                throw new ServiceBusException(false, "Object is not of supported type: " + unknownType + ". " +
 096                    "Only following types are supported through HTTP: string,int,long,bool,double,DateTime");
 97            }
 98
 099            var element = new XElement(XName.Get("Value", ManagementClientConstants.ServiceBusNamespace),
 0100                new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), type),
 0101                new XAttribute(XNamespace.Xmlns + prefix, ManagementClientConstants.XmlSchemaNamespace),
 0102                value);
 103
 0104            return element;
 105        }
 106    }
 107}