XmlQualifiedName.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public class XmlQualifiedName
  34. {
  35. // Constructors
  36. public XmlQualifiedName ()
  37. : this (string.Empty, string.Empty)
  38. {
  39. }
  40. public XmlQualifiedName (string name)
  41. : this (name, string.Empty)
  42. {
  43. }
  44. public XmlQualifiedName (string name, string ns)
  45. : base ()
  46. {
  47. this.name = (name == null) ? "" : name;
  48. this.ns = (ns == null) ? "" : ns;
  49. this.hash = this.name.GetHashCode () ^ this.ns.GetHashCode ();
  50. }
  51. // Fields
  52. public static readonly XmlQualifiedName Empty = new XmlQualifiedName ();
  53. private readonly string name;
  54. private readonly string ns;
  55. private readonly int hash;
  56. // Properties
  57. public bool IsEmpty
  58. {
  59. get {
  60. return name.Length == 0 && ns.Length == 0;
  61. }
  62. }
  63. public string Name
  64. {
  65. get { return name; }
  66. }
  67. public string Namespace
  68. {
  69. get { return ns; }
  70. }
  71. // Methods
  72. public override bool Equals (object other)
  73. {
  74. return this == (other as XmlQualifiedName);
  75. }
  76. public override int GetHashCode ()
  77. {
  78. return hash;
  79. }
  80. public override string ToString ()
  81. {
  82. if (ns == string.Empty)
  83. return name;
  84. else
  85. return ns + ":" + name;
  86. }
  87. public static string ToString (string name, string ns)
  88. {
  89. if (ns == string.Empty)
  90. return name;
  91. else
  92. return ns + ":" + name;
  93. }
  94. internal static XmlQualifiedName Parse (string name, IXmlNamespaceResolver resolver)
  95. {
  96. int index = name.IndexOf (':');
  97. if (index < 0)
  98. return new XmlQualifiedName (name);
  99. string ns = resolver.LookupNamespace (name.Substring (0, index));
  100. if (ns == null)
  101. throw new ArgumentException ("Invalid qualified name.");
  102. return new XmlQualifiedName (name.Substring (index + 1), ns);
  103. }
  104. // Operators
  105. public static bool operator == (XmlQualifiedName a, XmlQualifiedName b)
  106. {
  107. if((Object)a == (Object)b)
  108. return true;
  109. if((Object)a == null || (Object)b == null)
  110. return false;
  111. return (a.hash == b.hash) && (a.name == b.name) && (a.ns == b.ns);
  112. }
  113. public static bool operator != (XmlQualifiedName a, XmlQualifiedName b)
  114. {
  115. return !(a == b);
  116. }
  117. }
  118. }