XmlBinaryReaderSession.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. using StringHandle = System.Int64;
  5. namespace System.Xml
  6. {
  7. using System.Xml;
  8. using System.Collections.Generic;
  9. using System.Runtime.Serialization;
  10. public class XmlBinaryReaderSession : IXmlDictionary
  11. {
  12. const int MaxArrayEntries = 2048;
  13. XmlDictionaryString[] strings;
  14. Dictionary<int, XmlDictionaryString> stringDict;
  15. public XmlBinaryReaderSession()
  16. {
  17. }
  18. public XmlDictionaryString Add(int id, string value)
  19. {
  20. if (id < 0)
  21. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(SR.GetString(SR.XmlInvalidID)));
  22. if (value == null)
  23. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  24. XmlDictionaryString xmlString;
  25. if (TryLookup(id, out xmlString))
  26. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlIDDefined)));
  27. xmlString = new XmlDictionaryString(this, value, id);
  28. if (id >= MaxArrayEntries)
  29. {
  30. if (stringDict == null)
  31. this.stringDict = new Dictionary<int, XmlDictionaryString>();
  32. this.stringDict.Add(id, xmlString);
  33. }
  34. else
  35. {
  36. if (strings == null)
  37. {
  38. strings = new XmlDictionaryString[Math.Max(id + 1, 16)];
  39. }
  40. else if (id >= strings.Length)
  41. {
  42. XmlDictionaryString[] newStrings = new XmlDictionaryString[Math.Min(Math.Max(id + 1, strings.Length * 2), MaxArrayEntries)];
  43. Array.Copy(strings, newStrings, strings.Length);
  44. strings = newStrings;
  45. }
  46. strings[id] = xmlString;
  47. }
  48. return xmlString;
  49. }
  50. public bool TryLookup(int key, out XmlDictionaryString result)
  51. {
  52. if (strings != null && key >= 0 && key < strings.Length)
  53. {
  54. result = strings[key];
  55. return result != null;
  56. }
  57. else if (key >= MaxArrayEntries)
  58. {
  59. if (this.stringDict != null)
  60. return this.stringDict.TryGetValue(key, out result);
  61. }
  62. result = null;
  63. return false;
  64. }
  65. public bool TryLookup(string value, out XmlDictionaryString result)
  66. {
  67. if (value == null)
  68. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  69. if (strings != null)
  70. {
  71. for (int i = 0; i < strings.Length; i++)
  72. {
  73. XmlDictionaryString s = strings[i];
  74. if (s != null && s.Value == value)
  75. {
  76. result = s;
  77. return true;
  78. }
  79. }
  80. }
  81. if (this.stringDict != null)
  82. {
  83. foreach (XmlDictionaryString s in this.stringDict.Values)
  84. {
  85. if (s.Value == value)
  86. {
  87. result = s;
  88. return true;
  89. }
  90. }
  91. }
  92. result = null;
  93. return false;
  94. }
  95. public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result)
  96. {
  97. if (value == null)
  98. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  99. if (value.Dictionary != this)
  100. {
  101. result = null;
  102. return false;
  103. }
  104. result = value;
  105. return true;
  106. }
  107. public void Clear()
  108. {
  109. if (strings != null)
  110. Array.Clear(strings, 0, strings.Length);
  111. if (this.stringDict != null)
  112. this.stringDict.Clear();
  113. }
  114. }
  115. }