UniqueId.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // UniqueId.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Jonathan Pryor <[email protected]>
  7. //
  8. // Copyright (C) 2005,2007,2009 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Security;
  32. namespace System.Xml
  33. {
  34. public class UniqueId
  35. {
  36. Guid guid;
  37. string id;
  38. public UniqueId ()
  39. : this (Guid.NewGuid ())
  40. {
  41. }
  42. public UniqueId (byte [] id)
  43. : this (id, 0)
  44. {
  45. }
  46. public UniqueId (Guid id)
  47. {
  48. this.guid = id;
  49. }
  50. public UniqueId (string value)
  51. {
  52. if (value == null)
  53. throw new ArgumentNullException ("value cannot be null", "value");
  54. if (value.Length == 0)
  55. throw new FormatException ("UniqueId cannot be zero length");
  56. this.id = value;
  57. }
  58. public UniqueId (byte [] id, int offset)
  59. {
  60. if (id == null)
  61. throw new ArgumentNullException ();
  62. if (offset < 0)
  63. throw new ArgumentOutOfRangeException ("offset");
  64. if (offset >= id.Length)
  65. throw new ArgumentException ("id too small.", "offset");
  66. if (id.Length - offset != 16)
  67. throw new ArgumentException ("id and offset provide less than 16 bytes");
  68. if (offset == 0)
  69. guid = new Guid (id);
  70. else {
  71. List<byte> buf = new List<byte> (id);
  72. buf.RemoveRange (0, offset);
  73. guid = new Guid (buf.ToArray ());
  74. }
  75. }
  76. public UniqueId (char [] id, int offset, int count)
  77. {
  78. if (id == null)
  79. throw new ArgumentNullException ("id");
  80. if (offset < 0 || offset >= id.Length)
  81. throw new ArgumentOutOfRangeException ("offset");
  82. if (count < 0 || id.Length - offset < count)
  83. throw new ArgumentOutOfRangeException ("count");
  84. if (count == 0)
  85. throw new FormatException ();
  86. // Does it start with "urn:uuid:"? If so, it's a Guid.
  87. if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' &&
  88. id [offset+2] == 'n' && id [offset+3] == ':' &&
  89. id [offset+4] == 'u' && id [offset+5] == 'u' &&
  90. id [offset+6] == 'i' && id [offset+7] == 'd' &&
  91. id [offset+8] == ':') {
  92. if (count != 45)
  93. throw new ArgumentOutOfRangeException ("Invalid Guid");
  94. this.guid = new Guid (new string (id, offset+9, count-9));
  95. } else
  96. this.id = new string (id, offset, count);
  97. }
  98. public int CharArrayLength {
  99. get {return id != null ? id.Length : 45;}
  100. }
  101. public bool IsGuid {
  102. get { return guid != default (Guid); }
  103. }
  104. public override bool Equals (Object obj)
  105. {
  106. UniqueId other = obj as UniqueId;
  107. if (other == null)
  108. return false;
  109. if (IsGuid && other.IsGuid) {
  110. return guid.Equals (other.guid);
  111. }
  112. return id == other.id;
  113. }
  114. [MonoTODO ("Determine semantics when IsGuid==true")]
  115. public override int GetHashCode ()
  116. {
  117. return id != null ? id.GetHashCode () : guid.GetHashCode ();
  118. }
  119. public static bool operator == (UniqueId id1, UniqueId id2)
  120. {
  121. return object.Equals (id1, id2);
  122. }
  123. public static bool operator != (UniqueId id1, UniqueId id2)
  124. {
  125. return ! (id1 == id2);
  126. }
  127. public int ToCharArray (char [] array, int offset)
  128. {
  129. if (array == null)
  130. throw new ArgumentNullException ("array");
  131. if (offset < 0 || offset >= array.Length)
  132. throw new ArgumentOutOfRangeException ("offset");
  133. string s = ToString ();
  134. s.CopyTo (0, array, offset, s.Length);
  135. return s.Length;
  136. }
  137. public override string ToString ()
  138. {
  139. if (id == null)
  140. return "urn:uuid:" + guid;
  141. return id;
  142. }
  143. public bool TryGetGuid (out Guid guid)
  144. {
  145. if (IsGuid) {
  146. guid = this.guid;
  147. return true;
  148. } else {
  149. guid = default (Guid);
  150. return false;
  151. }
  152. }
  153. public bool TryGetGuid (byte [] buffer, int offset)
  154. {
  155. if (!IsGuid)
  156. return false;
  157. if (buffer == null)
  158. throw new ArgumentNullException ("buffer");
  159. if (offset < 0 || offset >= buffer.Length)
  160. throw new ArgumentOutOfRangeException ("offset");
  161. if (buffer.Length - offset < 16)
  162. throw new ArgumentOutOfRangeException ("offset");
  163. guid.ToByteArray ().CopyTo (buffer, offset);
  164. return true;
  165. }
  166. }
  167. }