XmlDictionaryString.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Xml
  5. {
  6. using System;
  7. using System.Xml;
  8. using System.Text;
  9. using System.Diagnostics;
  10. using System.Runtime.Serialization;
  11. public class XmlDictionaryString
  12. {
  13. internal const int MinKey = 0;
  14. internal const int MaxKey = int.MaxValue / 4;
  15. IXmlDictionary dictionary;
  16. string value;
  17. int key;
  18. byte[] buffer;
  19. static EmptyStringDictionary emptyStringDictionary = new EmptyStringDictionary();
  20. public XmlDictionaryString(IXmlDictionary dictionary, string value, int key)
  21. {
  22. if (dictionary == null)
  23. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dictionary"));
  24. if (value == null)
  25. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  26. if (key < MinKey || key > MaxKey)
  27. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("key", SR.GetString(SR.ValueMustBeInRange, MinKey, MaxKey)));
  28. this.dictionary = dictionary;
  29. this.value = value;
  30. this.key = key;
  31. }
  32. static internal string GetString(XmlDictionaryString s)
  33. {
  34. if (s == null)
  35. return null;
  36. return s.Value;
  37. }
  38. static public XmlDictionaryString Empty
  39. {
  40. get
  41. {
  42. return emptyStringDictionary.EmptyString;
  43. }
  44. }
  45. public IXmlDictionary Dictionary
  46. {
  47. get
  48. {
  49. return dictionary;
  50. }
  51. }
  52. public int Key
  53. {
  54. get
  55. {
  56. return key;
  57. }
  58. }
  59. public string Value
  60. {
  61. get
  62. {
  63. return value;
  64. }
  65. }
  66. internal byte[] ToUTF8()
  67. {
  68. if (buffer == null)
  69. buffer = System.Text.Encoding.UTF8.GetBytes(value);
  70. return buffer;
  71. }
  72. public override string ToString()
  73. {
  74. return value;
  75. }
  76. class EmptyStringDictionary : IXmlDictionary
  77. {
  78. XmlDictionaryString empty;
  79. public EmptyStringDictionary()
  80. {
  81. empty = new XmlDictionaryString(this, string.Empty, 0);
  82. }
  83. public XmlDictionaryString EmptyString
  84. {
  85. get
  86. {
  87. return empty;
  88. }
  89. }
  90. public bool TryLookup(string value, out XmlDictionaryString result)
  91. {
  92. if (value == null)
  93. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  94. if (value.Length == 0)
  95. {
  96. result = empty;
  97. return true;
  98. }
  99. result = null;
  100. return false;
  101. }
  102. public bool TryLookup(int key, out XmlDictionaryString result)
  103. {
  104. if (key == 0)
  105. {
  106. result = empty;
  107. return true;
  108. }
  109. result = null;
  110. return false;
  111. }
  112. public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result)
  113. {
  114. if (value == null)
  115. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  116. if (value.Dictionary != this)
  117. {
  118. result = null;
  119. return false;
  120. }
  121. result = value;
  122. return true;
  123. }
  124. }
  125. }
  126. }