BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XNamespace.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 using System;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Text;
32 using System.Xml;
33
34 namespace System.Xml.Linq
35 {
36         public sealed class XNamespace
37         {
38                 static readonly XNamespace blank, xml, xmlns;
39                 static Dictionary<string, XNamespace> nstable;
40
41                 static XNamespace ()
42                 {
43                         nstable = new Dictionary<string, XNamespace> ();
44                         blank = Get (String.Empty);
45                         xml = Get ("http://www.w3.org/XML/1998/namespace");
46                         xmlns = Get ("http://www.w3.org/2000/xmlns/");
47                 }
48
49                 public static XNamespace None { 
50                         get { return blank; }
51                 }
52
53                 public static XNamespace Xml {
54                         get { return xml; }
55                 }
56
57                 public static XNamespace Xmlns {
58                         get { return xmlns; }
59                 }
60
61                 public static XNamespace Get (string uri)
62                 {
63                         lock (nstable) {
64                                 XNamespace ret;
65                                 if (!nstable.TryGetValue (uri, out ret)) {
66                                         ret = new XNamespace (uri);
67                                         nstable [uri] = ret;
68                                 }
69                                 return ret;
70                         }
71                 }
72
73                 public XName GetName (string localName)
74                 {
75                         if (table == null)
76                                 table = new Dictionary<string, XName> ();
77                         lock (table) {
78                                 XName ret;
79                                 if (!table.TryGetValue (localName, out ret)) {
80                                         ret = new XName (localName, this);
81                                         table [localName] = ret;
82                                 }
83                                 return ret;
84                         }
85                 }
86
87                 string uri;
88                 Dictionary<string, XName> table;
89
90                 XNamespace (string namespaceName)
91                 {
92                         if (namespaceName == null)
93                                 throw new ArgumentNullException ("namespaceName");
94                         uri = namespaceName;
95                 }
96
97                 public string NamespaceName {
98                         get { return uri; }
99                 }
100
101                 public override bool Equals (object other)
102                 {
103                         if (Object.ReferenceEquals (this, other))
104                                 return true;
105                         XNamespace ns = other as XNamespace;
106                         return ns != null && uri == ns.uri;
107                 }
108
109                 public static bool operator == (XNamespace o1, XNamespace o2)
110                 {
111                         return (object) o1 != null ? o1.Equals (o2) : (object) o2 == null;
112                 }
113
114                 public static bool operator != (XNamespace o1, XNamespace o2)
115                 {
116                         return ! (o1 == o2);
117                 }
118                 
119                 public static XName operator + (XNamespace ns, string localName)
120                 {
121                         return new XName (localName, ns);
122                 }
123
124                 [CLSCompliant (false)]
125                 public static implicit operator XNamespace (string s)
126                 {
127                         return s != null ? XNamespace.Get (s) : null;
128                 }
129
130                 public override int GetHashCode ()
131                 {
132                         return uri.GetHashCode ();
133                 }
134
135                 public override string ToString ()
136                 {
137                         return uri;
138                 }
139         }
140 }