Implemented WriteCData and WriteComment.
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamespaceManager.cs
1 //
2 // XmlNamespaceManager.cs
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //
7 // (C) 2001 Jason Diamond  http://injektilo.org/
8 //
9
10 using System.Collections;
11
12 namespace System.Xml
13 {
14         public class XmlNamespaceManager : IEnumerable
15         {
16                 #region Fields
17
18                 private XmlNameTable nameTable;
19                 private NamespaceScope currentScope;
20
21                 #endregion
22
23                 #region Constructor
24
25                 public XmlNamespaceManager (XmlNameTable nameTable)
26                 {
27                         this.nameTable = nameTable;
28
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");
34
35                         PushScope ();
36                 }
37
38                 #endregion
39
40                 #region Properties
41
42                 public virtual string DefaultNamespace {
43                         get { return LookupNamespace (String.Empty); }
44                 }
45
46                 public XmlNameTable NameTable {
47                         get { return nameTable; }
48                 }
49
50                 #endregion
51
52                 #region Methods
53
54                 public virtual void AddNamespace (string prefix, string uri)
55                 {
56                         if (prefix == null)
57                                 throw new ArgumentNullException ("prefix", "Value cannot be null.");
58
59                         if (uri == null)
60                                 throw new ArgumentNullException ("uri", "Value cannot be null.");
61
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");
64
65                         if (currentScope.Namespaces == null)
66                                 currentScope.Namespaces = new Hashtable ();
67
68                         currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
69                 }
70
71                 [MonoTODO]
72                 public virtual IEnumerator GetEnumerator ()
73                 {
74                         throw new NotImplementedException ();
75                 }
76
77                 public virtual bool HasNamespace (string prefix)
78                 {
79                         return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
80                 }
81
82                 public virtual string LookupNamespace (string prefix)
83                 {
84                         NamespaceScope scope = currentScope;
85
86                         while (scope != null) {
87                                 if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
88                                         return scope.Namespaces[prefix] as string;
89                                 scope = scope.Next;
90                         }
91
92                         switch (prefix) {
93                         case "xmlns":
94                                 return nameTable.Get ("http://www.w3.org/2000/xmlns/");
95                         case "xml":
96                                 return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
97                         case "":
98                                 return nameTable.Get (String.Empty);
99                         }
100
101                         return null;
102                 }
103
104                 [MonoTODO]
105                 public virtual string LookupPrefix (string uri)
106                 {
107                         throw new NotImplementedException ();
108                 }
109
110                 public virtual bool PopScope ()
111                 {
112                         if (currentScope != null)
113                                 currentScope = currentScope.Next;
114
115                         return currentScope != null;
116                 }
117
118                 public virtual void PushScope ()
119                 {
120                         NamespaceScope newScope = new NamespaceScope ();
121                         newScope.Next = currentScope;
122                         currentScope = newScope;
123                 }
124
125                 [MonoTODO]
126                 public virtual void RemoveNamespace (string prefix, string uri)
127                 {
128                         throw new NotImplementedException ();
129                 }
130
131                 #endregion
132         }
133
134         internal class NamespaceScope
135         {
136                 internal NamespaceScope Next;
137                 internal Hashtable Namespaces;
138         }
139 }