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