Update PointConverter.cs
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XName.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 #if NET_2_0
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.IO;
33 using System.Runtime.Serialization;
34 using System.Text;
35 using System.Xml;
36
37 using XPI = System.Xml.Linq.XProcessingInstruction;
38
39 namespace System.Xml.Linq
40 {
41         [Serializable]
42         public sealed class XName : IEquatable<XName>, ISerializable
43         {
44                 string local;
45                 XNamespace ns;
46
47                 XName (SerializationInfo info, StreamingContext context)
48                 {
49                         string expandedName = info.GetString ("name");
50                         string local, ns;
51                         ExpandName (expandedName, out local, out ns);
52                         this.local = local;
53                         this.ns = XNamespace.Get (ns);
54                 }
55
56                 internal XName (string local, XNamespace ns)
57                 {
58                         this.local = XmlConvert.VerifyNCName (local);
59                         this.ns = ns;
60                 }
61
62                 static Exception ErrorInvalidExpandedName ()
63                 {
64                         return new ArgumentException ("Invalid expanded name.");
65                 }
66
67                 public string LocalName {
68                         get { return local; }
69                 }
70
71                 public XNamespace Namespace {
72                         get { return ns; }
73                 }
74
75                 public string NamespaceName {
76                         get { return ns.NamespaceName; }
77                 }
78
79                 public override bool Equals (object obj)
80                 {
81                         XName n = obj as XName;
82                         return n != null && this == n;
83                 }
84
85                 bool IEquatable<XName>.Equals (XName other)
86                 {
87                         return this == other;
88                 }
89
90                 public static XName Get (string expandedName)
91                 {
92                         string local, ns;
93                         ExpandName (expandedName, out local, out ns);
94                         return Get (local, ns);
95                 }
96
97                 static void ExpandName (string expandedName, out string local, out string ns)
98                 {
99                         if (expandedName == null)
100                                 throw new ArgumentNullException ("expandedName");
101                         ns = null;
102                         local = null;
103                         if (expandedName.Length == 0)
104                                 throw ErrorInvalidExpandedName ();
105                         //this.expanded = expandedName;
106                         if (expandedName [0] == '{') {
107                                 for (int i = 1; i < expandedName.Length; i++) {
108                                         if (expandedName [i] == '}')
109                                                 ns = expandedName.Substring (1, i - 1);
110                                 }
111                                 if (String.IsNullOrEmpty (ns)) // {}foo is invalid
112                                         throw ErrorInvalidExpandedName ();
113                                 if (expandedName.Length == ns.Length + 2) // {foo} is invalid
114                                         throw ErrorInvalidExpandedName ();
115                                 local = expandedName.Substring (ns.Length + 2);
116                         }
117                         else {
118                                 local = expandedName;
119                                 ns = String.Empty;
120                         }
121                 }
122
123                 public static XName Get (string localName, string namespaceName)
124                 {
125                         return XNamespace.Get (namespaceName).GetName (localName);
126                 }
127
128                 public override int GetHashCode ()
129                 {
130                         return local.GetHashCode () ^ ns.GetHashCode ();
131                 }
132
133                 public static bool operator == (XName left, XName right)
134                 {
135                         if ((object) left == null)
136                                 return (object) right == null;
137                         else if ((object) right == null)
138                                 return false;
139                         return object.ReferenceEquals (left, right) ||
140                                 left.local == right.local && left.ns == right.ns;
141                 }
142
143                 [CLSCompliant (false)]
144                 public static implicit operator XName (string expandedName)
145                 {
146                         return expandedName == null ? null : Get (expandedName);
147                 }
148
149                 public static bool operator != (XName left, XName right)
150                 {
151                         return ! (left == right);
152                 }
153
154                 public override string ToString ()
155                 {
156                         if (ns == XNamespace.None)
157                                 return local;
158                         return String.Concat ("{", ns.NamespaceName, "}", local);
159                 }
160
161                 // in .NET it is serialized as "NameSerializer". dunno how to create it.
162                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
163                 {
164                         if (info == null)
165                                 throw new ArgumentNullException ("info");
166
167                         info.AddValue ("name", ToString ());
168                 }
169         }
170 }
171
172 #endif