33c37fb941cb661956995bbdf07f40a63f5d79c1
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / Serialization / CodeIdentifiers.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CodeIdentifiers.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>                                                                
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml.Serialization {
9     
10     using System;
11     using System.Collections;
12     using System.IO;
13     using System.Globalization;
14     
15     class CaseInsensitiveKeyComparer : CaseInsensitiveComparer, IEqualityComparer{
16         public CaseInsensitiveKeyComparer() : base(CultureInfo.CurrentCulture) {
17         }
18
19         bool IEqualityComparer.Equals(Object x, Object y) {
20             return (Compare(x, y) == 0);
21         }
22         
23         int IEqualityComparer.GetHashCode(Object obj) {
24             string s = obj as string;
25             if (s == null)
26                 throw new ArgumentException(null, "obj");
27
28             return s.ToUpper(CultureInfo.CurrentCulture).GetHashCode();
29         }
30     }
31
32     /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers"]/*' />
33     ///<internalonly/>
34     /// <devdoc>
35     ///    <para>[To be supplied.]</para>
36     /// </devdoc>
37     public class CodeIdentifiers {
38         Hashtable identifiers;
39         Hashtable reservedIdentifiers;
40         ArrayList list;
41         bool camelCase;
42
43         public CodeIdentifiers() : this(true) {
44         }
45         
46         public CodeIdentifiers(bool caseSensitive) {
47             if (caseSensitive) {
48                 identifiers = new Hashtable();
49                 reservedIdentifiers = new Hashtable();
50             }
51             else {
52                 IEqualityComparer comparer = new CaseInsensitiveKeyComparer();
53                 identifiers = new Hashtable(comparer);
54                 reservedIdentifiers = new Hashtable(comparer);
55             }
56             
57             list = new ArrayList();
58         }
59
60         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Clear"]/*' />
61         public void Clear(){
62             identifiers.Clear();
63             list.Clear();
64         }
65
66         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.UseCamelCasing"]/*' />
67         /// <devdoc>
68         ///    <para>[To be supplied.]</para>
69         /// </devdoc>
70         public bool UseCamelCasing {
71             get { return camelCase; }
72             set { camelCase = value; }
73         }
74
75         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.MakeRightCase"]/*' />
76         /// <devdoc>
77         ///    <para>[To be supplied.]</para>
78         /// </devdoc>
79         public string MakeRightCase(string identifier) {
80             if (camelCase)
81                 return CodeIdentifier.MakeCamel(identifier);
82             else
83                 return CodeIdentifier.MakePascal(identifier);
84         }
85         
86         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.MakeUnique"]/*' />
87         /// <devdoc>
88         ///    <para>[To be supplied.]</para>
89         /// </devdoc>
90         public string MakeUnique(string identifier) {
91             if (IsInUse(identifier)) {
92                 for (int i = 1; ; i++) {
93                     string newIdentifier = identifier + i.ToString(CultureInfo.InvariantCulture);
94                     if (!IsInUse(newIdentifier)) {
95                         identifier = newIdentifier;
96                         break;
97                     }
98                 }
99             }
100             // Check that we did not violate the identifier length after appending the suffix.
101             if (identifier.Length > CodeIdentifier.MaxIdentifierLength) {
102                 return MakeUnique("Item");
103             }
104             return identifier;
105         }
106
107         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.AddReserved"]/*' />
108         /// <devdoc>
109         ///    <para>[To be supplied.]</para>
110         /// </devdoc>
111         public void AddReserved(string identifier) {
112             reservedIdentifiers.Add(identifier, identifier);
113         }
114         
115         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.RemoveReserved"]/*' />
116         /// <devdoc>
117         ///    <para>[To be supplied.]</para>
118         /// </devdoc>
119         public void RemoveReserved(string identifier) {
120             reservedIdentifiers.Remove(identifier);
121         }
122         
123         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.AddUnique"]/*' />
124         /// <devdoc>
125         ///    <para>[To be supplied.]</para>
126         /// </devdoc>
127         public string AddUnique(string identifier, object value) {
128             identifier = MakeUnique(identifier);
129             Add(identifier, value);
130             return identifier;
131         }
132         
133         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.IsInUse"]/*' />
134         /// <devdoc>
135         ///    <para>[To be supplied.]</para>
136         /// </devdoc>
137         public bool IsInUse(string identifier) {
138             return identifiers.Contains(identifier) || reservedIdentifiers.Contains(identifier);
139         }
140         
141         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Add"]/*' />
142         /// <devdoc>
143         ///    <para>[To be supplied.]</para>
144         /// </devdoc>
145         public void Add(string identifier, object value) {
146             identifiers.Add(identifier, value);
147             list.Add(value);
148         }
149         
150         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Remove"]/*' />
151         /// <devdoc>
152         ///    <para>[To be supplied.]</para>
153         /// </devdoc>
154         public void Remove(string identifier) {
155             list.Remove(identifiers[identifier]);
156             identifiers.Remove(identifier);
157         }
158         
159         /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.ToArray"]/*' />
160         /// <devdoc>
161         ///    <para>[To be supplied.]</para>
162         /// </devdoc>
163         public object ToArray(Type type) {
164             //Array array = Array.CreateInstance(type, identifiers.Values.Count);
165             //identifiers.Values.CopyTo(array, 0);
166             Array array = Array.CreateInstance(type, list.Count);
167             list.CopyTo(array, 0);
168             return array;
169         }
170
171         internal CodeIdentifiers Clone() {
172             CodeIdentifiers newIdentifiers = new CodeIdentifiers();
173             newIdentifiers.identifiers = (Hashtable)this.identifiers.Clone();
174             newIdentifiers.reservedIdentifiers = (Hashtable)this.reservedIdentifiers.Clone();
175             newIdentifiers.list = (ArrayList)this.list.Clone();
176             newIdentifiers.camelCase = this.camelCase;
177
178             return newIdentifiers;
179         }
180     }
181 }