XmlQualifiedName.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Xml.XmlQualifiedName.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // (C) Ximian, Inc.
  7. //
  8. // Modified:
  9. // 21st June 2002 : Ajay kumar Dwivedi ([email protected])
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. namespace System.Xml
  32. {
  33. #if NET_2_0
  34. [Serializable]
  35. #endif
  36. public class XmlQualifiedName
  37. {
  38. // Constructors
  39. public XmlQualifiedName ()
  40. : this (string.Empty, string.Empty)
  41. {
  42. }
  43. public XmlQualifiedName (string name)
  44. : this (name, string.Empty)
  45. {
  46. }
  47. public XmlQualifiedName (string name, string ns)
  48. : base ()
  49. {
  50. this.name = (name == null) ? "" : name;
  51. this.ns = (ns == null) ? "" : ns;
  52. this.hash = this.name.GetHashCode () ^ this.ns.GetHashCode ();
  53. }
  54. // Fields
  55. public static readonly XmlQualifiedName Empty = new XmlQualifiedName ();
  56. private readonly string name;
  57. private readonly string ns;
  58. private readonly int hash;
  59. // Properties
  60. public bool IsEmpty
  61. {
  62. get {
  63. return name.Length == 0 && ns.Length == 0;
  64. }
  65. }
  66. public string Name
  67. {
  68. get { return name; }
  69. }
  70. public string Namespace
  71. {
  72. get { return ns; }
  73. }
  74. // Methods
  75. public override bool Equals (object other)
  76. {
  77. return this == (other as XmlQualifiedName);
  78. }
  79. public override int GetHashCode ()
  80. {
  81. return hash;
  82. }
  83. public override string ToString ()
  84. {
  85. if (ns == string.Empty)
  86. return name;
  87. else
  88. return ns + ":" + name;
  89. }
  90. public static string ToString (string name, string ns)
  91. {
  92. if (ns == string.Empty)
  93. return name;
  94. else
  95. return ns + ":" + name;
  96. }
  97. internal static XmlQualifiedName Parse (string name, IXmlNamespaceResolver resolver)
  98. {
  99. int index = name.IndexOf (':');
  100. if (index < 0)
  101. return new XmlQualifiedName (name);
  102. string ns = resolver.LookupNamespace (name.Substring (0, index));
  103. if (ns == null)
  104. throw new ArgumentException ("Invalid qualified name.");
  105. return new XmlQualifiedName (name.Substring (index + 1), ns);
  106. }
  107. // Operators
  108. public static bool operator == (XmlQualifiedName a, XmlQualifiedName b)
  109. {
  110. if((Object)a == (Object)b)
  111. return true;
  112. if((Object)a == null || (Object)b == null)
  113. return false;
  114. return (a.hash == b.hash) && (a.name == b.name) && (a.ns == b.ns);
  115. }
  116. public static bool operator != (XmlQualifiedName a, XmlQualifiedName b)
  117. {
  118. return !(a == b);
  119. }
  120. }
  121. }