merging the Mainsoft branch to the trunk
[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                 internal virtual void AddNamespace (string prefix, string uri, bool atomizedNames)
134                 {
135                         if (prefix == null)
136                                 throw new ArgumentNullException ("prefix", "Value cannot be null.");
137
138                         if (uri == null)
139                                 throw new ArgumentNullException ("uri", "Value cannot be null.");
140                         if (!atomizedNames) {
141                                 prefix = nameTable.Add (prefix);
142                                 uri = nameTable.Add (uri);
143                         }
144
145                         IsValidDeclaration (prefix, uri, true);
146
147                         if (prefix.Length == 0)
148                                 defaultNamespace = uri;
149                         
150                         for (int i = declPos; i > declPos - count; i--) {
151                                 if (object.ReferenceEquals (decls [i].Prefix, prefix)) {
152                                         decls [i].Uri = uri;
153                                         return;
154                                 }
155                         }
156                         
157                         declPos ++;
158                         count ++;
159                         
160                         if (declPos == decls.Length)
161                                 GrowDecls ();
162                         decls [declPos].Prefix = prefix;
163                         decls [declPos].Uri = uri;
164                 }
165
166                 internal static string IsValidDeclaration (string prefix, string uri, bool throwException)
167                 {
168                         string message = null;
169                         if (prefix == PrefixXml && uri != XmlnsXml)
170                                 message = String.Format ("Prefix \"xml\" can only be bound to the fixed namespace URI \"{0}\". \"{1}\" is invalid.", XmlnsXml, uri);
171                         else if (message == null && prefix == "xmlns")
172                                 message = "Declaring prefix named \"xmlns\" is not allowed to any namespace.";
173                         else if (message == null && uri == XmlnsXmlns)
174                                 message = String.Format ("Namespace URI \"{0}\" cannot be declared with any namespace.", XmlnsXmlns);
175                         if (message != null && throwException)
176                                 throw new ArgumentException (message);
177                         else
178                                 return message;
179                 }
180
181                 public virtual IEnumerator GetEnumerator ()
182                 {
183                         // In fact it returns such table's enumerator that contains all the namespaces.
184                         // while HasNamespace() ignores pushed namespaces.
185                         
186                         Hashtable ht = new Hashtable ();
187                         for (int i = 0; i <= declPos; i++) {
188                                 if (decls [i].Prefix != string.Empty && decls [i].Uri != null) {
189                                         ht [decls [i].Prefix] = decls [i].Uri;
190                                 }
191                         }
192                         
193                         ht [string.Empty] = DefaultNamespace;
194                         ht [PrefixXml] = XmlnsXml;
195                         ht [PrefixXmlns] = XmlnsXmlns;
196                         
197                         return ht.Keys.GetEnumerator ();
198                 }
199
200 #if NET_2_0
201                 public virtual IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
202 #else
203                 IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
204                 {
205                         return GetNamespacesInScope (scope);
206                 }
207
208                 internal virtual IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
209 #endif
210                 {
211                         Hashtable table = new Hashtable ();
212
213                         if (scope == XmlNamespaceScope.Local) {
214                                 for (int i = 0; i < count; i++)
215                                         if (decls [declPos - i].Prefix == String.Empty && decls [declPos - i].Uri == String.Empty) {
216                                                 if (table.Contains (String.Empty))
217                                                         table.Remove (String.Empty);
218                                         }
219                                         else if (decls [declPos - i].Uri != null)
220                                                 table.Add (decls [declPos - i].Prefix, decls [declPos - i].Uri);
221                                 return table;
222                         } else {
223                                 for (int i = 0; i <= declPos; i++) {
224                                         if (decls [i].Prefix == String.Empty && decls [i].Uri == String.Empty) {
225                                                 // removal of default namespace
226                                                 if (table.Contains (String.Empty))
227                                                         table.Remove (String.Empty);
228                                         }
229                                         else if (decls [i].Uri != null)
230                                                 table [decls [i].Prefix] = decls [i].Uri;
231                                 }
232
233                                 if (scope == XmlNamespaceScope.All)
234                                         table.Add ("xml", XmlNamespaceManager.XmlnsXml);
235                                 return table;
236                         }
237                 }
238
239                 public virtual bool HasNamespace (string prefix)
240                 {
241                         return HasNamespace (prefix, false);
242                 }
243
244                 internal virtual bool HasNamespace (string prefix, bool atomizedNames)
245                 {
246                         if (prefix == null || count == 0)
247                                 return false;
248
249                         for (int i = declPos; i > declPos - count; i--) {
250                                 if (decls [i].Prefix == prefix)
251                                         return true;
252                         }
253                         
254                         return false;
255                 }
256
257                 public virtual string LookupNamespace (string prefix)
258                 {
259 #if NET_2_0
260                         return LookupNamespace (prefix, false);
261 #else
262                         return LookupNamespace (prefix, true);
263 #endif
264                 }
265
266                 internal virtual string LookupNamespace (string prefix, bool atomizedNames)
267                 {
268                         switch (prefix) {
269                         case PrefixXmlns:
270                                 return nameTable.Get (XmlnsXmlns);
271                         case PrefixXml:
272                                 return nameTable.Get (XmlnsXml);
273                         case "":
274                                 return DefaultNamespace;
275                         case null:
276                                 return null;
277                         }
278
279                         for (int i = declPos; i >= 0; i--) {
280                                 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && decls [i].Uri != null /* null == flag for removed */)
281                                         return decls [i].Uri;
282                         }
283                         
284                         return null;
285                 }
286
287                 public virtual string LookupPrefix (string uri)
288                 {
289 #if NET_2_0
290                         return LookupPrefix (uri, false);
291 #else
292                         return LookupPrefix (uri, true);
293 #endif
294                 }
295
296                 private bool CompareString (string s1, string s2, bool atomizedNames)
297                 {
298                         if (atomizedNames)
299                                 return object.ReferenceEquals (s1, s2);
300                         else
301                                 return s1 == s2;
302                 }
303
304                 internal string LookupPrefix (string uri, bool atomizedName)
305                 {
306                         if (uri == null)
307                                 return null;
308
309                         if (CompareString (uri, DefaultNamespace, atomizedName))
310                                 return string.Empty;
311
312                         if (CompareString (uri, XmlnsXml, atomizedName))
313                                 return PrefixXml;
314                         
315                         if (CompareString (uri, XmlnsXmlns, atomizedName))
316                                 return PrefixXmlns;
317
318                         for (int i = declPos; i >= 0; i--) {
319                                 if (CompareString (decls [i].Uri, uri, atomizedName) && decls [i].Prefix.Length > 0) // we already looked for ""
320                                         return decls [i].Prefix;
321                         }
322
323                         // ECMA specifies that this method returns String.Empty
324                         // in case of no match. But actually MS.NET returns null.
325                         // For more information,see
326                         //  http://lists.ximian.com/archives/public/mono-list/2003-January/005071.html
327                         //return String.Empty;
328                         return null;
329                 }
330
331                 public virtual bool PopScope ()
332                 {
333                         if (scopePos == -1)
334                                 return false;
335
336                         declPos -= count;
337                         defaultNamespace = scopes [scopePos].DefaultNamespace;
338                         count = scopes [scopePos].DeclCount;
339                         scopePos --;
340                         return true;
341                 }
342
343                 public virtual void PushScope ()
344                 {
345                         scopePos ++;
346                         if (scopePos == scopes.Length)
347                                 GrowScopes ();
348                         
349                         scopes [scopePos].DefaultNamespace = defaultNamespace;
350                         scopes [scopePos].DeclCount = count;
351                         count = 0;
352                 }
353
354                 // It is rarely used, so we don't need NameTable optimization on it.
355                 public virtual void RemoveNamespace (string prefix, string uri)
356                 {
357                         RemoveNamespace (prefix, uri, false);
358                 }
359
360                 internal virtual void RemoveNamespace (string prefix, string uri, bool atomizedNames)
361                 {
362                         if (prefix == null)
363                                 throw new ArgumentNullException ("prefix");
364
365                         if (uri == null)
366                                 throw new ArgumentNullException ("uri");
367                         
368                         if (count == 0)
369                                 return;
370
371                         for (int i = declPos; i > declPos - count; i--) {
372                                 if (CompareString (decls [i].Prefix, prefix, atomizedNames) && CompareString (decls [i].Uri, uri, atomizedNames))
373                                         decls [i].Uri = null;
374                         }
375                 }
376
377                 #endregion
378         }
379 }