SchemaHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Runtime.Serialization
  5. {
  6. using System;
  7. using System.Xml;
  8. using System.Xml.Schema;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using SchemaObjectDictionary = System.Collections.Generic.Dictionary<System.Xml.XmlQualifiedName, SchemaObjectInfo>;
  12. internal class SchemaObjectInfo
  13. {
  14. internal XmlSchemaType type;
  15. internal XmlSchemaElement element;
  16. internal XmlSchema schema;
  17. internal List<XmlSchemaType> knownTypes;
  18. internal SchemaObjectInfo(XmlSchemaType type, XmlSchemaElement element, XmlSchema schema, List<XmlSchemaType> knownTypes)
  19. {
  20. this.type = type;
  21. this.element = element;
  22. this.schema = schema;
  23. this.knownTypes = knownTypes;
  24. }
  25. }
  26. internal static class SchemaHelper
  27. {
  28. internal static bool NamespacesEqual(string ns1, string ns2)
  29. {
  30. if (ns1 == null || ns1.Length == 0)
  31. return (ns2 == null || ns2.Length == 0);
  32. else
  33. return ns1 == ns2;
  34. }
  35. internal static XmlSchemaType GetSchemaType(XmlSchemaSet schemas, XmlQualifiedName typeQName, out XmlSchema outSchema)
  36. {
  37. outSchema = null;
  38. ICollection currentSchemas = schemas.Schemas();
  39. string ns = typeQName.Namespace;
  40. foreach (XmlSchema schema in currentSchemas)
  41. {
  42. if (NamespacesEqual(ns, schema.TargetNamespace))
  43. {
  44. outSchema = schema;
  45. foreach (XmlSchemaObject schemaObj in schema.Items)
  46. {
  47. XmlSchemaType schemaType = schemaObj as XmlSchemaType;
  48. if (schemaType != null && schemaType.Name == typeQName.Name)
  49. {
  50. return schemaType;
  51. }
  52. }
  53. }
  54. }
  55. return null;
  56. }
  57. internal static XmlSchemaType GetSchemaType(SchemaObjectDictionary schemaInfo, XmlQualifiedName typeName)
  58. {
  59. SchemaObjectInfo schemaObjectInfo;
  60. if (schemaInfo.TryGetValue(typeName, out schemaObjectInfo))
  61. {
  62. return schemaObjectInfo.type;
  63. }
  64. return null;
  65. }
  66. internal static XmlSchema GetSchemaWithType(SchemaObjectDictionary schemaInfo, XmlSchemaSet schemas, XmlQualifiedName typeName)
  67. {
  68. SchemaObjectInfo schemaObjectInfo;
  69. if (schemaInfo.TryGetValue(typeName, out schemaObjectInfo))
  70. {
  71. if (schemaObjectInfo.schema != null)
  72. return schemaObjectInfo.schema;
  73. }
  74. ICollection currentSchemas = schemas.Schemas();
  75. string ns = typeName.Namespace;
  76. foreach (XmlSchema schema in currentSchemas)
  77. {
  78. if (NamespacesEqual(ns, schema.TargetNamespace))
  79. {
  80. return schema;
  81. }
  82. }
  83. return null;
  84. }
  85. internal static XmlSchemaElement GetSchemaElement(XmlSchemaSet schemas, XmlQualifiedName elementQName, out XmlSchema outSchema)
  86. {
  87. outSchema = null;
  88. ICollection currentSchemas = schemas.Schemas();
  89. string ns = elementQName.Namespace;
  90. foreach (XmlSchema schema in currentSchemas)
  91. {
  92. if (NamespacesEqual(ns, schema.TargetNamespace))
  93. {
  94. outSchema = schema;
  95. foreach (XmlSchemaObject schemaObj in schema.Items)
  96. {
  97. XmlSchemaElement schemaElement = schemaObj as XmlSchemaElement;
  98. if (schemaElement != null && schemaElement.Name == elementQName.Name)
  99. {
  100. return schemaElement;
  101. }
  102. }
  103. }
  104. }
  105. return null;
  106. }
  107. internal static XmlSchemaElement GetSchemaElement(SchemaObjectDictionary schemaInfo, XmlQualifiedName elementName)
  108. {
  109. SchemaObjectInfo schemaObjectInfo;
  110. if (schemaInfo.TryGetValue(elementName, out schemaObjectInfo))
  111. {
  112. return schemaObjectInfo.element;
  113. }
  114. return null;
  115. }
  116. internal static XmlSchema GetSchema(string ns, XmlSchemaSet schemas)
  117. {
  118. if (ns == null) { ns = String.Empty; }
  119. ICollection currentSchemas = schemas.Schemas();
  120. foreach (XmlSchema schema in currentSchemas)
  121. {
  122. if ((schema.TargetNamespace == null && ns.Length == 0) || ns.Equals(schema.TargetNamespace))
  123. {
  124. return schema;
  125. }
  126. }
  127. return CreateSchema(ns, schemas);
  128. }
  129. static XmlSchema CreateSchema(string ns, XmlSchemaSet schemas)
  130. {
  131. XmlSchema schema = new XmlSchema();
  132. schema.ElementFormDefault = XmlSchemaForm.Qualified;
  133. if (ns.Length > 0)
  134. {
  135. schema.TargetNamespace = ns;
  136. schema.Namespaces.Add(Globals.TnsPrefix, ns);
  137. }
  138. schemas.Add(schema);
  139. return schema;
  140. }
  141. internal static void AddElementForm(XmlSchemaElement element, XmlSchema schema)
  142. {
  143. if (schema.ElementFormDefault != XmlSchemaForm.Qualified)
  144. {
  145. element.Form = XmlSchemaForm.Qualified;
  146. }
  147. }
  148. internal static void AddSchemaImport(string ns, XmlSchema schema)
  149. {
  150. if (SchemaHelper.NamespacesEqual(ns, schema.TargetNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaInstanceNamespace))
  151. return;
  152. foreach (object item in schema.Includes)
  153. {
  154. if (item is XmlSchemaImport)
  155. {
  156. if (SchemaHelper.NamespacesEqual(ns, ((XmlSchemaImport)item).Namespace))
  157. return;
  158. }
  159. }
  160. XmlSchemaImport import = new XmlSchemaImport();
  161. if (ns != null && ns.Length > 0)
  162. import.Namespace = ns;
  163. schema.Includes.Add(import);
  164. }
  165. internal static XmlSchema GetSchemaWithGlobalElementDeclaration(XmlSchemaElement element, XmlSchemaSet schemas)
  166. {
  167. ICollection currentSchemas = schemas.Schemas();
  168. foreach (XmlSchema schema in currentSchemas)
  169. {
  170. foreach (XmlSchemaObject schemaObject in schema.Items)
  171. {
  172. XmlSchemaElement schemaElement = schemaObject as XmlSchemaElement;
  173. if (schemaElement == null)
  174. continue;
  175. if (schemaElement == element)
  176. {
  177. return schema;
  178. }
  179. }
  180. }
  181. return null;
  182. }
  183. internal static XmlQualifiedName GetGlobalElementDeclaration(XmlSchemaSet schemas, XmlQualifiedName typeQName, out bool isNullable)
  184. {
  185. ICollection currentSchemas = schemas.Schemas();
  186. string ns = typeQName.Namespace;
  187. if (ns == null)
  188. ns = string.Empty;
  189. isNullable = false;
  190. foreach (XmlSchema schema in currentSchemas)
  191. {
  192. foreach (XmlSchemaObject schemaObject in schema.Items)
  193. {
  194. XmlSchemaElement schemaElement = schemaObject as XmlSchemaElement;
  195. if (schemaElement == null)
  196. continue;
  197. if (schemaElement.SchemaTypeName.Equals(typeQName))
  198. {
  199. isNullable = schemaElement.IsNillable;
  200. return new XmlQualifiedName(schemaElement.Name, schema.TargetNamespace);
  201. }
  202. }
  203. }
  204. return null;
  205. }
  206. }
  207. }