2004-11-24 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlQualifiedName.cs
1 //
2 // System.Xml.XmlQualifiedName.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //               
6 // (C) Ximian, Inc.
7 // 
8 // Modified: 
9 //              21st June 2002 : Ajay kumar Dwivedi (adwiv@yahoo.com)
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33
34 namespace System.Xml
35 {
36 #if NET_2_0
37         [Serializable]
38 #endif
39         public class XmlQualifiedName
40         {
41                 // Constructors         
42                 public XmlQualifiedName ()
43                         : this (string.Empty, string.Empty)
44                 {
45                 }
46
47                 public XmlQualifiedName (string name)
48                         : this (name, string.Empty)
49                 {
50                 }
51
52                 public XmlQualifiedName (string name, string ns)
53                         : base ()
54                 {
55                         this.name = (name == null) ? "" : name;
56                         this.ns = (ns == null) ? "" : ns;
57                         this.hash = this.name.GetHashCode () ^ this.ns.GetHashCode ();
58                 }
59
60                 // Fields
61                 public static readonly XmlQualifiedName Empty = new XmlQualifiedName ();                
62                 private readonly string name;
63                 private readonly string ns;
64                 private readonly int hash;
65                 
66                 // Properties
67                 public bool IsEmpty
68                 {
69                         get {
70                                 return name.Length == 0 && ns.Length == 0;
71                         }
72                 }
73
74                 public string Name
75                 {
76                         get { return name; }
77                 }
78
79                 public string Namespace
80                 {
81                         get { return ns; }
82                 }
83
84                 // Methods
85                 public override bool Equals (object other)
86                 {
87                         return this == (other as XmlQualifiedName);
88                 }
89
90                 public override int GetHashCode () 
91                 { 
92                         return hash;
93                 }
94
95                 public override string ToString ()
96                 {
97                         if (ns == string.Empty)
98                                 return name;
99                         else
100                                 return ns + ":" + name;
101                 }
102
103                 public static string ToString (string name, string ns)
104                 {
105                         if (ns == string.Empty)
106                                 return name;
107                         else                            
108                                 return ns + ":" + name;
109                 }
110
111                 internal static XmlQualifiedName Parse (string name, IXmlNamespaceResolver resolver)
112                 {
113                         int index = name.IndexOf (':');
114                         if (index < 0)
115                                 return new XmlQualifiedName (name);
116                         string ns = resolver.LookupNamespace (name.Substring (0, index));
117                         if (ns == null)
118                                 throw new ArgumentException ("Invalid qualified name.");
119                         return new XmlQualifiedName (name.Substring (index + 1), ns);
120                 }
121
122                 // Operators
123                 public static bool operator == (XmlQualifiedName a, XmlQualifiedName b)
124                 {
125                         if((Object)a == (Object)b)
126                                 return true;
127
128                         if((Object)a == null || (Object)b == null)
129                                 return false;
130
131                         return (a.hash == b.hash) && (a.name == b.name) && (a.ns == b.ns);
132                 }
133
134                 public static bool operator != (XmlQualifiedName a, XmlQualifiedName b)
135                 {
136                         return !(a == b);
137                 }
138         }
139 }