2004-02-10 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlNamespaceManager.cs
1 //
2 // XmlNamespaceManager.cs
3 //
4 // Authors:
5 //   Jason Diamond (jason@injektilo.org)
6 //   Ben Maurer (bmaurer@users.sourceforge.net)
7 //
8 // (C) 2001 Jason Diamond  http://injektilo.org/
9 // (C) 2003 Ben Maurer
10 //
11
12 using System.Collections;
13 using System.Collections.Specialized;
14
15 namespace System.Xml
16 {
17         public class XmlNamespaceManager : IEnumerable
18         {
19                 #region Data
20                 struct NsDecl {
21                         public string Prefix, Uri;
22                 }
23                 
24                 struct NsScope {
25                         public int DeclCount;
26                         public string DefaultNamespace;
27                 }
28                 
29                 NsDecl [] decls;
30                 int declPos = -1;
31                 
32                 NsScope [] scopes;
33                 int scopePos = -1;
34                 
35                 string defaultNamespace;
36                 int count;
37                 
38                 void InitData ()
39                 {
40                         decls = new NsDecl [10];
41                         scopes = new NsScope [40];
42                 }
43                 
44                 // precondition declPos == nsDecl.Length
45                 void GrowDecls ()
46                 {
47                         NsDecl [] old = decls;
48                         decls = new NsDecl [declPos * 2 + 1];
49                         if (declPos > 0)
50                                 Array.Copy (old, 0, decls, 0, declPos);
51                 }
52                 
53                 // precondition scopePos == scopes.Length
54                 void GrowScopes ()
55                 {
56                         NsScope [] old = scopes;
57                         scopes = new NsScope [scopePos * 2 + 1];
58                         if (scopePos > 0)
59                                 Array.Copy (old, 0, scopes, 0, scopePos);
60                 }
61                 
62                 #endregion
63                 
64                 #region Fields
65
66                 private XmlNameTable nameTable;
67                 internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
68                 internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
69                 internal const string PrefixXml = "xml";
70                 internal const string PrefixXmlns = "xmlns";
71
72                 #endregion
73
74                 #region Constructor
75
76                 internal XmlNamespaceManager () {}
77                 public XmlNamespaceManager (XmlNameTable nameTable)
78                 {
79                         this.nameTable = nameTable;
80
81                         nameTable.Add (PrefixXmlns);
82                         nameTable.Add (PrefixXml);
83                         nameTable.Add (String.Empty);
84                         nameTable.Add (XmlnsXmlns);
85                         nameTable.Add (XmlnsXml);
86                         
87                         InitData ();
88                 }
89
90                 #endregion
91
92                 #region Properties
93
94                 public virtual string DefaultNamespace {
95                         get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
96                 }
97
98                 public XmlNameTable NameTable {
99                         get { return nameTable; }
100                 }
101
102                 #endregion
103
104                 #region Methods
105
106                 public virtual void AddNamespace (string prefix, string uri)
107                 {
108                         AddNamespace (prefix, uri, false);
109                 }
110
111 #if NET_1_2
112                 public virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
113 #else
114                 internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
115 #endif
116                 {
117                         if (prefix == null)
118                                 throw new ArgumentNullException ("prefix", "Value cannot be null.");
119
120                         if (uri == null)
121                                 throw new ArgumentNullException ("uri", "Value cannot be null.");
122                         if (!atomizedNames) {
123                                 prefix = nameTable.Add (prefix);
124                                 uri = nameTable.Add (uri);
125                         }
126
127                         IsValidDeclaration (prefix, uri, true);
128
129                         if (prefix.Length == 0)
130                                 defaultNamespace = uri;
131                         
132                         for (int i = declPos; i > declPos - count; i--) {
133                                 if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
134                                         decls [i].Uri = uri;
135                                         return;
136                                 }
137                         }
138                         
139                         declPos ++;
140                         count ++;
141                         
142                         if (declPos == decls.Length)
143                                 GrowDecls ();
144                         decls [declPos].Prefix = prefix;
145                         decls [declPos].Uri = uri;
146                 }
147
148                 internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
149                 {
150                         string message = null;
151                         if (prefix == PrefixXml && uri != XmlnsXml)
152                                 message = String.Format ("Prefix \"xml\" is only allowed to the fixed uri \"{0}\"", XmlnsXml);
153                         else if (uri == XmlnsXml)
154                                 message = String.Format ("Namespace URI \"{0}\" can only be declared with the fixed prefix \"xml\"", XmlnsXml);
155                         if (message == null && prefix == "xmlns")
156                                 message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
157                         if (message == null && uri == XmlnsXmlns)
158                                 message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
159                         if (message != null && throwException)
160                                 throw new ArgumentException (message);
161                         else
162                                 return message;
163                 }
164
165                 public virtual IEnumerator GetEnumerator ()
166                 {
167                         // In fact it returns such table's enumerator that contains all the namespaces.
168                         // while HasNamespace() ignores pushed namespaces.
169                         
170                         Hashtable ht = new Hashtable ();
171                         for (int i = 0; i <= declPos; i++) {
172                                 if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
173                                         ht [decls [i].Prefix] = decls [i].Uri;
174                                 }
175                         }
176                         
177                         ht [string.Empty] = DefaultNamespace;
178                         ht [PrefixXml] = XmlnsXml;
179                         ht [PrefixXmlns] = XmlnsXmlns;
180                         
181                         return ht.Keys.GetEnumerator ();
182                 }
183 #if NET_1_2
184                 [MonoTODO]
185                 public virtual StringDictionary GetNamespacesInScope (XmlNamespaceScope scope)
186                 {
187                         throw new NotImplementedException ();
188                 }
189 #endif
190
191                 public virtual bool HasNamespace (string prefix)
192                 {
193                         if (prefix == null || count == 0)
194                                 return false;
195
196                         for (int i = declPos; i > declPos - count; i--) {
197                                 if (decls [i].Prefix == prefix)
198                                         return true;
199                         }
200                         
201                         return false;
202                 }
203
204                 public virtual string LookupNamespace (string prefix)
205                 {
206                         return LookupNamespace (prefix, false);
207                 }
208
209 #if NET_1_2
210                 public string LookupNamespace (string prefix, bool atomizedName)
211 #else
212                 internal string LookupNamespace (string prefix, bool atomizedName)
213 #endif
214                 {
215                         switch (prefix) {
216                         case PrefixXmlns:
217                                 return nameTable.Get (XmlnsXmlns);
218                         case PrefixXml:
219                                 return nameTable.Get (XmlnsXml);
220                         case "":
221                                 return DefaultNamespace;
222                         case null:
223                                 return null;
224                         }
225
226                         for (int i = declPos; i >= 0; i--) {
227                                 if (CompareString (decls [i].Prefix, prefix, atomizedName) && decls [i].Uri != null /* null == flag for removed */)
228                                         return decls [i].Uri;
229                         }
230                         
231                         return null;
232                 }
233
234                 public virtual string LookupPrefix (string uri)
235                 {
236                         return LookupPrefix (uri, false);
237                 }
238
239                 private bool CompareString (string s1, string s2, bool atomizedNames)
240                 {
241                         if (atomizedNames)
242                                 return object.ReferenceEquals (s1, s2);
243                         else
244                                 return s1 == s2;
245                 }
246
247 #if NET_1_2
248                 public string LookupPrefix (string uri, bool atomizedName)
249 #else
250                 internal string LookupPrefix (string uri, bool atomizedName)
251 #endif
252                 {
253                         if (uri == null)
254                                 return null;
255
256                         if (CompareString (uri, DefaultNamespace, atomizedName))
257                                 return string.Empty;
258
259                         if (CompareString (uri, XmlnsXml, atomizedName))
260                                 return PrefixXml;
261                         
262                         if (CompareString (uri, XmlnsXmlns, atomizedName))
263                                 return PrefixXmlns;
264
265                         for (int i = declPos; i >= 0; i--) {
266                                 if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
267                                         return decls [i].Prefix;
268                         }
269
270                         // ECMA specifies that this method returns String.Empty
271                         // in case of no match. But actually MS.NET returns null.
272                         // For more information,see
273                         //  http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
274                         //return String.Empty;
275                         return null;
276                 }
277
278                 public virtual bool PopScope ()
279                 {
280                         if (scopePos == -1)
281                                 return false;
282
283                         declPos -= count;
284                         defaultNamespace = scopes [scopePos].DefaultNamespace;
285                         count = scopes [scopePos].DeclCount;
286                         scopePos --;
287                         return true;
288                 }
289
290                 public virtual void PushScope ()
291                 {
292                         scopePos ++;
293                         if (scopePos == scopes.Length)
294                                 GrowScopes ();
295                         
296                         scopes [scopePos].DefaultNamespace = defaultNamespace;
297                         scopes [scopePos].DeclCount = count;
298                         count = 0;
299                 }
300
301                 // It is rarely used, so we don't need NameTable optimization on it.
302                 public virtual void RemoveNamespace (string prefix, string uri)
303                 {
304                         RemoveNamespace (prefix, uri, false);
305                 }
306
307 #if NET_1_2
308                 public virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
309 #else
310                 internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
311 #endif
312                 {
313                         if (prefix == null)
314                                 throw new ArgumentNullException ("prefix");
315
316                         if (uri == null)
317                                 throw new ArgumentNullException ("uri");
318                         
319                         if (count == 0)
320                                 return;
321
322                         for (int i = declPos; i > declPos - count; i--) {
323                                 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
324                                         decls [i].Uri = null;
325                         }
326                 }
327
328                 #endregion
329         }
330 }