SerializationCodeGeneratorConfiguration.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ("namespace")]
  52. public string Namespace;
  53. [XmlArray ("namespaceImports")]
  54. [XmlArrayItem ("namespaceImport")]
  55. public string [] NamespaceImports;
  56. [System.ComponentModel.DefaultValue (SerializationFormat.Literal)]
  57. public SerializationFormat SerializationFormat = SerializationFormat.Literal;
  58. [XmlElement ("outFileName")]
  59. public string OutFileName;
  60. [XmlArray ("readerHooks")]
  61. public Hook[] ReaderHooks;
  62. [XmlArray ("writerHooks")]
  63. public Hook[] WriterHooks;
  64. public ArrayList GetHooks (HookType hookType, HookDir dir, HookAction action, Type type, string member)
  65. {
  66. if (dir == HookDir.Read)
  67. return FindHook (ReaderHooks, hookType, action, type, member);
  68. else
  69. return FindHook (WriterHooks, hookType, action, type, member);
  70. }
  71. ArrayList FindHook (Hook[] hooks, HookType hookType, HookAction action, Type type, string member)
  72. {
  73. ArrayList foundHooks = new ArrayList ();
  74. if (hooks == null) return foundHooks;
  75. foreach (Hook hook in hooks)
  76. {
  77. if (action == HookAction.InsertBefore && (hook.InsertBefore == null || hook.InsertBefore == ""))
  78. continue;
  79. else if (action == HookAction.InsertAfter && (hook.InsertAfter == null || hook.InsertAfter == ""))
  80. continue;
  81. else if (action == HookAction.Replace && (hook.Replace == null || hook.Replace == ""))
  82. continue;
  83. if (hook.HookType != hookType)
  84. continue;
  85. if (hook.Select != null)
  86. {
  87. if (hook.Select.TypeName != null && hook.Select.TypeName != "")
  88. if (hook.Select.TypeName != type.FullName) continue;
  89. if (hook.Select.TypeMember != null && hook.Select.TypeMember != "")
  90. if (hook.Select.TypeMember != member) continue;
  91. if (hook.Select.TypeAttributes != null && hook.Select.TypeAttributes.Length > 0)
  92. {
  93. object[] ats = type.GetCustomAttributes (true);
  94. bool found = false;
  95. foreach (object at in ats)
  96. if (Array.IndexOf (hook.Select.TypeAttributes, at.GetType().FullName) != -1) { found = true; break; }
  97. if (!found) continue;
  98. }
  99. }
  100. foundHooks.Add (hook);
  101. }
  102. return foundHooks;
  103. }
  104. }
  105. [XmlType ("hook")]
  106. internal class Hook
  107. {
  108. [XmlAttribute ("type")]
  109. public HookType HookType;
  110. [XmlElement ("select")]
  111. public Select Select;
  112. [XmlElement ("insertBefore")]
  113. public string InsertBefore;
  114. [XmlElement ("insertAfter")]
  115. public string InsertAfter;
  116. [XmlElement ("replace")]
  117. public string Replace;
  118. public string GetCode (HookAction action)
  119. {
  120. if (action == HookAction.InsertBefore)
  121. return InsertBefore;
  122. else if (action == HookAction.InsertAfter)
  123. return InsertAfter;
  124. else
  125. return Replace;
  126. }
  127. }
  128. [XmlType ("select")]
  129. internal class Select
  130. {
  131. [XmlElement ("typeName")]
  132. public string TypeName;
  133. [XmlElement("typeAttribute")]
  134. public string[] TypeAttributes;
  135. [XmlElement ("typeMember")]
  136. public string TypeMember;
  137. }
  138. internal enum HookDir
  139. {
  140. Read,
  141. Write
  142. }
  143. internal enum HookAction
  144. {
  145. InsertBefore,
  146. InsertAfter,
  147. Replace
  148. }
  149. [XmlType ("hookType")]
  150. internal enum HookType
  151. {
  152. attributes,
  153. elements,
  154. unknownAttribute,
  155. unknownElement,
  156. member,
  157. type
  158. }
  159. }