CodeIdentifiers.cs 6.8 KB

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