SerializationCodeGeneratorConfiguration.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // System.Xml.Serialization.SerializationCodeGeneratorConfiguration.cs:
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2002, 2003 Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Xml.Serialization;
  32. namespace System.Xml.Serialization
  33. {
  34. [XmlType ("configuration")]
  35. internal class SerializationCodeGeneratorConfiguration
  36. {
  37. [XmlElement ("serializer")]
  38. public SerializerInfo[] Serializers;
  39. }
  40. [XmlType ("serializer")]
  41. internal class SerializerInfo
  42. {
  43. [XmlAttribute ("class")]
  44. public string ClassName;
  45. [XmlAttribute ("assembly")]
  46. public string Assembly;
  47. [XmlElement ("reader")]
  48. public string ReaderClassName;
  49. [XmlElement ("writer")]
  50. public string WriterClassName;
  51. [XmlElement ("noreader")]
  52. public bool NoReader;
  53. [XmlElement ("nowriter")]
  54. public bool NoWriter;
  55. [XmlElement ("generateAsInternal")]
  56. public bool GenerateAsInternal;
  57. [XmlElement ("namespace")]
  58. public string Namespace;
  59. [XmlArray ("namespaceImports")]
  60. [XmlArrayItem ("namespaceImport")]
  61. public string [] NamespaceImports;
  62. [System.ComponentModel.DefaultValue (SerializationFormat.Literal)]
  63. public SerializationFormat SerializationFormat = SerializationFormat.Literal;
  64. [XmlElement ("outFileName")]
  65. public string OutFileName;
  66. [XmlArray ("readerHooks")]
  67. public Hook[] ReaderHooks;
  68. [XmlArray ("writerHooks")]
  69. public Hook[] WriterHooks;
  70. public ArrayList GetHooks (HookType hookType, HookDir dir, HookAction action, Type type, string member)
  71. {
  72. if (dir == HookDir.Read)
  73. return FindHook (ReaderHooks, hookType, action, type, member);
  74. else
  75. return FindHook (WriterHooks, hookType, action, type, member);
  76. }
  77. ArrayList FindHook (Hook[] hooks, HookType hookType, HookAction action, Type type, string member)
  78. {
  79. ArrayList foundHooks = new ArrayList ();
  80. if (hooks == null) return foundHooks;
  81. foreach (Hook hook in hooks)
  82. {
  83. if (action == HookAction.InsertBefore && (hook.InsertBefore == null || hook.InsertBefore == ""))
  84. continue;
  85. else if (action == HookAction.InsertAfter && (hook.InsertAfter == null || hook.InsertAfter == ""))
  86. continue;
  87. else if (action == HookAction.Replace && (hook.Replace == null || hook.Replace == ""))
  88. continue;
  89. if (hook.HookType != hookType)
  90. continue;
  91. if (hook.Select != null)
  92. {
  93. if (hook.Select.TypeName != null && hook.Select.TypeName != "")
  94. if (hook.Select.TypeName != type.FullName) continue;
  95. if (hook.Select.TypeMember != null && hook.Select.TypeMember != "")
  96. if (hook.Select.TypeMember != member) continue;
  97. if (hook.Select.TypeAttributes != null && hook.Select.TypeAttributes.Length > 0)
  98. {
  99. object[] ats = type.GetCustomAttributes (true);
  100. bool found = false;
  101. foreach (object at in ats)
  102. if (Array.IndexOf (hook.Select.TypeAttributes, at.GetType().FullName) != -1) { found = true; break; }
  103. if (!found) continue;
  104. }
  105. }
  106. foundHooks.Add (hook);
  107. }
  108. return foundHooks;
  109. }
  110. }
  111. [XmlType ("hook")]
  112. internal class Hook
  113. {
  114. [XmlAttribute ("type")]
  115. public HookType HookType;
  116. [XmlElement ("select")]
  117. public Select Select;
  118. [XmlElement ("insertBefore")]
  119. public string InsertBefore;
  120. [XmlElement ("insertAfter")]
  121. public string InsertAfter;
  122. [XmlElement ("replace")]
  123. public string Replace;
  124. public string GetCode (HookAction action)
  125. {
  126. if (action == HookAction.InsertBefore)
  127. return InsertBefore;
  128. else if (action == HookAction.InsertAfter)
  129. return InsertAfter;
  130. else
  131. return Replace;
  132. }
  133. }
  134. [XmlType ("select")]
  135. internal class Select
  136. {
  137. [XmlElement ("typeName")]
  138. public string TypeName;
  139. [XmlElement("typeAttribute")]
  140. public string[] TypeAttributes;
  141. [XmlElement ("typeMember")]
  142. public string TypeMember;
  143. }
  144. internal enum HookDir
  145. {
  146. Read,
  147. Write
  148. }
  149. internal enum HookAction
  150. {
  151. InsertBefore,
  152. InsertAfter,
  153. Replace
  154. }
  155. [XmlType ("hookType")]
  156. internal enum HookType
  157. {
  158. attributes,
  159. elements,
  160. unknownAttribute,
  161. unknownElement,
  162. member,
  163. type
  164. }
  165. }