NamespacePrefix.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #region Copyright and License
  2. //
  3. // Fizzler - CSS Selector Engine for Microsoft .NET Framework
  4. // Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
  5. //
  6. // This library is free software; you can redistribute it and/or modify it under
  7. // the terms of the GNU Lesser General Public License as published by the Free
  8. // Software Foundation; either version 3 of the License, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful, but WITHOUT
  12. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. // details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with this library; if not, write to the Free Software Foundation, Inc.,
  18. // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. #endregion
  21. #pragma warning disable
  22. namespace Fizzler
  23. {
  24. using System;
  25. /// <summary>
  26. /// Represent a type or attribute name.
  27. /// </summary>
  28. [Serializable]
  29. public struct NamespacePrefix
  30. {
  31. /// <summary>
  32. /// Represents a name from either the default or any namespace
  33. /// in a target document, depending on whether a default namespace is
  34. /// in effect or not.
  35. /// </summary>
  36. public static readonly NamespacePrefix None = new NamespacePrefix(null);
  37. /// <summary>
  38. /// Represents an empty namespace.
  39. /// </summary>
  40. public static readonly NamespacePrefix Empty = new NamespacePrefix(string.Empty);
  41. /// <summary>
  42. /// Represents any namespace.
  43. /// </summary>
  44. public static readonly NamespacePrefix Any = new NamespacePrefix("*");
  45. /// <summary>
  46. /// Initializes an instance with a namespace prefix specification.
  47. /// </summary>
  48. public NamespacePrefix(string text) : this()
  49. {
  50. Text = text;
  51. }
  52. /// <summary>
  53. /// Gets the raw text value of this instance.
  54. /// </summary>
  55. public string Text { get; private set; }
  56. /// <summary>
  57. /// Indicates whether this instance represents a name
  58. /// from either the default or any namespace in a target
  59. /// document, depending on whether a default namespace is
  60. /// in effect or not.
  61. /// </summary>
  62. public bool IsNone { get { return Text == null; } }
  63. /// <summary>
  64. /// Indicates whether this instance represents a name
  65. /// from any namespace (including one without one)
  66. /// in a target document.
  67. /// </summary>
  68. public bool IsAny
  69. {
  70. get { return !IsNone && Text.Length == 1 && Text[0] == '*'; }
  71. }
  72. /// <summary>
  73. /// Indicates whether this instance represents a name
  74. /// without a namespace in a target document.
  75. /// </summary>
  76. public bool IsEmpty { get { return !IsNone && Text.Length == 0; } }
  77. /// <summary>
  78. /// Indicates whether this instance represents a name from a
  79. /// specific namespace or not.
  80. /// </summary>
  81. public bool IsSpecific { get { return !IsNone && !IsAny; } }
  82. /// <summary>
  83. /// Indicates whether this instance and a specified object are equal.
  84. /// </summary>
  85. public override bool Equals(object obj)
  86. {
  87. return obj is NamespacePrefix && Equals((NamespacePrefix)obj);
  88. }
  89. /// <summary>
  90. /// Indicates whether this instance and another are equal.
  91. /// </summary>
  92. public bool Equals(NamespacePrefix other)
  93. {
  94. return Text == other.Text;
  95. }
  96. /// <summary>
  97. /// Returns the hash code for this instance.
  98. /// </summary>
  99. public override int GetHashCode()
  100. {
  101. return IsNone ? 0 : Text.GetHashCode();
  102. }
  103. /// <summary>
  104. /// Returns a string representation of this instance.
  105. /// </summary>
  106. public override string ToString()
  107. {
  108. return IsNone ? "(none)" : Text;
  109. }
  110. /// <summary>
  111. /// Formats this namespace together with a name.
  112. /// </summary>
  113. public string Format(string name)
  114. {
  115. if (name == null) throw new ArgumentNullException("name");
  116. if (name.Length == 0) throw new ArgumentException(null, "name");
  117. return Text + (IsNone ? null : "|") + name;
  118. }
  119. }
  120. }
  121. #pragma warning restore