Merge pull request #1200 from akoeplinger/remove-jvm
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / CodeIdentifiers.cs
1 // 
2 // System.Xml.Serialization.CodeIdentifiers 
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
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 using System.Globalization;
34
35 namespace System.Xml.Serialization {
36         public class CodeIdentifiers {
37
38                 #region Fields
39
40                 bool useCamelCasing;
41                 Hashtable table;
42                 Hashtable reserved;
43
44                 #endregion
45
46                 #region Constructors
47
48                 public CodeIdentifiers ()
49                         : this (true)
50                 {
51                 }
52
53 #if NET_2_0
54                 public
55 #else
56                 private
57 #endif
58                 CodeIdentifiers (bool caseSensitive)
59                 {
60 #if NET_2_0
61                         StringComparer c = caseSensitive ?
62                                 StringComparer.Ordinal :
63                                 StringComparer.OrdinalIgnoreCase;
64                         table = new Hashtable (c);
65                         reserved = new Hashtable (c);
66 #else
67                         table = new Hashtable ();
68                         reserved = new Hashtable ();
69 #endif
70                 }
71
72                 #endregion // Constructors
73
74                 #region Properties
75
76                 public bool UseCamelCasing {
77                         get { return useCamelCasing; }
78                         set { useCamelCasing = value; }
79                 }
80
81                 #endregion // Properties
82
83                 #region Methods
84
85                 public void Add (string identifier, object value)
86                 {
87                         table.Add (identifier, value);
88                 }
89
90                 public void AddReserved (string identifier)
91                 {
92                         reserved.Add (identifier, identifier);
93                 }
94
95                 public string AddUnique (string identifier, object value)
96                 {
97                         string unique = MakeUnique (identifier);
98                         Add (unique, value);
99                         return unique;
100                 }
101
102                 public void Clear ()
103                 {
104                         table.Clear ();
105                 }
106
107                 public bool IsInUse (string identifier)
108                 {
109                         return (table.ContainsKey (identifier) || reserved.ContainsKey (identifier));
110                 }
111
112                 public string MakeRightCase (string identifier)
113                 {
114                         if (UseCamelCasing)
115                                 return CodeIdentifier.MakeCamel (identifier);
116                         else
117                                 return CodeIdentifier.MakePascal (identifier);
118                 }
119
120                 public string MakeUnique (string identifier)
121                 {
122                         string uniqueIdentifier = identifier;
123                         int i = 1;
124
125                         while (IsInUse (uniqueIdentifier)) {
126                                 uniqueIdentifier = String.Format (CultureInfo.InvariantCulture, "{0}{1}", identifier, i);
127                                 i += 1;
128                         }
129
130                         return uniqueIdentifier;
131                 }
132
133                 public void Remove (string identifier)
134                 {
135                         table.Remove (identifier);
136                 }
137
138                 public void RemoveReserved (string identifier)
139                 {
140                         reserved.Remove (identifier);
141                 }
142
143                 public object ToArray (Type type)
144                 {
145                         Array list = Array.CreateInstance (type, table.Count);
146                         table.CopyTo (list, 0);
147                         return list;
148                 }
149
150                 #endregion // Methods
151         }
152 }