2005-04-12 Dick Porter <dick@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 //   Atsushi Enomoto (atsushi@ximian.com)
8 //
9 // (C) 2001 Jason Diamond  http://injektilo.org/
10 // (C) 2003 Ben Maurer
11 // (C) 2004 Novell Inc.
12 //
13
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 using System.Collections;
36 using System.Collections.Specialized;
37
38 namespace System.Xml
39 {
40         public class XmlNamespaceManager : IXmlNamespaceResolver, IEnumerable
41         {
42                 #region Data
43                 struct NsDecl {
44                         public string Prefix, Uri;
45                 }
46                 
47                 struct NsScope {
48                         public int DeclCount;
49                         public string DefaultNamespace;
50                 }
51                 
52                 NsDecl [] decls;
53                 int declPos = -1;
54                 
55                 NsScope [] scopes;
56                 int scopePos = -1;
57                 
58                 string defaultNamespace;
59                 int count;
60                 
61                 void InitData ()
62                 {
63                         decls = new NsDecl [10];
64                         scopes = new NsScope [40];
65                 }
66                 
67                 // precondition declPos == nsDecl.Length
68                 void GrowDecls ()
69                 {
70                         NsDecl [] old = decls;
71                         decls = new NsDecl [declPos * 2 + 1];
72                         if (declPos > 0)
73                                 Array.Copy (old, 0, decls, 0, declPos);
74                 }
75                 
76                 // precondition scopePos == scopes.Length
77                 void GrowScopes ()
78                 {
79                         NsScope [] old = scopes;
80                         scopes = new NsScope [scopePos * 2 + 1];
81                         if (scopePos > 0)
82                                 Array.Copy (old, 0, scopes, 0, scopePos);
83                 }
84                 
85                 #endregion
86                 
87                 #region Fields
88
89                 private XmlNameTable nameTable;
90                 internal const string XmlnsXml = "http://www.w3.org/XML/1998/namespace";
91                 internal const string XmlnsXmlns = "http://www.w3.org/2000/xmlns/";
92                 internal const string PrefixXml = "xml";
93                 internal const string PrefixXmlns = "xmlns";
94
95                 #endregion
96
97                 #region Constructor
98
99                 public XmlNamespaceManager (XmlNameTable nameTable)
100                 {
101                         this.nameTable = nameTable;
102
103                         nameTable.Add (PrefixXmlns);
104                         nameTable.Add (PrefixXml);
105                         nameTable.Add (String.Empty);
106                         nameTable.Add (XmlnsXmlns);
107                         nameTable.Add (XmlnsXml);
108                         
109                         InitData ();
110                 }
111
112                 #endregion
113
114                 #region Properties
115
116                 public virtual string DefaultNamespace {
117                         get { return defaultNamespace == null ? string.Empty : defaultNamespace; }
118                 }
119
120                 public XmlNameTable NameTable {
121                         get { return nameTable; }
122                 }
123
124                 #endregion
125
126                 #region Methods
127
128                 public virtual void AddNamespace (string prefix, string uri)
129                 {
130                         AddNamespace (prefix, uri, false);
131                 }
132
133 #if NET_2_0
134                 [Obsolete]
135                 public virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
136 #else
137                 internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
138 #endif
139                 {
140                         if (prefix == null)
141                                 throw new ArgumentNullException ("prefix", "Value cannot be null.");
142
143                         if (uri == null)
144                                 throw new ArgumentNullException ("uri", "Value cannot be null.");
145                         if (!atomizedNames) {
146                                 prefix = nameTable.Add (prefix);
147                                 uri = nameTable.Add (uri);
148                         }
149
150                         IsValidDeclaration (prefix, uri, true);
151
152                         if (prefix.Length == 0)
153                                 defaultNamespace = uri;
154                         
155                         for (int i = declPos; i > declPos - count; i--) {
156                                 if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
157                                         decls [i].Uri = uri;
158                                         return;
159                                 }
160                         }
161                         
162                         declPos ++;
163                         count ++;
164                         
165                         if (declPos == decls.Length)
166                                 GrowDecls ();
167                         decls [declPos].Prefix = prefix;
168                         decls [declPos].Uri = uri;
169                 }
170
171                 internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
172                 {
173                         string message = null;
174                         if (prefix == PrefixXml && uri != XmlnsXml)
175                                 message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
176                         else if (message == null && prefix == "xmlns")
177                                 message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
178                         else if (message == null && uri == XmlnsXmlns)
179                                 message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
180                         if (message != null && throwException)
181                                 throw new ArgumentException (message);
182                         else
183                                 return message;
184                 }
185
186                 public virtual IEnumerator GetEnumerator ()
187                 {
188                         // In fact it returns such table's enumerator that contains all the namespaces.
189                         // while HasNamespace() ignores pushed namespaces.
190                         
191                         Hashtable ht = new Hashtable ();
192                         for (int i = 0; i <= declPos; i++) {
193                                 if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
194                                         ht [decls [i].Prefix] = decls [i].Uri;
195                                 }
196                         }
197                         
198                         ht [string.Empty] = DefaultNamespace;
199                         ht [PrefixXml] = XmlnsXml;
200                         ht [PrefixXmlns] = XmlnsXmlns;
201                         
202                         return ht.Keys.GetEnumerator ();
203                 }
204
205 #if NET_2_0
206                 public virtual IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
207 #else
208                 IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
209                 {
210                         return GetNamespacesInScope (scope);
211                 }
212
213                 internal virtual IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
214 #endif
215                 {
216                         Hashtable table = new Hashtable ();
217
218                         if (scope == XmlNamespaceScope.Local) {
219                                 for (int i = 0; i < count; i++)
220                                         if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
221                                                 if (table.Contains (String.Empty))
222                                                         table.Remove (String.Empty);
223                                         }
224                                         else if (decls [declPos - i].Uri != null)
225                                                 table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
226                                 return table;
227                         } else {
228                                 for (int i = 0; i <= declPos; i++) {
229                                         if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
230                                                 // removal of default namespace
231                                                 if (table.Contains (String.Empty))
232                                                         table.Remove (String.Empty);
233                                         }
234                                         else if (decls [i].Uri != null)
235                                                 table [decls [i].Prefix] = decls [i].Uri;
236                                 }
237
238                                 if (scope == XmlNamespaceScope.All)
239                                         table.Add ("xml", XmlNamespaceManager.XmlnsXml);
240                                 return table;
241                         }
242                 }
243
244                 public virtual bool HasNamespace (string prefix)
245                 {
246                         return HasNamespace (prefix, false);
247                 }
248                 
249 #if NET_2_0
250                 [Obsolete]
251                 public virtual bool HasNamespace (string prefix, bool atomizedNames)
252 #else
253                 internal virtual bool HasNamespace (string prefix, bool atomizedNames)
254 #endif
255                 {
256                         if (prefix == null || count == 0)
257                                 return false;
258
259                         for (int i = declPos; i > declPos - count; i--) {
260                                 if (decls [i].Prefix == prefix)
261                                         return true;
262                         }
263                         
264                         return false;
265                 }
266
267                 public virtual string LookupNamespace (string prefix)
268                 {
269 #if NET_2_0
270                         return LookupNamespace (prefix, false);
271 #else
272                         return LookupNamespace (prefix, true);
273 #endif
274                 }
275
276 #if NET_2_0
277                 [Obsolete]
278                 public virtual string LookupNamespace (string prefix, bool atomizedNames)
279 #else
280                 string IXmlNamespaceResolver.LookupNamespace (string prefix, bool atomizedNames)
281                 {
282                         return LookupNamespace (prefix, atomizedNames);
283                 }
284
285                 internal virtual string LookupNamespace (string prefix, bool atomizedNames)
286 #endif
287                 {
288                         switch (prefix) {
289                         case PrefixXmlns:
290                                 return nameTable.Get (XmlnsXmlns);
291                         case PrefixXml:
292                                 return nameTable.Get (XmlnsXml);
293                         case "":
294                                 return DefaultNamespace;
295                         case null:
296                                 return null;
297                         }
298
299                         for (int i = declPos; i >= 0; i--) {
300                                 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && decls [i].Uri != null /* null == flag for removed */)
301                                         return decls [i].Uri;
302                         }
303                         
304                         return null;
305                 }
306
307                 public virtual string LookupPrefix (string uri)
308                 {
309 #if NET_2_0
310                         return LookupPrefix (uri, false);
311 #else
312                         return LookupPrefix (uri, true);
313 #endif
314                 }
315
316                 private bool CompareString (string s1, string s2, bool atomizedNames)
317                 {
318                         if (atomizedNames)
319                                 return object.ReferenceEquals (s1, s2);
320                         else
321                                 return s1 == s2;
322                 }
323
324 #if NET_2_0
325                 [Obsolete]
326                 public string LookupPrefix (string uri, bool atomizedName)
327 #else
328                 string IXmlNamespaceResolver.LookupPrefix (string uri, bool atomizedName)
329                 {
330                         return LookupPrefix (uri, atomizedName);
331                 }
332
333                 internal string LookupPrefix (string uri, bool atomizedName)
334 #endif
335                 {
336                         if (uri == null)
337                                 return null;
338
339                         if (CompareString (uri, DefaultNamespace, atomizedName))
340                                 return string.Empty;
341
342                         if (CompareString (uri, XmlnsXml, atomizedName))
343                                 return PrefixXml;
344                         
345                         if (CompareString (uri, XmlnsXmlns, atomizedName))
346                                 return PrefixXmlns;
347
348                         for (int i = declPos; i >= 0; i--) {
349                                 if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
350                                         return decls [i].Prefix;
351                         }
352
353                         // ECMA specifies that this method returns String.Empty
354                         // in case of no match. But actually MS.NET returns null.
355                         // For more information,see
356                         //  http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
357                         //return String.Empty;
358                         return null;
359                 }
360
361                 public virtual bool PopScope ()
362                 {
363                         if (scopePos == -1)
364                                 return false;
365
366                         declPos -= count;
367                         defaultNamespace = scopes [scopePos].DefaultNamespace;
368                         count = scopes [scopePos].DeclCount;
369                         scopePos --;
370                         return true;
371                 }
372
373                 public virtual void PushScope ()
374                 {
375                         scopePos ++;
376                         if (scopePos == scopes.Length)
377                                 GrowScopes ();
378                         
379                         scopes [scopePos].DefaultNamespace = defaultNamespace;
380                         scopes [scopePos].DeclCount = count;
381                         count = 0;
382                 }
383
384                 // It is rarely used, so we don't need NameTable optimization on it.
385                 public virtual void RemoveNamespace (string prefix, string uri)
386                 {
387                         RemoveNamespace (prefix, uri, false);
388                 }
389
390 #if NET_2_0
391                 [Obsolete]
392                 public virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
393 #else
394                 internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
395 #endif
396                 {
397                         if (prefix == null)
398                                 throw new ArgumentNullException ("prefix");
399
400                         if (uri == null)
401                                 throw new ArgumentNullException ("uri");
402                         
403                         if (count == 0)
404                                 return;
405
406                         for (int i = declPos; i > declPos - count; i--) {
407                                 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
408                                         decls [i].Uri = null;
409                         }
410                 }
411
412                 #endregion
413         }
414 }