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 currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
72 public virtual IEnumerator GetEnumerator ()
74 throw new NotImplementedException ();
77 public virtual bool HasNamespace (string prefix)
79 return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
82 public virtual string LookupNamespace (string prefix)
84 NamespaceScope scope = currentScope;
86 while (scope != null) {
87 if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
88 return scope.Namespaces[prefix] as string;
94 return nameTable.Get ("http://www.w3.org/2000/xmlns/");
96 return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
98 return nameTable.Get (String.Empty);
104 public virtual string LookupPrefix (string uri)
109 NamespaceScope scope = currentScope;
111 while (scope != null)
113 if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
114 foreach (DictionaryEntry entry in scope.Namespaces) {
115 if (entry.Value.ToString() == uri)
116 return nameTable.Get (entry.Key as string) as string;
126 public virtual bool PopScope ()
128 if (currentScope != null)
129 currentScope = currentScope.Next;
131 return currentScope != null;
134 public virtual void PushScope ()
136 NamespaceScope newScope = new NamespaceScope ();
137 newScope.Next = currentScope;
138 currentScope = newScope;
142 public virtual void RemoveNamespace (string prefix, string uri)
144 throw new NotImplementedException ();
150 internal class NamespaceScope
152 internal NamespaceScope Next;
153 internal Hashtable Namespaces;