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