| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Globalization; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | public class FaultReason |
| | | 12 | | { |
| | 0 | 13 | | public FaultReason(FaultReasonText translation) |
| | | 14 | | { |
| | 0 | 15 | | if (translation == null) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(translation)); |
| | | 18 | | } |
| | | 19 | | |
| | 0 | 20 | | Init(translation); |
| | 0 | 21 | | } |
| | | 22 | | |
| | 242 | 23 | | public FaultReason(string text) |
| | | 24 | | { |
| | | 25 | | // Let FaultReasonText constructor throw |
| | 242 | 26 | | Init(new FaultReasonText(text)); |
| | 242 | 27 | | } |
| | | 28 | | |
| | 69 | 29 | | internal FaultReason(string text, CultureInfo cultureInfo) |
| | | 30 | | { |
| | | 31 | | // Let FaultReasonText constructor throw |
| | 69 | 32 | | Init(new FaultReasonText(text, cultureInfo)); |
| | 69 | 33 | | } |
| | | 34 | | |
| | 92 | 35 | | public FaultReason(IEnumerable<FaultReasonText> translations) |
| | | 36 | | { |
| | 92 | 37 | | if (translations == null) |
| | | 38 | | { |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(translations)); |
| | | 40 | | } |
| | | 41 | | |
| | 92 | 42 | | int count = 0; |
| | 552 | 43 | | foreach (FaultReasonText faultReasonText in translations) |
| | | 44 | | { |
| | 184 | 45 | | count++; |
| | | 46 | | } |
| | | 47 | | |
| | 92 | 48 | | if (count == 0) |
| | | 49 | | { |
| | 0 | 50 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(translations), |
| | 0 | 51 | | SR.AtLeastOneFaultReasonMustBeSpecified); |
| | | 52 | | } |
| | | 53 | | |
| | 92 | 54 | | FaultReasonText[] array = new FaultReasonText[count]; |
| | 92 | 55 | | int index = 0; |
| | 552 | 56 | | foreach (FaultReasonText faultReasonText in translations) |
| | | 57 | | { |
| | 184 | 58 | | array[index++] = faultReasonText ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof( |
| | 184 | 59 | | SR.NoNullTranslations); |
| | | 60 | | } |
| | 92 | 61 | | Init(array); |
| | 92 | 62 | | } |
| | | 63 | | |
| | | 64 | | private void Init(FaultReasonText translation) |
| | | 65 | | { |
| | 311 | 66 | | Init(new FaultReasonText[] { translation }); |
| | 311 | 67 | | } |
| | | 68 | | |
| | | 69 | | private void Init(FaultReasonText[] translations) |
| | | 70 | | { |
| | 403 | 71 | | Translations = new ReadOnlyCollection<FaultReasonText>(translations); |
| | 403 | 72 | | } |
| | | 73 | | |
| | | 74 | | public FaultReasonText GetMatchingTranslation() |
| | | 75 | | { |
| | 5 | 76 | | return GetMatchingTranslation(CultureInfo.CurrentCulture); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public FaultReasonText GetMatchingTranslation(CultureInfo cultureInfo) |
| | | 80 | | { |
| | 52 | 81 | | if (cultureInfo == null) |
| | | 82 | | { |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(cultureInfo)); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // If there's only one translation, use it |
| | 52 | 87 | | if (Translations.Count == 1) |
| | | 88 | | { |
| | 48 | 89 | | return Translations[0]; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // Search for an exact match |
| | 24 | 93 | | for (int i = 0; i < Translations.Count; i++) |
| | | 94 | | { |
| | 8 | 95 | | if (Translations[i].Matches(cultureInfo)) |
| | | 96 | | { |
| | 0 | 97 | | return Translations[i]; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | // If no exact match is found, proceed by looking for the a translation with a language that is a parent of |
| | | 102 | | |
| | 4 | 103 | | if (Translations.Count == 0) |
| | | 104 | | { |
| | 0 | 105 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 106 | | new ArgumentException(SR.NoMatchingTranslationFoundForFaultText)); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // Search for a more general language |
| | 4 | 110 | | string localLang = cultureInfo.Name; |
| | 0 | 111 | | while (true) |
| | | 112 | | { |
| | 4 | 113 | | int idx = localLang.LastIndexOf('-'); |
| | | 114 | | |
| | | 115 | | // We don't want to accept xml:lang="" |
| | 4 | 116 | | if (idx == -1) |
| | | 117 | | { |
| | | 118 | | break; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | // Clip off the last subtag and look for a match |
| | 0 | 122 | | localLang = localLang.Substring(0, idx); |
| | | 123 | | |
| | 0 | 124 | | for (int i = 0; i < Translations.Count; i++) |
| | | 125 | | { |
| | 0 | 126 | | if (Translations[i].XmlLang == localLang) |
| | | 127 | | { |
| | 0 | 128 | | return Translations[i]; |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // Return the first translation if no match is found |
| | 4 | 134 | | return Translations[0]; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | // public on full framework, but exposes a SynchronizedReadOnlyCollection |
| | 1495 | 138 | | internal ReadOnlyCollection<FaultReasonText> Translations { get; private set; } |
| | | 139 | | |
| | | 140 | | public override string ToString() |
| | | 141 | | { |
| | 5 | 142 | | if (Translations.Count == 0) |
| | | 143 | | { |
| | 0 | 144 | | return string.Empty; |
| | | 145 | | } |
| | | 146 | | |
| | 5 | 147 | | return GetMatchingTranslation().Text; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |