1 //------------------------------------------------------------------------------
2 // <copyright file="CodeIdentifiers.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 namespace System.Xml.Serialization {
11 using System.Collections;
13 using System.Globalization;
15 class CaseInsensitiveKeyComparer : CaseInsensitiveComparer, IEqualityComparer{
16 public CaseInsensitiveKeyComparer() : base(CultureInfo.CurrentCulture) {
19 bool IEqualityComparer.Equals(Object x, Object y) {
20 return (Compare(x, y) == 0);
23 int IEqualityComparer.GetHashCode(Object obj) {
24 string s = obj as string;
26 throw new ArgumentException(null, "obj");
28 return s.ToUpper(CultureInfo.CurrentCulture).GetHashCode();
32 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers"]/*' />
35 /// <para>[To be supplied.]</para>
37 public class CodeIdentifiers {
38 Hashtable identifiers;
39 Hashtable reservedIdentifiers;
43 public CodeIdentifiers() : this(true) {
46 public CodeIdentifiers(bool caseSensitive) {
48 identifiers = new Hashtable();
49 reservedIdentifiers = new Hashtable();
52 IEqualityComparer comparer = new CaseInsensitiveKeyComparer();
53 identifiers = new Hashtable(comparer);
54 reservedIdentifiers = new Hashtable(comparer);
57 list = new ArrayList();
60 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Clear"]/*' />
66 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.UseCamelCasing"]/*' />
68 /// <para>[To be supplied.]</para>
70 public bool UseCamelCasing {
71 get { return camelCase; }
72 set { camelCase = value; }
75 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.MakeRightCase"]/*' />
77 /// <para>[To be supplied.]</para>
79 public string MakeRightCase(string identifier) {
81 return CodeIdentifier.MakeCamel(identifier);
83 return CodeIdentifier.MakePascal(identifier);
86 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.MakeUnique"]/*' />
88 /// <para>[To be supplied.]</para>
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;
100 // Check that we did not violate the identifier length after appending the suffix.
101 if (identifier.Length > CodeIdentifier.MaxIdentifierLength) {
102 return MakeUnique("Item");
107 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.AddReserved"]/*' />
109 /// <para>[To be supplied.]</para>
111 public void AddReserved(string identifier) {
112 reservedIdentifiers.Add(identifier, identifier);
115 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.RemoveReserved"]/*' />
117 /// <para>[To be supplied.]</para>
119 public void RemoveReserved(string identifier) {
120 reservedIdentifiers.Remove(identifier);
123 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.AddUnique"]/*' />
125 /// <para>[To be supplied.]</para>
127 public string AddUnique(string identifier, object value) {
128 identifier = MakeUnique(identifier);
129 Add(identifier, value);
133 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.IsInUse"]/*' />
135 /// <para>[To be supplied.]</para>
137 public bool IsInUse(string identifier) {
138 return identifiers.Contains(identifier) || reservedIdentifiers.Contains(identifier);
141 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Add"]/*' />
143 /// <para>[To be supplied.]</para>
145 public void Add(string identifier, object value) {
146 identifiers.Add(identifier, value);
150 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.Remove"]/*' />
152 /// <para>[To be supplied.]</para>
154 public void Remove(string identifier) {
155 list.Remove(identifiers[identifier]);
156 identifiers.Remove(identifier);
159 /// <include file='doc\CodeIdentifiers.uex' path='docs/doc[@for="CodeIdentifiers.ToArray"]/*' />
161 /// <para>[To be supplied.]</para>
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);
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;
178 return newIdentifiers;