2 // XmlNamespaceManager.cs
5 // Jason Diamond (jason@injektilo.org)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
7 // Atsushi Enomoto (atsushi@ximian.com)
9 // (C) 2001 Jason Diamond http://injektilo.org/
10 // (C) 2003 Ben Maurer
11 // (C) 2004 Novell Inc.
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
37 using System.Collections.Generic;
39 using System.Collections.Specialized;
43 public class XmlNamespaceManager : IXmlNamespaceResolver, IEnumerable
47 public string Prefix, Uri;
52 public string DefaultNamespace;
61 string defaultNamespace;
66 decls = new NsDecl [10];
67 scopes = new NsScope [40];
70 // precondition declPos == nsDecl.Length
73 NsDecl [] old = decls;
74 decls = new NsDecl [declPos * 2 + 1];
76 Array.Copy (old, 0, decls, 0, declPos);
79 // precondition scopePos == scopes.Length
82 NsScope [] old = scopes;
83 scopes = new NsScope [scopePos * 2 + 1];
85 Array.Copy (old, 0, scopes, 0, scopePos);
92 private XmlNameTable nameTable;
93 internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
94 internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
95 internal const string PrefixXml = "xml";
96 internal const string PrefixXmlns = "xmlns";
102 public XmlNamespaceManager (XmlNameTable nameTable)
104 this.nameTable = nameTable;
106 nameTable.Add (PrefixXmlns);
107 nameTable.Add (PrefixXml);
108 nameTable.Add (String.Empty);
109 nameTable.Add (XmlnsXmlns);
110 nameTable.Add (XmlnsXml);
119 public virtual string DefaultNamespace {
120 get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
124 public virtual XmlNameTable NameTable {
126 public XmlNameTable NameTable {
128 get { return nameTable; }
135 public virtual void AddNamespace (string prefix, string uri)
137 AddNamespace (prefix, uri, false);
140 internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
143 throw new ArgumentNullException ("prefix", "Value cannot be null.");
146 throw new ArgumentNullException ("uri", "Value cannot be null.");
147 if (!atomizedNames) {
148 prefix = nameTable.Add (prefix);
149 uri = nameTable.Add (uri);
152 IsValidDeclaration (prefix, uri, true);
154 if (prefix.Length == 0)
155 defaultNamespace = uri;
157 for (int i = declPos; i > declPos - count; i--) {
158 if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
167 if (declPos == decls.Length)
169 decls [declPos].Prefix = prefix;
170 decls [declPos].Uri = uri;
173 internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
175 string message = null;
176 if (prefix == PrefixXml && uri != XmlnsXml)
177 message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
178 else if (message == null && prefix == "xmlns")
179 message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
180 else if (message == null && uri == XmlnsXmlns)
181 message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
182 if (message != null && throwException)
183 throw new ArgumentException (message);
188 public virtual IEnumerator GetEnumerator ()
190 // In fact it returns such table's enumerator that contains all the namespaces.
191 // while HasNamespace() ignores pushed namespaces.
193 Hashtable ht = new Hashtable ();
194 for (int i = 0; i <= declPos; i++) {
195 if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
196 ht [decls [i].Prefix] = decls [i].Uri;
200 ht [string.Empty] = DefaultNamespace;
201 ht [PrefixXml] = XmlnsXml;
202 ht [PrefixXmlns] = XmlnsXmlns;
204 return ht.Keys.GetEnumerator ();
208 public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
210 IDictionary namespaceTable = GetNamespacesInScopeImpl (scope);
211 IDictionary<string, string> namespaces = new Dictionary<string, string>(namespaceTable.Count);
213 foreach (DictionaryEntry entry in namespaceTable) {
214 namespaces[(string) entry.Key] = (string) entry.Value;
219 IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
221 return GetNamespacesInScopeImpl (scope);
225 internal virtual IDictionary GetNamespacesInScopeImpl (XmlNamespaceScope scope)
227 Hashtable table = new Hashtable ();
229 if (scope == XmlNamespaceScope.Local) {
230 for (int i = 0; i < count; i++)
231 if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
232 if (table.Contains (String.Empty))
233 table.Remove (String.Empty);
235 else if (decls [declPos - i].Uri != null)
236 table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
239 for (int i = 0; i <= declPos; i++) {
240 if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
241 // removal of default namespace
242 if (table.Contains (String.Empty))
243 table.Remove (String.Empty);
245 else if (decls [i].Uri != null)
246 table [decls [i].Prefix] = decls [i].Uri;
249 if (scope == XmlNamespaceScope.All)
250 table.Add ("xml", XmlNamespaceManager.XmlnsXml);
255 public virtual bool HasNamespace (string prefix)
257 return HasNamespace (prefix, false);
260 internal virtual bool HasNamespace (string prefix, bool atomizedNames)
262 if (prefix == null || count == 0)
265 for (int i = declPos; i > declPos - count; i--) {
266 if (decls [i].Prefix == prefix)
273 public virtual string LookupNamespace (string prefix)
276 return LookupNamespace (prefix, false);
278 return LookupNamespace (prefix, true);
282 internal virtual string LookupNamespace (string prefix, bool atomizedNames)
286 return nameTable.Get (XmlnsXmlns);
288 return nameTable.Get (XmlnsXml);
290 return DefaultNamespace;
295 for (int i = declPos; i >= 0; i--) {
296 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && decls [i].Uri != null /* null == flag for removed */)
297 return decls [i].Uri;
303 public virtual string LookupPrefix (string uri)
306 return LookupPrefix (uri, false);
308 return LookupPrefix (uri, true);
312 private bool CompareString (string s1, string s2, bool atomizedNames)
315 return object.ReferenceEquals (s1, s2);
320 internal string LookupPrefix (string uri, bool atomizedName)
325 if (CompareString (uri, DefaultNamespace, atomizedName))
328 if (CompareString (uri, XmlnsXml, atomizedName))
331 if (CompareString (uri, XmlnsXmlns, atomizedName))
334 for (int i = declPos; i >= 0; i--) {
335 if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
336 return decls [i].Prefix;
339 // ECMA specifies that this method returns String.Empty
340 // in case of no match. But actually MS.NET returns null.
341 // For more information,see
342 // http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
343 //return String.Empty;
347 public virtual bool PopScope ()
353 defaultNamespace = scopes [scopePos].DefaultNamespace;
354 count = scopes [scopePos].DeclCount;
359 public virtual void PushScope ()
362 if (scopePos == scopes.Length)
365 scopes [scopePos].DefaultNamespace = defaultNamespace;
366 scopes [scopePos].DeclCount = count;
370 // It is rarely used, so we don't need NameTable optimization on it.
371 public virtual void RemoveNamespace (string prefix, string uri)
373 RemoveNamespace (prefix, uri, false);
376 internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
379 throw new ArgumentNullException ("prefix");
382 throw new ArgumentNullException ("uri");
387 for (int i = declPos; i > declPos - count; i--) {
388 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
389 decls [i].Uri = null;