FaultReason.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. public class FaultReason
  9. {
  10. SynchronizedReadOnlyCollection<FaultReasonText> translations;
  11. public FaultReason(FaultReasonText translation)
  12. {
  13. if (translation == null)
  14. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("translation");
  15. Init(translation);
  16. }
  17. public FaultReason(string text)
  18. {
  19. // Let FaultReasonText constructor throw
  20. Init(new FaultReasonText(text));
  21. }
  22. internal FaultReason(string text, string xmlLang)
  23. {
  24. // Let FaultReasonText constructor throw
  25. Init(new FaultReasonText(text, xmlLang));
  26. }
  27. internal FaultReason(string text, CultureInfo cultureInfo)
  28. {
  29. // Let FaultReasonText constructor throw
  30. Init(new FaultReasonText(text, cultureInfo));
  31. }
  32. public FaultReason(IEnumerable<FaultReasonText> translations)
  33. {
  34. if (translations == null)
  35. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("translations"));
  36. int count = 0;
  37. foreach (FaultReasonText faultReasonText in translations)
  38. count++;
  39. if (count == 0)
  40. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.AtLeastOneFaultReasonMustBeSpecified), "translations"));
  41. FaultReasonText[] array = new FaultReasonText[count];
  42. int index = 0;
  43. foreach (FaultReasonText faultReasonText in translations)
  44. {
  45. if (faultReasonText == null)
  46. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("translations", SR.GetString(SR.NoNullTranslations));
  47. array[index++] = faultReasonText;
  48. }
  49. Init(array);
  50. }
  51. void Init(FaultReasonText translation)
  52. {
  53. Init(new FaultReasonText[] { translation });
  54. }
  55. void Init(FaultReasonText[] translations)
  56. {
  57. this.translations = new SynchronizedReadOnlyCollection<FaultReasonText>(new object(), Array.AsReadOnly<FaultReasonText>(translations));
  58. }
  59. public FaultReasonText GetMatchingTranslation()
  60. {
  61. return GetMatchingTranslation(CultureInfo.CurrentCulture);
  62. }
  63. // [....], This function should always return a translation so that a fault can be surfaced.
  64. public FaultReasonText GetMatchingTranslation(CultureInfo cultureInfo)
  65. {
  66. if (cultureInfo == null)
  67. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("cultureInfo"));
  68. // If there's only one translation, use it
  69. if (translations.Count == 1)
  70. return translations[0];
  71. // Search for an exact match
  72. for (int i = 0; i < translations.Count; i++)
  73. if (translations[i].Matches(cultureInfo))
  74. return translations[i];
  75. // If no exact match is found, proceed by looking for the a translation with a language that is a parent of the current culture
  76. if (translations.Count == 0)
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.NoMatchingTranslationFoundForFaultText)));
  78. // Search for a more general language
  79. #pragma warning suppress 56506
  80. string localLang = cultureInfo.Name;
  81. while (true)
  82. {
  83. int idx = localLang.LastIndexOf('-');
  84. // We don't want to accept xml:lang=""
  85. if (idx == -1)
  86. break;
  87. // Clip off the last subtag and look for a match
  88. localLang = localLang.Substring(0, idx);
  89. for (int i = 0; i < translations.Count; i++)
  90. if (translations[i].XmlLang == localLang)
  91. return translations[i];
  92. }
  93. // Return the first translation if no match is found
  94. return translations[0];
  95. }
  96. public SynchronizedReadOnlyCollection<FaultReasonText> Translations
  97. {
  98. get { return translations; }
  99. }
  100. public override string ToString()
  101. {
  102. if (translations.Count == 0)
  103. return string.Empty;
  104. return GetMatchingTranslation().Text;
  105. }
  106. }
  107. }