Revert "[system.xml] System.Xml.Serialization.Advanced from reference sources"
[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                 public
54                 CodeIdentifiers (bool caseSensitive)
55                 {
56                         StringComparer c = caseSensitive ?
57                                 StringComparer.Ordinal :
58                                 StringComparer.OrdinalIgnoreCase;
59                         table = new Hashtable (c);
60                         reserved = new Hashtable (c);
61                 }
62
63                 #endregion // Constructors
64
65                 #region Properties
66
67                 public bool UseCamelCasing {
68                         get { return useCamelCasing; }
69                         set { useCamelCasing = value; }
70                 }
71
72                 #endregion // Properties
73
74                 #region Methods
75
76                 public void Add (string identifier, object value)
77                 {
78                         table.Add (identifier, value);
79                 }
80
81                 public void AddReserved (string identifier)
82                 {
83                         reserved.Add (identifier, identifier);
84                 }
85
86                 public string AddUnique (string identifier, object value)
87                 {
88                         string unique = MakeUnique (identifier);
89                         Add (unique, value);
90                         return unique;
91                 }
92
93                 public void Clear ()
94                 {
95                         table.Clear ();
96                 }
97
98                 public bool IsInUse (string identifier)
99                 {
100                         return (table.ContainsKey (identifier) || reserved.ContainsKey (identifier));
101                 }
102
103                 public string MakeRightCase (string identifier)
104                 {
105                         if (UseCamelCasing)
106                                 return CodeIdentifier.MakeCamel (identifier);
107                         else
108                                 return CodeIdentifier.MakePascal (identifier);
109                 }
110
111                 public string MakeUnique (string identifier)
112                 {
113                         string uniqueIdentifier = identifier;
114                         int i = 1;
115
116                         while (IsInUse (uniqueIdentifier)) {
117                                 uniqueIdentifier = String.Format (CultureInfo.InvariantCulture, "{0}{1}", identifier, i);
118                                 i += 1;
119                         }
120
121                         return uniqueIdentifier;
122                 }
123
124                 public void Remove (string identifier)
125                 {
126                         table.Remove (identifier);
127                 }
128
129                 public void RemoveReserved (string identifier)
130                 {
131                         reserved.Remove (identifier);
132                 }
133
134                 public object ToArray (Type type)
135                 {
136                         Array list = Array.CreateInstance (type, table.Count);
137                         table.CopyTo (list, 0);
138                         return list;
139                 }
140
141                 #endregion // Methods
142         }
143 }