| | 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 |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Xml.Linq; |
| | 8 | | using Microsoft.Azure.ServiceBus.Management; |
| | 9 | |
|
| | 10 | | internal static class FilterExtensions |
| | 11 | | { |
| | 12 | | public static Filter ParseFromXElement(XElement xElement) |
| | 13 | | { |
| 0 | 14 | | var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)); |
| 0 | 15 | | if (attribute == null) |
| | 16 | | { |
| 0 | 17 | | return null; |
| | 18 | | } |
| | 19 | |
|
| 0 | 20 | | switch (attribute.Value) |
| | 21 | | { |
| | 22 | | case "SqlFilter": |
| 0 | 23 | | return SqlFilterExtensions.ParseFromXElement(xElement); |
| | 24 | | case "CorrelationFilter": |
| 0 | 25 | | return CorrelationFilterExtensions.ParseFromXElement(xElement); |
| | 26 | | case "TrueFilter": |
| 0 | 27 | | return new TrueFilter(); |
| | 28 | | case "FalseFilter": |
| 0 | 29 | | return new FalseFilter(); |
| | 30 | | default: |
| 0 | 31 | | MessagingEventSource.Log.ManagementSerializationException( |
| 0 | 32 | | $"{nameof(FilterExtensions)}_{nameof(ParseFromXElement)}", |
| 0 | 33 | | xElement.ToString()); |
| 0 | 34 | | return null; |
| | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public static XElement Serialize(this Filter filter) |
| | 39 | | { |
| | 40 | | switch (filter) |
| | 41 | | { |
| | 42 | | case SqlFilter sqlFilter: |
| | 43 | | switch (sqlFilter) |
| | 44 | | { |
| | 45 | | case TrueFilter _: |
| 0 | 46 | | return sqlFilter.Serialize(nameof(TrueFilter)); |
| | 47 | |
|
| | 48 | | case FalseFilter _: |
| 0 | 49 | | return sqlFilter.Serialize(nameof(FalseFilter)); |
| | 50 | |
|
| | 51 | | default: |
| 0 | 52 | | return sqlFilter.Serialize(nameof(SqlFilter)); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | case CorrelationFilter correlationFilter: |
| 0 | 56 | | return correlationFilter.Serialize(); |
| | 57 | |
|
| | 58 | | default: |
| 0 | 59 | | throw new NotImplementedException($"filter type {filter.GetType().Name} is not supported."); |
| | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |