XmlDictionary.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. namespace System.Xml
  33. {
  34. public class XmlDictionary : IXmlDictionary
  35. {
  36. internal class EmptyDictionary : XmlDictionary
  37. {
  38. public static readonly EmptyDictionary Instance =
  39. new EmptyDictionary ();
  40. public EmptyDictionary ()
  41. : base (1)
  42. {
  43. }
  44. }
  45. static XmlDictionary empty = new XmlDictionary (true);
  46. public static IXmlDictionary Empty {
  47. get { return empty; }
  48. }
  49. readonly bool is_readonly;
  50. Dictionary<string, XmlDictionaryString> dict;
  51. List<XmlDictionaryString> list;
  52. public XmlDictionary ()
  53. {
  54. dict = new Dictionary<string, XmlDictionaryString> ();
  55. list = new List<XmlDictionaryString> ();
  56. }
  57. public XmlDictionary (int capacity)
  58. {
  59. dict = new Dictionary<string, XmlDictionaryString> (capacity);
  60. list = new List<XmlDictionaryString> (capacity);
  61. }
  62. // for static empty.
  63. private XmlDictionary (bool isReadOnly)
  64. : this (1)
  65. {
  66. is_readonly = isReadOnly;
  67. }
  68. public virtual XmlDictionaryString Add (string value)
  69. {
  70. if (is_readonly)
  71. throw new InvalidOperationException ();
  72. XmlDictionaryString ret;
  73. if (dict.TryGetValue (value, out ret))
  74. return ret;
  75. ret = new XmlDictionaryString (this, value, dict.Count);
  76. dict.Add (value, ret);
  77. list.Add (ret);
  78. return ret;
  79. }
  80. public virtual bool TryLookup (
  81. int key, out XmlDictionaryString result)
  82. {
  83. if (key < 0 || dict.Count <= key) {
  84. result = null;
  85. return false;
  86. }
  87. result = list [key];
  88. return true;
  89. }
  90. public virtual bool TryLookup (string value,
  91. out XmlDictionaryString result)
  92. {
  93. if (value == null)
  94. throw new ArgumentNullException ();
  95. return dict.TryGetValue (value, out result);
  96. }
  97. public virtual bool TryLookup (XmlDictionaryString value,
  98. out XmlDictionaryString result)
  99. {
  100. if (value == null)
  101. throw new ArgumentNullException ();
  102. if (value.Dictionary != this) {
  103. result = null;
  104. return false;
  105. }
  106. for (int i = 0; i < list.Count; i++) {
  107. if (object.ReferenceEquals (list [i], value)) {
  108. result = value;
  109. return true;
  110. }
  111. }
  112. result = null;
  113. return false;
  114. }
  115. }
  116. }
  117. #endif