Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.XML / System.Xml / XmlNameEntryCache.cs
1 //
2 // System.Xml.XmlNameEntryCache.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2005 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33
34 namespace System.Xml
35 {
36         internal class XmlNameEntryCache
37         {
38                 Hashtable table = new Hashtable ();
39                 XmlNameTable nameTable;
40                 XmlNameEntry dummy = new XmlNameEntry (String.Empty, String.Empty, String.Empty);
41                 char [] cacheBuffer;
42
43                 public XmlNameEntryCache (XmlNameTable nameTable)
44                 {
45                         this.nameTable = nameTable;
46                 }
47
48                 public string GetAtomizedPrefixedName (string prefix, string local)
49                 {
50                         if (prefix == null || prefix.Length == 0)
51                                 return local;
52
53                         if (cacheBuffer == null)
54                                 cacheBuffer = new char [20];
55                         if (cacheBuffer.Length < prefix.Length + local.Length + 1)
56                                 cacheBuffer = new char [Math.Max (
57                                         prefix.Length + local.Length + 1,
58                                         cacheBuffer.Length << 1)];
59                         prefix.CopyTo (0, cacheBuffer, 0, prefix.Length);
60                         cacheBuffer [prefix.Length] = ':';
61                         local.CopyTo (0, cacheBuffer, prefix.Length + 1, local.Length);
62                         return nameTable.Add (cacheBuffer, 0, prefix.Length + local.Length + 1);
63                 }
64
65                 public XmlNameEntry Add (string prefix, string local, string ns,
66                         bool atomic)
67                 {
68                         if (!atomic) {
69                                 prefix = nameTable.Add (prefix);
70                                 local = nameTable.Add (local);
71                                 ns = nameTable.Add (ns);
72                         }
73                         XmlNameEntry e = GetInternal (prefix, local, ns, true);
74                         if (e == null) {
75                                 e = new XmlNameEntry (prefix, local, ns);
76                                 table [e] = e;
77                         }
78                         return e;
79                 }
80
81                 public XmlNameEntry Get (string prefix, string local, string ns,
82                         bool atomic)
83                 {
84                         XmlNameEntry e = GetInternal (prefix, local, ns, atomic);
85                         return e;
86                 }
87
88                 private XmlNameEntry GetInternal (string prefix, string local,
89                         string ns, bool atomic)
90                 {
91                         if (!atomic) {
92                                 if (nameTable.Get (prefix) == null ||
93                                     nameTable.Get (local) == null ||
94                                     nameTable.Get (ns) == null)
95                                         return null;
96                         }
97                         dummy.Update (prefix, local, ns);
98
99                         return table [dummy] as XmlNameEntry;
100                 }
101         }
102 }