MonikerUtility.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Text;
  10. internal static class MonikerUtility
  11. {
  12. internal static string Getkeyword(string moniker, out MonikerHelper.MonikerAttribute keyword)
  13. {
  14. moniker = moniker.TrimStart();
  15. int indexOfEqualSign = moniker.IndexOf("=", StringComparison.Ordinal);
  16. if (indexOfEqualSign == -1)
  17. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.NoEqualSignFound, moniker)));
  18. int indexOfComma = moniker.IndexOf(",", StringComparison.Ordinal);
  19. if (indexOfComma != -1 && indexOfComma < indexOfEqualSign)
  20. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.NoEqualSignFound, moniker)));
  21. string suspectedKeyword = moniker.Substring(0, indexOfEqualSign).Trim();
  22. suspectedKeyword = suspectedKeyword.ToLower(System.Globalization.CultureInfo.InvariantCulture);
  23. foreach (MonikerHelper.KeywordInfo keywordInfo in MonikerHelper.KeywordInfo.KeywordCollection)
  24. {
  25. if (suspectedKeyword == keywordInfo.Name)
  26. {
  27. keyword = keywordInfo.Attrib;
  28. moniker = moniker.Substring(indexOfEqualSign + 1).TrimStart();
  29. return moniker;
  30. }
  31. }
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.UnknownMonikerKeyword, suspectedKeyword)));
  33. }
  34. internal static string GetValue(string moniker, out string val)
  35. {
  36. StringBuilder value = new StringBuilder();
  37. int index = 0;
  38. moniker = moniker.Trim();
  39. if (string.IsNullOrEmpty(moniker))
  40. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.KewordMissingValue)));
  41. switch (moniker[index])
  42. {
  43. case '"':
  44. case '\'':
  45. {
  46. char quoteToCompare = moniker[index];
  47. index++;
  48. for (; index < moniker.Length; index++)
  49. {
  50. if (moniker[index] == quoteToCompare)
  51. {
  52. if ((index < (moniker.Length - 1)) && (moniker[index + 1] == quoteToCompare))
  53. {
  54. value.Append(quoteToCompare);
  55. index++;
  56. }
  57. else
  58. {
  59. break;
  60. }
  61. }
  62. else
  63. value.Append(moniker[index]);
  64. }
  65. if (index < moniker.Length)
  66. {
  67. index++;
  68. if (index < moniker.Length)
  69. {
  70. moniker = moniker.Substring(index);
  71. moniker = moniker.Trim();
  72. if (!String.IsNullOrEmpty(moniker))
  73. {
  74. if (moniker[0] == ',')
  75. {
  76. moniker = moniker.Substring(1);
  77. moniker = moniker.Trim();
  78. }
  79. else
  80. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.BadlyTerminatedValue, value.ToString())));
  81. }
  82. }
  83. else
  84. moniker = "";
  85. }
  86. else
  87. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.MissingQuote, value.ToString())));
  88. break;
  89. }
  90. default:
  91. {
  92. for (; (index < moniker.Length) && (moniker[index] != ','); index++)
  93. value.Append(moniker[index]);
  94. if (index < moniker.Length)
  95. {
  96. index++;
  97. if (index < moniker.Length)
  98. {
  99. moniker = moniker.Substring(index);
  100. moniker = moniker.Trim();
  101. }
  102. }
  103. else
  104. moniker = "";
  105. break;
  106. }
  107. }
  108. val = value.ToString().Trim();
  109. return moniker;
  110. }
  111. internal static void Parse(string displayName, ref Dictionary<MonikerHelper.MonikerAttribute, string> propertyTable)
  112. {
  113. int indexOfMonikerData = displayName.IndexOf(":", StringComparison.Ordinal);
  114. if (indexOfMonikerData == -1)
  115. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.MonikerMissingColon)));
  116. string monikerParams = displayName.Substring(indexOfMonikerData + 1).Trim();
  117. MonikerHelper.MonikerAttribute keyword;
  118. string value;
  119. while (!string.IsNullOrEmpty(monikerParams))
  120. {
  121. monikerParams = Getkeyword(monikerParams, out keyword);
  122. propertyTable.TryGetValue(keyword, out value);
  123. if (!String.IsNullOrEmpty(value))
  124. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MonikerSyntaxException(SR.GetString(SR.RepeatedKeyword)));
  125. monikerParams = GetValue(monikerParams, out value);
  126. propertyTable[keyword] = value;
  127. }
  128. }
  129. }
  130. }