2 // XmlNamespaceManager.cs
5 // Jason Diamond (jason@injektilo.org)
7 // (C) 2001 Jason Diamond http://injektilo.org/
10 using System.Collections;
14 public class XmlNamespaceManager : IEnumerable
18 private XmlNameTable nameTable;
19 private NamespaceScope currentScope;
25 public XmlNamespaceManager (XmlNameTable nameTable)
27 this.nameTable = nameTable;
29 nameTable.Add ("xmlns");
30 nameTable.Add ("xml");
31 nameTable.Add (String.Empty);
32 nameTable.Add ("http://www.w3.org/2000/xmlns/");
33 nameTable.Add ("http://www.w3.org/XML/1998/namespace");
42 public virtual string DefaultNamespace {
43 get { return LookupNamespace (String.Empty); }
46 public XmlNameTable NameTable {
47 get { return nameTable; }
54 public virtual void AddNamespace (string prefix, string uri)
57 throw new ArgumentNullException ("prefix", "Value cannot be null.");
60 throw new ArgumentNullException ("uri", "Value cannot be null.");
62 if (prefix.Length > 2 && prefix.Substring (0, 3).ToLower () == "xml")
63 throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML.", "prefix");
65 if (currentScope.Namespaces == null)
66 currentScope.Namespaces = new Hashtable ();
68 if (prefix == String.Empty)
69 currentScope.Namespaces [prefix] = nameTable.Add (uri);
71 currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
75 public virtual IEnumerator GetEnumerator ()
77 throw new NotImplementedException ();
80 public virtual bool HasNamespace (string prefix)
82 return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
85 public virtual string LookupNamespace (string prefix)
87 NamespaceScope scope = currentScope;
89 while (scope != null) {
90 if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
91 return scope.Namespaces[prefix] as string;
97 return nameTable.Get ("http://www.w3.org/2000/xmlns/");
99 return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
101 return nameTable.Get (String.Empty);
107 public virtual string LookupPrefix (string uri)
112 NamespaceScope scope = currentScope;
114 while (scope != null)
116 if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
117 foreach (DictionaryEntry entry in scope.Namespaces) {
118 if (entry.Value.ToString() == uri)
119 return nameTable.Get (entry.Key as string) as string;
129 public virtual bool PopScope ()
131 if (currentScope != null)
132 currentScope = currentScope.Next;
134 return currentScope != null;
137 public virtual void PushScope ()
139 NamespaceScope newScope = new NamespaceScope ();
140 newScope.Next = currentScope;
141 currentScope = newScope;
145 public virtual void RemoveNamespace (string prefix, string uri)
147 throw new NotImplementedException ();
153 internal class NamespaceScope
155 internal NamespaceScope Next;
156 internal Hashtable Namespaces;