ImportContext.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //------------------------------------------------------------------------------
  2. // <copyright file="ImportContext.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.IO;
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using System.Xml.Serialization;
  13. using System.Collections;
  14. using System.Collections.Specialized;
  15. #if !MONO_HYBRID_SYSTEM_XML
  16. public class ImportContext {
  17. bool shareTypes;
  18. SchemaObjectCache cache; // cached schema top-level items
  19. Hashtable mappings; // XmlSchema -> SerializableMapping, XmlSchemaSimpleType -> EnumMapping, XmlSchemaComplexType -> StructMapping
  20. Hashtable elements; // XmlSchemaElement -> ElementAccessor
  21. CodeIdentifiers typeIdentifiers;
  22. /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.ImportContext"]/*' />
  23. /// <devdoc>
  24. /// <para>[To be supplied.]</para>
  25. /// </devdoc>
  26. public ImportContext(CodeIdentifiers identifiers, bool shareTypes) {
  27. this.typeIdentifiers = identifiers;
  28. this.shareTypes = shareTypes;
  29. }
  30. internal ImportContext() : this(null, false) {}
  31. internal SchemaObjectCache Cache {
  32. get {
  33. if (cache == null)
  34. cache = new SchemaObjectCache();
  35. return cache;
  36. }
  37. }
  38. internal Hashtable Elements {
  39. get {
  40. if (elements == null)
  41. elements = new Hashtable();
  42. return elements;
  43. }
  44. }
  45. internal Hashtable Mappings {
  46. get {
  47. if (mappings == null)
  48. mappings = new Hashtable();
  49. return mappings;
  50. }
  51. }
  52. /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.TypeIdentifiers"]/*' />
  53. /// <devdoc>
  54. /// <para>[To be supplied.]</para>
  55. /// </devdoc>
  56. public CodeIdentifiers TypeIdentifiers {
  57. get {
  58. if (typeIdentifiers == null)
  59. typeIdentifiers = new CodeIdentifiers();
  60. return typeIdentifiers;
  61. }
  62. }
  63. /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.ShareTypes"]/*' />
  64. /// <devdoc>
  65. /// <para>[To be supplied.]</para>
  66. /// </devdoc>
  67. public bool ShareTypes {
  68. get { return shareTypes; }
  69. }
  70. /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.Warnings"]/*' />
  71. /// <devdoc>
  72. /// <para>[To be supplied.]</para>
  73. /// </devdoc>
  74. public StringCollection Warnings {
  75. get { return Cache.Warnings; }
  76. }
  77. }
  78. #endif
  79. internal class SchemaObjectCache {
  80. Hashtable graph;
  81. Hashtable hash;
  82. Hashtable objectCache;
  83. StringCollection warnings;
  84. //
  85. internal Hashtable looks = new Hashtable();
  86. Hashtable Graph {
  87. get {
  88. if (graph == null)
  89. graph = new Hashtable();
  90. return graph;
  91. }
  92. }
  93. Hashtable Hash {
  94. get {
  95. if (hash == null)
  96. hash = new Hashtable();
  97. return hash;
  98. }
  99. }
  100. Hashtable ObjectCache {
  101. get {
  102. if (objectCache == null)
  103. objectCache = new Hashtable();
  104. return objectCache;
  105. }
  106. }
  107. internal StringCollection Warnings {
  108. get {
  109. if (warnings == null)
  110. warnings = new StringCollection();
  111. return warnings;
  112. }
  113. }
  114. internal XmlSchemaObject AddItem(XmlSchemaObject item, XmlQualifiedName qname, XmlSchemas schemas) {
  115. if (item == null)
  116. return null;
  117. if (qname == null || qname.IsEmpty)
  118. return null;
  119. string key = item.GetType().Name + ":" + qname.ToString();
  120. ArrayList list = (ArrayList)ObjectCache[key];
  121. if (list == null) {
  122. list = new ArrayList();
  123. ObjectCache[key] = list;
  124. }
  125. for (int i = 0; i < list.Count; i++) {
  126. XmlSchemaObject cachedItem = (XmlSchemaObject)list[i];
  127. if (cachedItem == item)
  128. return cachedItem;
  129. if (Match(cachedItem, item, true)) {
  130. return cachedItem;
  131. }
  132. else {
  133. Warnings.Add(Res.GetString(Res.XmlMismatchSchemaObjects, item.GetType().Name, qname.Name, qname.Namespace));
  134. Warnings.Add("DEBUG:Cached item key:\r\n" + (string)looks[cachedItem] + "\r\nnew item key:\r\n" + (string)looks[item]);
  135. }
  136. }
  137. // no match found we need to insert the new type in the cache
  138. list.Add(item);
  139. return item;
  140. }
  141. internal bool Match(XmlSchemaObject o1, XmlSchemaObject o2, bool shareTypes) {
  142. if (o1 == o2)
  143. return true;
  144. if (o1.GetType() != o2.GetType())
  145. return false;
  146. if (Hash[o1] == null)
  147. Hash[o1] = GetHash(o1);
  148. int hash1 = (int)Hash[o1];
  149. int hash2 = GetHash(o2);
  150. if (hash1 != hash2)
  151. return false;
  152. if (shareTypes)
  153. return CompositeHash(o1, hash1) == CompositeHash(o2, hash2);
  154. return true;
  155. }
  156. private ArrayList GetDependencies(XmlSchemaObject o, ArrayList deps, Hashtable refs) {
  157. if (refs[o] == null) {
  158. refs[o] = o;
  159. deps.Add(o);
  160. ArrayList list = Graph[o] as ArrayList;
  161. if (list != null) {
  162. for (int i = 0; i < list.Count; i++) {
  163. GetDependencies((XmlSchemaObject)list[i], deps, refs);
  164. }
  165. }
  166. }
  167. return deps;
  168. }
  169. private int CompositeHash(XmlSchemaObject o, int hash) {
  170. ArrayList list = GetDependencies(o, new ArrayList(), new Hashtable());
  171. double tmp = 0;
  172. for (int i = 0; i < list.Count; i++) {
  173. object cachedHash = Hash[list[i]];
  174. if (cachedHash is int) {
  175. tmp += (int)cachedHash/list.Count;
  176. }
  177. }
  178. return (int)tmp;
  179. }
  180. internal void GenerateSchemaGraph(XmlSchemas schemas) {
  181. SchemaGraph graph = new SchemaGraph(Graph, schemas);
  182. ArrayList items = graph.GetItems();
  183. for (int i = 0; i < items.Count; i++) {
  184. GetHash((XmlSchemaObject)items[i]);
  185. }
  186. }
  187. private int GetHash(XmlSchemaObject o) {
  188. object hash = Hash[o];
  189. if (hash != null) {
  190. if (hash is XmlSchemaObject) {
  191. }
  192. else {
  193. return (int)hash;
  194. }
  195. }
  196. // new object, generate the hash
  197. string hashString = ToString(o, new SchemaObjectWriter());
  198. looks[o] = hashString;
  199. int code = hashString.GetHashCode();
  200. Hash[o] = code;
  201. return code;
  202. }
  203. string ToString(XmlSchemaObject o, SchemaObjectWriter writer) {
  204. return writer.WriteXmlSchemaObject(o);
  205. }
  206. }
  207. internal class SchemaGraph {
  208. ArrayList empty = new ArrayList();
  209. XmlSchemas schemas;
  210. Hashtable scope;
  211. int items;
  212. internal SchemaGraph(Hashtable scope, XmlSchemas schemas) {
  213. this.scope = scope;
  214. schemas.Compile(null, false);
  215. this.schemas = schemas;
  216. items = 0;
  217. foreach(XmlSchema s in schemas) {
  218. items += s.Items.Count;
  219. foreach (XmlSchemaObject item in s.Items) {
  220. Depends(item);
  221. }
  222. }
  223. }
  224. internal ArrayList GetItems() {
  225. return new ArrayList(scope.Keys);
  226. }
  227. internal void AddRef(ArrayList list, XmlSchemaObject o) {
  228. if (o == null)
  229. return;
  230. if (schemas.IsReference(o))
  231. return;
  232. if (o.Parent is XmlSchema) {
  233. string ns = ((XmlSchema)o.Parent).TargetNamespace;
  234. if (ns == XmlSchema.Namespace)
  235. return;
  236. if (list.Contains(o))
  237. return;
  238. list.Add(o);
  239. }
  240. }
  241. internal ArrayList Depends(XmlSchemaObject item) {
  242. if (item.Parent is XmlSchema) {
  243. if (scope[item] != null)
  244. return (ArrayList)scope[item];
  245. ArrayList refs = new ArrayList();
  246. Depends(item, refs);
  247. scope.Add(item, refs);
  248. return refs;
  249. }
  250. return empty;
  251. }
  252. internal void Depends(XmlSchemaObject item, ArrayList refs) {
  253. if (item == null || scope[item] != null)
  254. return;
  255. Type t = item.GetType();
  256. if (typeof(XmlSchemaType).IsAssignableFrom(t)) {
  257. XmlQualifiedName baseName = XmlQualifiedName.Empty;
  258. XmlSchemaType baseType = null;
  259. XmlSchemaParticle particle = null;
  260. XmlSchemaObjectCollection attributes = null;
  261. if (item is XmlSchemaComplexType) {
  262. XmlSchemaComplexType ct = (XmlSchemaComplexType)item;
  263. if (ct.ContentModel != null) {
  264. XmlSchemaContent content = ct.ContentModel.Content;
  265. if (content is XmlSchemaComplexContentRestriction) {
  266. baseName = ((XmlSchemaComplexContentRestriction)content).BaseTypeName;
  267. attributes = ((XmlSchemaComplexContentRestriction)content).Attributes;
  268. }
  269. else if (content is XmlSchemaSimpleContentRestriction) {
  270. XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction)content;
  271. if (restriction.BaseType != null)
  272. baseType = restriction.BaseType;
  273. else
  274. baseName = restriction.BaseTypeName;
  275. attributes = restriction.Attributes;
  276. }
  277. else if (content is XmlSchemaComplexContentExtension) {
  278. XmlSchemaComplexContentExtension extension = (XmlSchemaComplexContentExtension)content;
  279. attributes = extension.Attributes;
  280. particle = extension.Particle;
  281. baseName = extension.BaseTypeName;
  282. }
  283. else if (content is XmlSchemaSimpleContentExtension) {
  284. XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension)content;
  285. attributes = extension.Attributes;
  286. baseName = extension.BaseTypeName;
  287. }
  288. }
  289. else {
  290. attributes = ct.Attributes;
  291. particle = ct.Particle;
  292. }
  293. if (particle is XmlSchemaGroupRef) {
  294. XmlSchemaGroupRef refGroup = (XmlSchemaGroupRef)particle;
  295. particle = ((XmlSchemaGroup)schemas.Find(refGroup.RefName, typeof(XmlSchemaGroup), false)).Particle;
  296. }
  297. else if (particle is XmlSchemaGroupBase) {
  298. particle = (XmlSchemaGroupBase)particle;
  299. }
  300. }
  301. else if (item is XmlSchemaSimpleType) {
  302. XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)item;
  303. XmlSchemaSimpleTypeContent content = simpleType.Content;
  304. if (content is XmlSchemaSimpleTypeRestriction) {
  305. baseType = ((XmlSchemaSimpleTypeRestriction)content).BaseType;
  306. baseName = ((XmlSchemaSimpleTypeRestriction)content).BaseTypeName;
  307. }
  308. else if (content is XmlSchemaSimpleTypeList) {
  309. XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)content;
  310. if (list.ItemTypeName != null && !list.ItemTypeName.IsEmpty)
  311. baseName = list.ItemTypeName;
  312. if (list.ItemType != null) {
  313. baseType = list.ItemType;
  314. }
  315. }
  316. else if (content is XmlSchemaSimpleTypeRestriction) {
  317. baseName = ((XmlSchemaSimpleTypeRestriction)content).BaseTypeName;
  318. }
  319. else if (t == typeof(XmlSchemaSimpleTypeUnion)) {
  320. XmlQualifiedName[] memberTypes = ((XmlSchemaSimpleTypeUnion)item).MemberTypes;
  321. if (memberTypes != null) {
  322. for (int i = 0; i < memberTypes.Length; i++) {
  323. XmlSchemaType type = (XmlSchemaType)schemas.Find(memberTypes[i], typeof(XmlSchemaType), false);
  324. AddRef(refs, type);
  325. }
  326. }
  327. }
  328. }
  329. if (baseType == null && !baseName.IsEmpty && baseName.Namespace != XmlSchema.Namespace)
  330. baseType = (XmlSchemaType)schemas.Find(baseName, typeof(XmlSchemaType), false);
  331. if (baseType != null) {
  332. AddRef(refs, baseType);
  333. }
  334. if (particle != null) {
  335. Depends(particle, refs);
  336. }
  337. if (attributes != null) {
  338. for (int i = 0; i < attributes.Count; i++) {
  339. Depends(attributes[i], refs);
  340. }
  341. }
  342. }
  343. else if (t == typeof(XmlSchemaElement)) {
  344. XmlSchemaElement el = (XmlSchemaElement)item;
  345. if (!el.SubstitutionGroup.IsEmpty) {
  346. if (el.SubstitutionGroup.Namespace != XmlSchema.Namespace) {
  347. XmlSchemaElement head = (XmlSchemaElement)schemas.Find(el.SubstitutionGroup, typeof(XmlSchemaElement), false);
  348. AddRef(refs, head);
  349. }
  350. }
  351. if (!el.RefName.IsEmpty) {
  352. el = (XmlSchemaElement)schemas.Find(el.RefName, typeof(XmlSchemaElement), false);
  353. AddRef(refs, el);
  354. }
  355. else if (!el.SchemaTypeName.IsEmpty) {
  356. XmlSchemaType type = (XmlSchemaType)schemas.Find(el.SchemaTypeName, typeof(XmlSchemaType), false);
  357. AddRef(refs, type);
  358. }
  359. else {
  360. Depends(el.SchemaType, refs);
  361. }
  362. }
  363. else if (t == typeof(XmlSchemaGroup)) {
  364. Depends(((XmlSchemaGroup)item).Particle);
  365. }
  366. else if (t == typeof(XmlSchemaGroupRef)) {
  367. XmlSchemaGroup group = (XmlSchemaGroup)schemas.Find(((XmlSchemaGroupRef)item).RefName, typeof(XmlSchemaGroup), false);
  368. AddRef(refs, group);
  369. }
  370. else if (typeof(XmlSchemaGroupBase).IsAssignableFrom(t)) {
  371. foreach (XmlSchemaObject o in ((XmlSchemaGroupBase)item).Items) {
  372. Depends(o, refs);
  373. }
  374. }
  375. else if (t == typeof(XmlSchemaAttributeGroupRef)) {
  376. XmlSchemaAttributeGroup group = (XmlSchemaAttributeGroup)schemas.Find(((XmlSchemaAttributeGroupRef)item).RefName, typeof(XmlSchemaAttributeGroup), false);
  377. AddRef(refs, group);
  378. }
  379. else if (t == typeof(XmlSchemaAttributeGroup)) {
  380. foreach (XmlSchemaObject o in ((XmlSchemaAttributeGroup)item).Attributes) {
  381. Depends(o, refs);
  382. }
  383. }
  384. else if (t == typeof(XmlSchemaAttribute)) {
  385. XmlSchemaAttribute at = (XmlSchemaAttribute)item;
  386. if (!at.RefName.IsEmpty) {
  387. at = (XmlSchemaAttribute)schemas.Find(at.RefName, typeof(XmlSchemaAttribute), false);
  388. AddRef(refs, at);
  389. }
  390. else if (!at.SchemaTypeName.IsEmpty) {
  391. XmlSchemaType type = (XmlSchemaType)schemas.Find(at.SchemaTypeName, typeof(XmlSchemaType), false);
  392. AddRef(refs, type);
  393. }
  394. else {
  395. Depends(at.SchemaType, refs);
  396. }
  397. }
  398. if (typeof(XmlSchemaAnnotated).IsAssignableFrom(t)) {
  399. XmlAttribute[] attrs = (XmlAttribute[])((XmlSchemaAnnotated)item).UnhandledAttributes;
  400. if (attrs != null) {
  401. for (int i = 0; i < attrs.Length; i++) {
  402. XmlAttribute attribute = attrs[i];
  403. if (attribute.LocalName == Wsdl.ArrayType && attribute.NamespaceURI == Wsdl.Namespace) {
  404. string dims;
  405. XmlQualifiedName qname = TypeScope.ParseWsdlArrayType(attribute.Value, out dims, item);
  406. XmlSchemaType type = (XmlSchemaType)schemas.Find(qname, typeof(XmlSchemaType), false);
  407. AddRef(refs, type);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }