5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Generic;
33 using System.Runtime.Serialization;
37 using XPI = System.Xml.Linq.XProcessingInstruction;
39 namespace System.Xml.Linq
42 public sealed class XName : IEquatable<XName>, ISerializable
47 XName (SerializationInfo info, StreamingContext context)
49 string expandedName = info.GetString ("name");
51 ExpandName (expandedName, out local, out ns);
53 this.ns = XNamespace.Get (ns);
56 internal XName (string local, XNamespace ns)
58 this.local = XmlConvert.VerifyNCName (local);
62 static Exception ErrorInvalidExpandedName ()
64 return new ArgumentException ("Invalid expanded name.");
67 public string LocalName {
71 public XNamespace Namespace {
75 public string NamespaceName {
76 get { return ns.NamespaceName; }
79 public override bool Equals (object obj)
81 XName n = obj as XName;
82 return n != null && this == n;
85 bool IEquatable<XName>.Equals (XName other)
90 public static XName Get (string expandedName)
93 ExpandName (expandedName, out local, out ns);
94 return Get (local, ns);
97 static void ExpandName (string expandedName, out string local, out string ns)
99 if (expandedName == null)
100 throw new ArgumentNullException ("expandedName");
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);
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);
118 local = expandedName;
123 public static XName Get (string localName, string namespaceName)
125 return XNamespace.Get (namespaceName).GetName (localName);
128 public override int GetHashCode ()
130 return local.GetHashCode () ^ ns.GetHashCode ();
133 public static bool operator == (XName left, XName right)
135 if ((object) left == null)
136 return (object) right == null;
137 else if ((object) right == null)
139 return object.ReferenceEquals (left, right) ||
140 left.local == right.local && left.ns == right.ns;
143 [CLSCompliant (false)]
144 public static implicit operator XName (string expandedName)
146 return expandedName == null ? null : Get (expandedName);
149 public static bool operator != (XName left, XName right)
151 return ! (left == right);
154 public override string ToString ()
156 if (ns == XNamespace.None)
158 return String.Concat ("{", ns.NamespaceName, "}", local);
161 // in .NET it is serialized as "NameSerializer". dunno how to create it.
162 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
165 throw new ArgumentNullException ("info");
167 info.AddValue ("name", ToString ());