2004-07-28 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         public class XmlQualifiedName
37         {
38                 // Constructors         
39                 public XmlQualifiedName ()
40                         : this (string.Empty, string.Empty)
41                 {
42                 }
43
44                 public XmlQualifiedName (string name)
45                         : this (name, string.Empty)
46                 {
47                 }
48
49                 public XmlQualifiedName (string name, string ns)
50                         : base ()
51                 {
52                         this.name = (name == null) ? "" : name;
53                         this.ns = (ns == null) ? "" : ns;
54                         this.hash = this.name.GetHashCode () ^ this.ns.GetHashCode ();
55                 }
56
57                 // Fields
58                 public static readonly XmlQualifiedName Empty = new XmlQualifiedName ();                
59                 private readonly string name;
60                 private readonly string ns;
61                 private readonly int hash;
62                 
63                 // Properties
64                 public bool IsEmpty
65                 {
66                         get {
67                                 return name.Length == 0 && ns.Length == 0;
68                         }
69                 }
70
71                 public string Name
72                 {
73                         get { return name; }
74                 }
75
76                 public string Namespace
77                 {
78                         get { return ns; }
79                 }
80
81                 // Methods
82                 public override bool Equals (object other)
83                 {
84                         return this == (other as XmlQualifiedName);
85                 }
86
87                 public override int GetHashCode () 
88                 { 
89                         return hash;
90                 }
91
92                 public override string ToString ()
93                 {
94                         if (ns == string.Empty)
95                                 return name;
96                         else
97                                 return ns + ":" + name;
98                 }
99
100                 public static string ToString (string name, string ns)
101                 {
102                         if (ns == string.Empty)
103                                 return name;
104                         else                            
105                                 return ns + ":" + name;
106                 }
107
108                 internal static XmlQualifiedName Parse (string name, IXmlNamespaceResolver resolver)
109                 {
110                         int index = name.IndexOf (':');
111                         if (index < 0)
112                                 return new XmlQualifiedName (name);
113                         string ns = resolver.LookupNamespace (name.Substring (0, index));
114                         if (ns == null)
115                                 throw new ArgumentException ("Invalid qualified name.");
116                         return new XmlQualifiedName (name.Substring (index + 1), ns);
117                 }
118
119                 // Operators
120                 public static bool operator == (XmlQualifiedName a, XmlQualifiedName b)
121                 {
122                         if((Object)a == (Object)b)
123                                 return true;
124
125                         if((Object)a == null || (Object)b == null)
126                                 return false;
127
128                         return (a.hash == b.hash) && (a.name == b.name) && (a.ns == b.ns);
129                 }
130
131                 public static bool operator != (XmlQualifiedName a, XmlQualifiedName b)
132                 {
133                         return !(a == b);
134                 }
135         }
136 }