2010-02-18 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Runtime.Serialization / System.Xml / UniqueId.cs
1 //
2 // UniqueId.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Jonathan Pryor <jpryor@novell.com>
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 #if NET_2_0
30 using System;
31 using System.Collections.Generic;
32 using System.Security;
33
34 namespace System.Xml
35 {
36         public class UniqueId
37         {
38                 Guid guid;
39                 string id;
40
41                 public UniqueId ()
42                         : this (Guid.NewGuid ())
43                 {
44                 }
45
46                 public UniqueId (byte [] id)
47                         : this (id, 0)
48                 {
49                 }
50
51                 public UniqueId (Guid id)
52                 {
53                         this.guid = id;
54                 }
55
56 #if !NET_2_1
57                 [SecurityCritical]
58                 [SecurityTreatAsSafe]
59 #endif
60                 public UniqueId (string value)
61                 {
62                         if (value == null)
63                                 throw new ArgumentNullException ("value cannot be null", "value");
64
65                         if (value.Length == 0)
66                                 throw new FormatException ("UniqueId cannot be zero length");
67
68                         this.id = value;
69                 }
70
71 #if !NET_2_1
72                 [SecurityCritical]
73                 [SecurityTreatAsSafe]
74 #endif
75                 public UniqueId (byte [] id, int offset)
76                 {
77                         if (id == null)
78                                 throw new ArgumentNullException ();
79                         if (offset < 0)
80                                 throw new ArgumentOutOfRangeException ("offset");
81                         if (offset >= id.Length)
82                                 throw new ArgumentException ("id too small.", "offset");
83
84                         if (id.Length - offset != 16)
85                                 throw new ArgumentException ("id and offset provide less than 16 bytes");
86
87                         if (offset == 0)
88                                 guid = new Guid (id);
89                         else {
90                                 List<byte> buf = new List<byte> (id);
91                                 buf.RemoveRange (0, offset);
92                                 guid = new Guid (buf.ToArray ());
93                         }
94                 }
95
96 #if !NET_2_1
97                 [SecurityCritical]
98                 [SecurityTreatAsSafe]
99 #endif
100                 public UniqueId (char [] id, int offset, int count)
101                 {
102                         if (id == null)
103                                 throw new ArgumentNullException ("id");
104                         if (offset < 0 || offset >= id.Length)
105                                 throw new ArgumentOutOfRangeException ("offset");
106                         if (count < 0 || id.Length - offset < count)
107                                 throw new ArgumentOutOfRangeException ("count");
108                         if (count == 0)
109                                 throw new FormatException ();
110
111                         // Does it start with "urn:uuid:"?  If so, it's a Guid.
112                         if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' && 
113                                         id [offset+2] == 'n' && id [offset+3] == ':' &&
114                                         id [offset+4] == 'u' && id [offset+5] == 'u' &&
115                                         id [offset+6] == 'i' && id [offset+7] == 'd' &&
116                                         id [offset+8] == ':') {
117                                 if (count != 45)
118                                         throw new ArgumentOutOfRangeException ("Invalid Guid");
119                                 this.guid = new Guid (new string (id, offset+9, count-9));
120                         } else
121                                 this.id = new string (id, offset, count);
122                 }
123
124                 public int CharArrayLength {
125 #if !NET_2_1
126                         [SecurityCritical]
127                         [SecurityTreatAsSafe]
128 #endif
129                         get {return id != null ? id.Length : 45;}
130                 }
131
132                 public bool IsGuid {
133                         get { return guid != default (Guid); }
134                 }
135
136                 public override bool Equals (Object obj)
137                 {
138                         UniqueId other = obj as UniqueId;
139
140                         if (other == null)
141                                 return false;
142
143                         if (IsGuid && other.IsGuid) {
144                                 return guid.Equals (other.guid);
145                         }
146
147                         return id == other.id;
148                 }
149
150                 [MonoTODO ("Determine semantics when IsGuid==true")]
151                 public override int GetHashCode ()
152                 {
153                         return id != null ? id.GetHashCode () : guid.GetHashCode ();
154                 }
155
156                 public static bool operator == (UniqueId id1, UniqueId id2)
157                 {
158                         return object.Equals (id1, id2);
159                 }
160
161                 public static bool operator != (UniqueId id1, UniqueId id2)
162                 {
163                         return ! (id1 == id2);
164                 }
165
166 #if !NET_2_1
167                 [SecurityCritical]
168                 [SecurityTreatAsSafe]
169 #endif
170                 public int ToCharArray (char [] array, int offset)
171                 {
172                         if (array == null)
173                                 throw new ArgumentNullException ("array");
174                         if (offset < 0 || offset >= array.Length)
175                                 throw new ArgumentOutOfRangeException ("offset");
176
177                         string s = ToString ();
178                         s.CopyTo (0, array, offset, s.Length);
179                         return s.Length;
180                 }
181
182 #if !NET_2_1
183                 [SecurityCritical]
184                 [SecurityTreatAsSafe]
185 #endif
186                 public override string ToString ()
187                 {
188                         if (id == null)
189                                 return "urn:uuid:" + guid;
190
191                         return id;
192                 }
193
194                 public bool TryGetGuid (out Guid guid)
195                 {
196                         if (IsGuid) {
197                                 guid = this.guid;
198                                 return true;
199                         } else {
200                                 guid = default (Guid);
201                                 return false;
202                         }
203                 }
204
205 #if !NET_2_1
206                 [SecurityCritical]
207                 [SecurityTreatAsSafe]
208 #endif
209                 public bool TryGetGuid (byte [] buffer, int offset)
210                 {
211                         if (!IsGuid)
212                                 return false;
213
214                         if (buffer == null)
215                                 throw new ArgumentNullException ("buffer");
216                         if (offset < 0 || offset >= buffer.Length)
217                                 throw new ArgumentOutOfRangeException ("offset");
218                         if (buffer.Length - offset < 16)
219                                 throw new ArgumentOutOfRangeException ("offset");
220
221                         guid.ToByteArray ().CopyTo (buffer, offset);
222                         return true;
223                 }
224         }
225 }
226 #endif