XmlDictionary.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // XmlDictionary.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. namespace System.Xml
  32. {
  33. public class XmlDictionary : IXmlDictionary
  34. {
  35. internal class EmptyDictionary : XmlDictionary
  36. {
  37. public static readonly EmptyDictionary Instance =
  38. new EmptyDictionary ();
  39. public EmptyDictionary ()
  40. : base (1)
  41. {
  42. }
  43. }
  44. static XmlDictionary empty = new XmlDictionary (true);
  45. public static IXmlDictionary Empty {
  46. get { return empty; }
  47. }
  48. readonly bool is_readonly;
  49. Dictionary<string, XmlDictionaryString> dict;
  50. List<XmlDictionaryString> list;
  51. public XmlDictionary ()
  52. {
  53. dict = new Dictionary<string, XmlDictionaryString> ();
  54. list = new List<XmlDictionaryString> ();
  55. }
  56. public XmlDictionary (int capacity)
  57. {
  58. dict = new Dictionary<string, XmlDictionaryString> (capacity);
  59. list = new List<XmlDictionaryString> (capacity);
  60. }
  61. // for static empty.
  62. private XmlDictionary (bool isReadOnly)
  63. : this (1)
  64. {
  65. is_readonly = isReadOnly;
  66. }
  67. public virtual XmlDictionaryString Add (string value)
  68. {
  69. if (is_readonly)
  70. throw new InvalidOperationException ();
  71. XmlDictionaryString ret;
  72. if (dict.TryGetValue (value, out ret))
  73. return ret;
  74. ret = new XmlDictionaryString (this, value, dict.Count);
  75. dict.Add (value, ret);
  76. list.Add (ret);
  77. return ret;
  78. }
  79. public virtual bool TryLookup (
  80. int key, out XmlDictionaryString result)
  81. {
  82. if (key < 0 || dict.Count <= key) {
  83. result = null;
  84. return false;
  85. }
  86. result = list [key];
  87. return true;
  88. }
  89. public virtual bool TryLookup (string value,
  90. out XmlDictionaryString result)
  91. {
  92. if (value == null)
  93. throw new ArgumentNullException ();
  94. return dict.TryGetValue (value, out result);
  95. }
  96. public virtual bool TryLookup (XmlDictionaryString value,
  97. out XmlDictionaryString result)
  98. {
  99. if (value == null)
  100. throw new ArgumentNullException ();
  101. if (value.Dictionary != this) {
  102. result = null;
  103. return false;
  104. }
  105. for (int i = 0; i < list.Count; i++) {
  106. if (object.ReferenceEquals (list [i], value)) {
  107. result = value;
  108. return true;
  109. }
  110. }
  111. result = null;
  112. return false;
  113. }
  114. }
  115. }