XmlSchemaImporter.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. //
  2. // System.Xml.Serialization.XmlSchemaImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using System.Collections;
  13. namespace System.Xml.Serialization {
  14. public class XmlSchemaImporter {
  15. #region Fields
  16. XmlSchemas schemas;
  17. CodeIdentifiers typeIdentifiers;
  18. CodeIdentifiers elemIdentifiers = new CodeIdentifiers ();
  19. Hashtable mappedTypes = new Hashtable ();
  20. Hashtable dataMappedTypes = new Hashtable ();
  21. Queue pendingMaps = new Queue ();
  22. Hashtable sharedAnonymousTypes = new Hashtable ();
  23. bool encodedFormat = false;
  24. XmlReflectionImporter auxXmlRefImporter;
  25. SoapReflectionImporter auxSoapRefImporter;
  26. Hashtable forcedBaseTypes;
  27. static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
  28. static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array",XmlSerializer.EncodingNamespace);
  29. static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType",XmlSerializer.EncodingNamespace);
  30. XmlSchemaElement anyElement = null;
  31. class MapFixup
  32. {
  33. public XmlTypeMapping Map;
  34. public XmlSchemaComplexType SchemaType;
  35. public XmlQualifiedName TypeName;
  36. }
  37. #endregion
  38. #region Constructors
  39. public XmlSchemaImporter (XmlSchemas schemas)
  40. {
  41. this.schemas = schemas;
  42. typeIdentifiers = new CodeIdentifiers ();
  43. }
  44. public XmlSchemaImporter (XmlSchemas schemas, CodeIdentifiers typeIdentifiers)
  45. : this (schemas)
  46. {
  47. this.typeIdentifiers = typeIdentifiers;
  48. }
  49. internal bool UseEncodedFormat
  50. {
  51. get { return encodedFormat; }
  52. set { encodedFormat = value; }
  53. }
  54. #endregion // Constructors
  55. #region Methods
  56. [MonoTODO]
  57. public XmlMembersMapping ImportAnyType (XmlQualifiedName typeName, string elementName)
  58. {
  59. throw new NotImplementedException ();
  60. }
  61. public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, Type baseType)
  62. {
  63. return ImportDerivedTypeMapping (name, baseType, true);
  64. }
  65. public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, Type baseType, bool baseTypeCanBeIndirect)
  66. {
  67. XmlQualifiedName qname;
  68. XmlSchemaType stype;
  69. if (encodedFormat)
  70. {
  71. qname = name;
  72. stype = schemas.Find (name, typeof (XmlSchemaComplexType)) as XmlSchemaComplexType;
  73. if (stype == null) throw new InvalidOperationException ("Schema type '" + name + "' not found or not valid");
  74. }
  75. else
  76. {
  77. if (!LocateElement (name, out qname, out stype)) return null;
  78. }
  79. XmlTypeMapping map = GetRegisteredTypeMapping (qname);
  80. if (map != null) return map;
  81. RegisterForcedBaseType (qname, baseType);
  82. map = CreateTypeMapping (qname, SchemaTypes.Class, name);
  83. map.Documentation = GetDocumentation (stype);
  84. RegisterMapFixup (map, qname, (XmlSchemaComplexType)stype);
  85. BuildPendingMaps ();
  86. return map;
  87. }
  88. [MonoTODO]
  89. public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, bool baseTypeCanBeIndirect)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName name)
  94. {
  95. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (name, typeof (XmlSchemaElement));
  96. if (elem == null) throw new InvalidOperationException ("Schema element '" + name + "' not found or not valid");
  97. XmlSchemaComplexType stype;
  98. if (elem.SchemaType != null)
  99. {
  100. stype = elem.SchemaType as XmlSchemaComplexType;
  101. }
  102. else
  103. {
  104. if (elem.SchemaTypeName.IsEmpty) return null;
  105. if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace) return null;
  106. object type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaComplexType));
  107. if (type == null) throw new InvalidOperationException ("Schema type '" + elem.SchemaTypeName + "' not found");
  108. stype = type as XmlSchemaComplexType;
  109. }
  110. if (stype == null)
  111. throw new InvalidOperationException ("Schema element '" + name + "' not found or not valid");
  112. XmlMemberMapping[] mapping = ImportMembersMappingComposite (stype, name);
  113. return new XmlMembersMapping (name.Name, name.Namespace, mapping);
  114. }
  115. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName[] names)
  116. {
  117. XmlMemberMapping[] mapping = new XmlMemberMapping [names.Length];
  118. for (int n=0; n<names.Length; n++)
  119. {
  120. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (names[n], typeof (XmlSchemaElement));
  121. if (elem == null) throw new InvalidOperationException ("Schema element '" + names[n] + "' not found");
  122. XmlQualifiedName typeQName = new XmlQualifiedName ("Message", names[n].Namespace);
  123. TypeData td = GetElementTypeData (typeQName, elem);
  124. mapping[n] = ImportMemberMapping (elem.Name, typeQName.Namespace, td);
  125. }
  126. BuildPendingMaps ();
  127. return new XmlMembersMapping (mapping);
  128. }
  129. public XmlMembersMapping ImportEncodedMembersMapping (string name, string ns, SoapSchemaMember[] members, bool hasWrapperElement)
  130. {
  131. XmlMemberMapping[] mapping = new XmlMemberMapping [members.Length];
  132. for (int n=0; n<members.Length; n++)
  133. {
  134. TypeData td = GetTypeData (members[n].MemberType, null);
  135. mapping[n] = ImportMemberMapping (members[n].MemberName, members[n].MemberType.Namespace, td);
  136. }
  137. BuildPendingMaps ();
  138. return new XmlMembersMapping (name, ns, hasWrapperElement, mapping);
  139. }
  140. public XmlMembersMapping ImportEncodedMembersMapping (string name, string ns, SoapSchemaMember member)
  141. {
  142. XmlSchemaComplexType stype = schemas.Find (member.MemberType, typeof (XmlSchemaComplexType)) as XmlSchemaComplexType;
  143. if (stype == null) throw new InvalidOperationException ("Schema type '" + member.MemberType + "' not found or not valid");
  144. XmlMemberMapping[] mapping = ImportMembersMappingComposite (stype, member.MemberType);
  145. return new XmlMembersMapping (name, ns, mapping);
  146. }
  147. public XmlMemberMapping[] ImportMembersMappingComposite (XmlSchemaComplexType stype, XmlQualifiedName refer)
  148. {
  149. if (stype.Particle == null)
  150. return new XmlMemberMapping [0];
  151. ClassMap cmap = new ClassMap ();
  152. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  153. if (seq == null) throw new InvalidOperationException ("Schema element '" + refer + "' cannot be imported as XmlMembersMapping");
  154. CodeIdentifiers classIds = new CodeIdentifiers ();
  155. ImportParticleComplexContent (refer, cmap, seq, classIds, false);
  156. BuildPendingMaps ();
  157. int n = 0;
  158. XmlMemberMapping[] mapping = new XmlMemberMapping [cmap.AllMembers.Count];
  159. foreach (XmlTypeMapMember mapMem in cmap.AllMembers)
  160. mapping[n++] = new XmlMemberMapping (mapMem.Name, refer.Namespace, mapMem, encodedFormat);
  161. return mapping;
  162. }
  163. XmlMemberMapping ImportMemberMapping (string name, string ns, TypeData type)
  164. {
  165. XmlTypeMapMemberElement mapMem = new XmlTypeMapMemberElement ();
  166. mapMem.Name = name;
  167. mapMem.TypeData = type;
  168. mapMem.ElementInfo.Add (CreateElementInfo (ns, mapMem, name, type, true));
  169. return new XmlMemberMapping (name, ns, mapMem, encodedFormat);
  170. }
  171. [MonoTODO]
  172. public XmlMembersMapping ImportMembersMapping (XmlQualifiedName[] names, Type baseType, bool baseTypeCanBeIndirect)
  173. {
  174. throw new NotImplementedException ();
  175. }
  176. public XmlTypeMapping ImportTypeMapping (XmlQualifiedName name)
  177. {
  178. XmlQualifiedName qname;
  179. XmlSchemaType stype;
  180. if (!LocateElement (name, out qname, out stype)) return null;
  181. XmlTypeMapping map = GetRegisteredTypeMapping (qname);
  182. if (map != null) return map;
  183. map = CreateTypeMapping (qname, SchemaTypes.Class, name);
  184. map.Documentation = GetDocumentation (stype);
  185. RegisterMapFixup (map, qname, (XmlSchemaComplexType)stype);
  186. BuildPendingMaps ();
  187. return map;
  188. }
  189. bool LocateElement (XmlQualifiedName name, out XmlQualifiedName qname, out XmlSchemaType stype)
  190. {
  191. qname = null;
  192. stype = null;
  193. XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (name, typeof (XmlSchemaElement));
  194. if (elem == null) return false;
  195. // The root element must be an element with complex type
  196. if (elem.SchemaType != null)
  197. {
  198. stype = elem.SchemaType;
  199. qname = name;
  200. }
  201. else
  202. {
  203. if (elem.SchemaTypeName.IsEmpty) return false;
  204. if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace) return false;
  205. object type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaComplexType));
  206. if (type == null) type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaSimpleType));
  207. if (type == null) throw new InvalidOperationException ("Schema type '" + elem.SchemaTypeName + "' not found");
  208. stype = (XmlSchemaType) type;
  209. qname = stype.QualifiedName;
  210. }
  211. if (stype is XmlSchemaSimpleType) return false;
  212. return true;
  213. }
  214. public XmlTypeMapping ImportType (XmlQualifiedName name, XmlQualifiedName root)
  215. {
  216. XmlTypeMapping map = GetRegisteredTypeMapping (name);
  217. if (map != null) return map;
  218. XmlSchemaType type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaComplexType));
  219. if (type == null) type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaSimpleType));
  220. if (type == null)
  221. {
  222. if (name.Namespace == XmlSerializer.EncodingNamespace)
  223. throw new InvalidOperationException ("Referenced type '" + name + "' valid only for encoded SOAP");
  224. else
  225. throw new InvalidOperationException ("Referenced type '" + name + "' not found");
  226. }
  227. return ImportType (name, type, root);
  228. }
  229. XmlTypeMapping ImportType (XmlQualifiedName name, XmlSchemaType stype, XmlQualifiedName root)
  230. {
  231. XmlTypeMapping map = GetRegisteredTypeMapping (name);
  232. if (map != null) return map;
  233. if (stype is XmlSchemaComplexType)
  234. return ImportClassComplexType (name, (XmlSchemaComplexType) stype, root);
  235. else if (stype is XmlSchemaSimpleType)
  236. return ImportClassSimpleType (name, (XmlSchemaSimpleType) stype, root);
  237. throw new NotSupportedException ("Schema type not supported: " + stype.GetType ());
  238. }
  239. XmlTypeMapping ImportClassComplexType (XmlQualifiedName typeQName, XmlSchemaComplexType stype, XmlQualifiedName root)
  240. {
  241. XmlTypeMapping map;
  242. // The need for fixups: If the complex type is an array, then to get the type of the
  243. // array we need first to get the type of the items of the array.
  244. // But if one of the item types or its children has a referece to this type array,
  245. // then we enter in an infinite loop. This does not happen with class types because
  246. // the class map is registered before parsing the children. We can't do the same
  247. // with the array type because to register the array map we need the type of the array.
  248. if (CanBeArray (typeQName, stype))
  249. {
  250. TypeData typeData;
  251. ListMap listMap = BuildArrayMap (typeQName, stype, out typeData);
  252. if (listMap != null)
  253. {
  254. map = CreateArrayTypeMapping (typeQName, typeData);
  255. map.ObjectMap = listMap;
  256. return map;
  257. }
  258. // After all, it is not an array. Create a class map then.
  259. }
  260. else if (CanBeAnyElement (stype))
  261. {
  262. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlElement)));
  263. }
  264. else if (CanBeIXmlSerializable (stype))
  265. {
  266. return GetTypeMapping (TypeTranslator.GetTypeData(typeof(object)));
  267. }
  268. // Register the map right now but do not build it,
  269. // This will avoid loops.
  270. map = CreateTypeMapping (typeQName, SchemaTypes.Class, root);
  271. map.Documentation = GetDocumentation (stype);
  272. RegisterMapFixup (map, typeQName, stype);
  273. return map;
  274. }
  275. void RegisterMapFixup (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  276. {
  277. MapFixup fixup = new MapFixup ();
  278. fixup.Map = map;
  279. fixup.SchemaType = stype;
  280. fixup.TypeName = typeQName;
  281. pendingMaps.Enqueue (fixup);
  282. }
  283. void BuildPendingMaps ()
  284. {
  285. while (pendingMaps.Count > 0) {
  286. MapFixup fixup = (MapFixup) pendingMaps.Dequeue ();
  287. if (fixup.Map.ObjectMap == null) {
  288. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  289. if (fixup.Map.ObjectMap == null) pendingMaps.Enqueue (fixup);
  290. }
  291. }
  292. }
  293. void BuildPendingMap (XmlTypeMapping map)
  294. {
  295. if (map.ObjectMap != null) return;
  296. foreach (MapFixup fixup in pendingMaps)
  297. {
  298. if (fixup.Map == map) {
  299. BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
  300. return;
  301. }
  302. }
  303. throw new InvalidOperationException ("Can't complete map of type " + map.XmlType + " : " + map.Namespace);
  304. }
  305. void BuildClassMap (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  306. {
  307. CodeIdentifiers classIds = new CodeIdentifiers();
  308. classIds.AddReserved (map.TypeData.TypeName);
  309. ClassMap cmap = new ClassMap ();
  310. map.ObjectMap = cmap;
  311. bool isMixed = stype.IsMixed;
  312. if (stype.Particle != null)
  313. {
  314. if (HasForcedBaseType (typeQName))
  315. ImportForcedDerivedType (map, typeQName, ref isMixed);
  316. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, isMixed);
  317. }
  318. else
  319. {
  320. if (stype.ContentModel is XmlSchemaSimpleContent) {
  321. ImportSimpleContent (typeQName, map, (XmlSchemaSimpleContent)stype.ContentModel, classIds, isMixed);
  322. }
  323. else if (stype.ContentModel is XmlSchemaComplexContent) {
  324. ImportComplexContent (typeQName, map, (XmlSchemaComplexContent)stype.ContentModel, classIds, isMixed);
  325. }
  326. }
  327. ImportAttributes (typeQName, cmap, stype.Attributes, stype.AnyAttribute, classIds);
  328. ImportExtensionTypes (typeQName);
  329. }
  330. void ImportAttributes (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat, CodeIdentifiers classIds)
  331. {
  332. if (anyat != null)
  333. {
  334. XmlTypeMapMemberAnyAttribute member = new XmlTypeMapMemberAnyAttribute ();
  335. member.Name = classIds.AddUnique ("AnyAttribute", member);
  336. member.TypeData = TypeTranslator.GetTypeData (typeof(XmlAttribute[]));
  337. cmap.AddMember (member);
  338. }
  339. foreach (XmlSchemaObject at in atts)
  340. {
  341. if (at is XmlSchemaAttribute)
  342. {
  343. string ns;
  344. XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
  345. XmlSchemaAttribute refAttr = GetRefAttribute (typeQName, attr, out ns);
  346. XmlTypeMapMemberAttribute member = new XmlTypeMapMemberAttribute ();
  347. member.Name = classIds.AddUnique (CodeIdentifier.MakeValid (refAttr.Name), member);
  348. member.Documentation = GetDocumentation (attr);
  349. member.AttributeName = refAttr.Name;
  350. member.Namespace = ns;
  351. member.Form = refAttr.Form;
  352. member.TypeData = GetAttributeTypeData (typeQName, attr);
  353. if (refAttr.DefaultValue != null) member.DefaultValue = XmlCustomFormatter.FromXmlString (member.TypeData, refAttr.DefaultValue);
  354. if (member.TypeData.IsComplexType)
  355. member.MappedType = GetTypeMapping (member.TypeData);
  356. cmap.AddMember (member);
  357. }
  358. else if (at is XmlSchemaAttributeGroupRef)
  359. {
  360. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
  361. XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
  362. ImportAttributes (typeQName, cmap, grp.Attributes, grp.AnyAttribute, classIds);
  363. }
  364. }
  365. }
  366. ListMap BuildArrayMap (XmlQualifiedName typeQName, XmlSchemaComplexType stype, out TypeData arrayTypeData)
  367. {
  368. if (encodedFormat)
  369. {
  370. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  371. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  372. XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
  373. XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
  374. if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  375. XmlAttribute xat = null;
  376. foreach (XmlAttribute at in uatts)
  377. if (at.LocalName == "arrayType" && at.NamespaceURI == XmlSerializer.WsdlNamespace)
  378. { xat = at; break; }
  379. if (xat == null)
  380. throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName);
  381. string name, ns, dims;
  382. TypeTranslator.ParseArrayType (xat.Value, out name, out ns, out dims);
  383. return BuildEncodedArrayMap (name + dims, ns, out arrayTypeData);
  384. }
  385. else
  386. {
  387. ClassMap cmap = new ClassMap ();
  388. CodeIdentifiers classIds = new CodeIdentifiers();
  389. ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, false);
  390. XmlTypeMapMemberFlatList list = (cmap.AllMembers.Count == 1) ? cmap.AllMembers[0] as XmlTypeMapMemberFlatList : null;
  391. if (list != null && list.ChoiceMember == null)
  392. {
  393. arrayTypeData = list.TypeData;
  394. return list.ListMap;
  395. }
  396. else
  397. {
  398. arrayTypeData = null;
  399. return null;
  400. }
  401. }
  402. }
  403. ListMap BuildEncodedArrayMap (string type, string ns, out TypeData arrayTypeData)
  404. {
  405. ListMap map = new ListMap ();
  406. int i = type.LastIndexOf ("[");
  407. if (i == -1) throw new InvalidOperationException ("Invalid arrayType value: " + type);
  408. if (type.IndexOf (",",i) != -1) throw new InvalidOperationException ("Multidimensional arrays are not supported");
  409. string itemType = type.Substring (0,i);
  410. TypeData itemTypeData;
  411. if (itemType.IndexOf ("[") != -1)
  412. {
  413. ListMap innerListMap = BuildEncodedArrayMap (itemType, ns, out itemTypeData);
  414. int dims = itemType.Split ('[').Length - 1;
  415. string name = TypeTranslator.GetArrayName (type, dims);
  416. XmlQualifiedName qname = new XmlQualifiedName (name, ns);
  417. XmlTypeMapping tmap = CreateArrayTypeMapping (qname, itemTypeData);
  418. tmap.ObjectMap = innerListMap;
  419. }
  420. else
  421. {
  422. itemTypeData = GetTypeData (new XmlQualifiedName (itemType, ns), null);
  423. }
  424. arrayTypeData = itemTypeData.ListTypeData;
  425. map.ItemInfo = new XmlTypeMapElementInfoList();
  426. map.ItemInfo.Add (CreateElementInfo ("", null, "Item", itemTypeData, true));
  427. return map;
  428. }
  429. XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
  430. {
  431. foreach (object ob in atts)
  432. {
  433. XmlSchemaAttribute att = ob as XmlSchemaAttribute;
  434. if (att != null && att.RefName == arrayTypeRefName) return att;
  435. XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
  436. if (gref != null)
  437. {
  438. XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
  439. att = FindArrayAttribute (grp.Attributes);
  440. if (att != null) return att;
  441. }
  442. }
  443. return null;
  444. }
  445. void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed)
  446. {
  447. ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed);
  448. if (isMixed)
  449. {
  450. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  451. member.Name = classIds.AddUnique ("Text", member);
  452. member.TypeData = TypeTranslator.GetTypeData (typeof(string[]));
  453. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData));
  454. member.IsXmlTextCollector = true;
  455. member.ListMap = new ListMap ();
  456. member.ListMap.ItemInfo = member.ElementInfo;
  457. cmap.AddMember (member);
  458. }
  459. }
  460. void ImportParticleContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  461. {
  462. if (particle is XmlSchemaGroupRef)
  463. particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
  464. if (particle.MaxOccurs > 1) multiValue = true;
  465. if (particle is XmlSchemaSequence) {
  466. ImportSequenceContent (typeQName, cmap, ((XmlSchemaSequence)particle).Items, classIds, multiValue, ref isMixed);
  467. }
  468. else if (particle is XmlSchemaChoice) {
  469. if (((XmlSchemaChoice)particle).Items.Count == 1)
  470. ImportSequenceContent (typeQName, cmap, ((XmlSchemaChoice)particle).Items, classIds, multiValue, ref isMixed);
  471. else
  472. ImportChoiceContent (typeQName, cmap, (XmlSchemaChoice)particle, classIds, multiValue);
  473. }
  474. else if (particle is XmlSchemaAll) {
  475. ImportSequenceContent (typeQName, cmap, ((XmlSchemaAll)particle).Items, classIds, multiValue, ref isMixed);
  476. }
  477. }
  478. void ImportSequenceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection items, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
  479. {
  480. foreach (XmlSchemaObject item in items)
  481. {
  482. XmlTypeMapMember mapMember;
  483. if (item is XmlSchemaElement)
  484. {
  485. string ns;
  486. XmlSchemaElement elem = (XmlSchemaElement) item;
  487. TypeData typeData = GetElementTypeData (typeQName, elem);
  488. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  489. if (elem.MaxOccurs == 1 && !multiValue)
  490. {
  491. XmlTypeMapMemberElement member = null;
  492. if (typeData.SchemaType != SchemaTypes.Array)
  493. {
  494. member = new XmlTypeMapMemberElement ();
  495. if (refElem.DefaultValue != null) member.DefaultValue = XmlCustomFormatter.FromXmlString (typeData, refElem.DefaultValue);
  496. }
  497. else if (GetTypeMapping (typeData).IsSimpleType)
  498. {
  499. // It is a simple list (space separated list).
  500. // Since this is not supported, map as a single item value
  501. // TODO: improve this
  502. member = new XmlTypeMapMemberElement ();
  503. typeData = typeData.ListItemTypeData;
  504. }
  505. else
  506. member = new XmlTypeMapMemberList ();
  507. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  508. member.Documentation = GetDocumentation (elem);
  509. member.TypeData = typeData;
  510. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
  511. cmap.AddMember (member);
  512. }
  513. else
  514. {
  515. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  516. member.ListMap = new ListMap ();
  517. member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
  518. member.Documentation = GetDocumentation (elem);
  519. member.TypeData = typeData.ListTypeData;
  520. member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
  521. member.ListMap.ItemInfo = member.ElementInfo;
  522. cmap.AddMember (member);
  523. }
  524. }
  525. else if (item is XmlSchemaAny)
  526. {
  527. XmlSchemaAny elem = (XmlSchemaAny) item;
  528. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement ();
  529. member.Name = classIds.AddUnique ("Any", member);
  530. member.Documentation = GetDocumentation (elem);
  531. Type ctype;
  532. if (elem.MaxOccurs > 1 || multiValue)
  533. ctype = isMixed ? typeof(XmlNode[]) : typeof(XmlElement[]);
  534. else
  535. ctype = isMixed ? typeof(XmlNode) : typeof(XmlElement);
  536. member.TypeData = TypeTranslator.GetTypeData (ctype);
  537. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, member.TypeData);
  538. einfo.IsUnnamedAnyElement = true;
  539. member.ElementInfo.Add (einfo);
  540. if (isMixed)
  541. {
  542. einfo = CreateTextElementInfo (typeQName.Namespace, member, member.TypeData);
  543. member.ElementInfo.Add (einfo);
  544. member.IsXmlTextCollector = true;
  545. isMixed = false; //Allow only one XmlTextAttribute
  546. }
  547. cmap.AddMember (member);
  548. }
  549. else if (item is XmlSchemaParticle) {
  550. ImportParticleContent (typeQName, cmap, (XmlSchemaParticle)item, classIds, multiValue, ref isMixed);
  551. }
  552. }
  553. }
  554. void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
  555. {
  556. XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
  557. multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
  558. if (choices.Count == 0) return;
  559. if (choice.MaxOccurs > 1) multiValue = true;
  560. XmlTypeMapMemberElement member;
  561. if (multiValue)
  562. {
  563. member = new XmlTypeMapMemberFlatList ();
  564. member.Name = classIds.AddUnique ("Items", member);
  565. ListMap listMap = new ListMap ();
  566. listMap.ItemInfo = choices;
  567. ((XmlTypeMapMemberFlatList)member).ListMap = listMap;
  568. }
  569. else
  570. {
  571. member = new XmlTypeMapMemberElement ();
  572. member.Name = classIds.AddUnique ("Item", member);
  573. }
  574. // If all choices have the same type, use that type for the member.
  575. // If not use System.Object.
  576. // If there are at least two choices with the same type, use a choice
  577. // identifier attribute
  578. TypeData typeData = null;
  579. bool twoEqual = false;
  580. bool allEqual = true;
  581. Hashtable types = new Hashtable ();
  582. foreach (XmlTypeMapElementInfo einfo in choices)
  583. {
  584. if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
  585. else types.Add (einfo.TypeData, einfo);
  586. TypeData choiceType = einfo.TypeData;
  587. if (choiceType.SchemaType == SchemaTypes.Class)
  588. {
  589. // When comparing class types, use the most generic class in the
  590. // inheritance hierarchy
  591. XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
  592. BuildPendingMap (choiceMap);
  593. while (choiceMap.BaseMap != null) {
  594. choiceMap = choiceMap.BaseMap;
  595. BuildPendingMap (choiceMap);
  596. choiceType = choiceMap.TypeData;
  597. }
  598. }
  599. if (typeData == null) typeData = choiceType;
  600. else if (typeData != choiceType) allEqual = false;
  601. }
  602. if (!allEqual)
  603. typeData = TypeTranslator.GetTypeData (typeof(object));
  604. if (twoEqual)
  605. {
  606. // Create the choice member
  607. XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
  608. choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
  609. member.ChoiceMember = choiceMember.Name;
  610. // Create the choice enum
  611. XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
  612. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  613. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
  614. for (int n=0; n<choices.Count; n++)
  615. {
  616. XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
  617. string xmlName = (it.Namespace != null && it.Namespace != "") ? it.Namespace + ":" + it.ElementName : it.ElementName;
  618. string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
  619. members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
  620. }
  621. enumMap.ObjectMap = new EnumMap (members, false);
  622. choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
  623. choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false));
  624. cmap.AddMember (choiceMember);
  625. }
  626. if (multiValue)
  627. typeData = typeData.ListTypeData;
  628. member.ElementInfo = choices;
  629. member.Documentation = GetDocumentation (choice);
  630. member.TypeData = typeData;
  631. cmap.AddMember (member);
  632. }
  633. bool ImportChoices (XmlQualifiedName typeQName, XmlTypeMapMember member, XmlTypeMapElementInfoList choices, XmlSchemaObjectCollection items)
  634. {
  635. bool multiValue = false;
  636. foreach (XmlSchemaObject titem in items)
  637. {
  638. XmlSchemaObject item = titem;
  639. if (item is XmlSchemaGroupRef)
  640. item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
  641. if (item is XmlSchemaElement)
  642. {
  643. string ns;
  644. XmlSchemaElement elem = (XmlSchemaElement) item;
  645. TypeData typeData = GetElementTypeData (typeQName, elem);
  646. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  647. choices.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
  648. if (elem.MaxOccurs > 1) multiValue = true;
  649. }
  650. else if (item is XmlSchemaAny)
  651. {
  652. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  653. einfo.IsUnnamedAnyElement = true;
  654. choices.Add (einfo);
  655. }
  656. else if (item is XmlSchemaChoice) {
  657. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaChoice)item).Items) || multiValue;
  658. }
  659. else if (item is XmlSchemaSequence) {
  660. multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaSequence)item).Items) || multiValue;
  661. }
  662. }
  663. return multiValue;
  664. }
  665. void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
  666. {
  667. ClassMap cmap = (ClassMap)map.ObjectMap;
  668. XmlQualifiedName qname = GetContentBaseType (content.Content);
  669. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  670. member.Name = classIds.AddUnique("Value", member);
  671. member.TypeData = FindBuiltInType (qname);
  672. member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
  673. member.IsXmlTextCollector = true;
  674. cmap.AddMember (member);
  675. XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
  676. if (ext != null)
  677. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  678. }
  679. TypeData FindBuiltInType (XmlQualifiedName qname)
  680. {
  681. if (qname.Namespace == XmlSchema.Namespace)
  682. return TypeTranslator.GetPrimitiveTypeData (qname.Name);
  683. XmlSchemaComplexType ct = (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
  684. if (ct != null)
  685. {
  686. XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
  687. if (sc == null) throw new InvalidOperationException ("Invalid schema");
  688. return FindBuiltInType (GetContentBaseType (sc.Content));
  689. }
  690. XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
  691. if (st != null)
  692. return FindBuiltInType (qname, st);
  693. throw new InvalidOperationException ("Definition of type " + qname + " not found");
  694. }
  695. TypeData FindBuiltInType (XmlQualifiedName qname, XmlSchemaSimpleType st)
  696. {
  697. if (CanBeEnum (st))
  698. return ImportType (qname, null).TypeData;
  699. if (st.Content is XmlSchemaSimpleTypeRestriction) {
  700. return FindBuiltInType (GetContentBaseType (st.Content));
  701. }
  702. else if (st.Content is XmlSchemaSimpleTypeList) {
  703. return FindBuiltInType (GetContentBaseType (st.Content)).ListTypeData;
  704. }
  705. else if (st.Content is XmlSchemaSimpleTypeUnion)
  706. {
  707. // Check if all types of the union are equal. If not, then will use anyType.
  708. XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
  709. TypeData utype = null;
  710. // Anonymous types are unique
  711. if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
  712. return FindBuiltInType (anyType);
  713. foreach (XmlQualifiedName mt in uni.MemberTypes)
  714. {
  715. TypeData qn = FindBuiltInType (mt);
  716. if (utype != null && qn != utype) return FindBuiltInType (anyType);
  717. else utype = qn;
  718. }
  719. return utype;
  720. }
  721. else
  722. return null;
  723. }
  724. XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
  725. {
  726. if (ob is XmlSchemaSimpleContentExtension)
  727. return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
  728. else if (ob is XmlSchemaSimpleContentRestriction)
  729. return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
  730. else if (ob is XmlSchemaSimpleTypeRestriction)
  731. return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
  732. else if (ob is XmlSchemaSimpleTypeList)
  733. return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
  734. else
  735. return null;
  736. }
  737. void ImportComplexContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaComplexContent content, CodeIdentifiers classIds, bool isMixed)
  738. {
  739. ClassMap cmap = (ClassMap)map.ObjectMap;
  740. XmlQualifiedName qname;
  741. XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
  742. if (ext != null) qname = ext.BaseTypeName;
  743. else qname = ((XmlSchemaComplexContentRestriction)content.Content).BaseTypeName;
  744. if (HasForcedBaseType (typeQName))
  745. RegisterForcedBaseType (qname, GetForcedBaseType (typeQName));
  746. // Add base map members to this map
  747. XmlTypeMapping baseMap = ImportType (qname, null);
  748. BuildPendingMap (baseMap);
  749. ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
  750. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  751. cmap.AddMember (member);
  752. if (baseClassMap.XmlTextCollector != null) isMixed = false;
  753. else if (content.IsMixed) isMixed = true;
  754. map.BaseMap = baseMap;
  755. baseMap.DerivedTypes.Add (map);
  756. if (ext != null) {
  757. // Add the members of this map
  758. if (ext.Particle != null)
  759. ImportParticleComplexContent (typeQName, cmap, ext.Particle, classIds, isMixed);
  760. ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
  761. }
  762. else {
  763. if (isMixed) ImportParticleComplexContent (typeQName, cmap, null, classIds, true);
  764. }
  765. }
  766. public void ImportForcedDerivedType (XmlTypeMapping map, XmlQualifiedName qname, ref bool isMixed)
  767. {
  768. ClassMap cmap = (ClassMap)map.ObjectMap;
  769. XmlTypeMapping baseMap;
  770. Type baseType = GetForcedBaseType (qname);
  771. if (encodedFormat)
  772. {
  773. if (auxXmlRefImporter == null) auxXmlRefImporter = new XmlReflectionImporter ();
  774. baseMap = auxXmlRefImporter.ImportTypeMapping (baseType);
  775. }
  776. else
  777. {
  778. if (auxSoapRefImporter == null) auxSoapRefImporter = new SoapReflectionImporter ();
  779. baseMap = auxSoapRefImporter.ImportTypeMapping (baseType);
  780. }
  781. ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
  782. foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
  783. cmap.AddMember (member);
  784. if (baseClassMap.XmlTextCollector != null) isMixed = false;
  785. map.BaseMap = baseMap;
  786. baseMap.DerivedTypes.Add (map);
  787. }
  788. void ImportExtensionTypes (XmlQualifiedName qname)
  789. {
  790. foreach (XmlSchema schema in schemas) {
  791. foreach (XmlSchemaObject sob in schema.Items)
  792. {
  793. XmlSchemaComplexType sct = sob as XmlSchemaComplexType;
  794. if (sct != null && sct.ContentModel is XmlSchemaComplexContent) {
  795. XmlQualifiedName exqname;
  796. XmlSchemaComplexContentExtension ext = sct.ContentModel.Content as XmlSchemaComplexContentExtension;
  797. if (ext != null) exqname = ext.BaseTypeName;
  798. else exqname = ((XmlSchemaComplexContentRestriction)sct.ContentModel.Content).BaseTypeName;
  799. if (exqname == qname)
  800. ImportType (new XmlQualifiedName (sct.Name, schema.TargetNamespace), sct, null);
  801. }
  802. }
  803. }
  804. }
  805. XmlTypeMapping ImportClassSimpleType (XmlQualifiedName typeQName, XmlSchemaSimpleType stype, XmlQualifiedName root)
  806. {
  807. if (CanBeEnum (stype))
  808. {
  809. // Create an enum map
  810. CodeIdentifiers codeIdents = new CodeIdentifiers ();
  811. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  812. XmlTypeMapping enumMap = CreateTypeMapping (typeQName, SchemaTypes.Enum, null);
  813. enumMap.Documentation = GetDocumentation (stype);
  814. codeIdents.AddReserved (enumMap.TypeData.TypeName);
  815. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [rest.Facets.Count];
  816. for (int n=0; n<rest.Facets.Count; n++)
  817. {
  818. XmlSchemaEnumerationFacet enu = (XmlSchemaEnumerationFacet) rest.Facets[n];
  819. string enumName = codeIdents.AddUnique(CodeIdentifier.MakeValid (enu.Value), enu);
  820. members [n] = new EnumMap.EnumMapMember (enu.Value, enumName);
  821. members [n].Documentation = GetDocumentation (enu);
  822. }
  823. enumMap.ObjectMap = new EnumMap (members, false);
  824. enumMap.IsSimpleType = true;
  825. return enumMap;
  826. }
  827. if (stype.Content is XmlSchemaSimpleTypeList)
  828. {
  829. XmlSchemaSimpleTypeList slist = (XmlSchemaSimpleTypeList)stype.Content;
  830. TypeData arrayTypeData = FindBuiltInType (slist.ItemTypeName, stype);
  831. ListMap listMap = new ListMap ();
  832. listMap.ItemInfo = new XmlTypeMapElementInfoList ();
  833. listMap.ItemInfo.Add (CreateElementInfo (typeQName.Namespace, null, "Item", arrayTypeData.ListItemTypeData, false));
  834. XmlTypeMapping map = CreateArrayTypeMapping (typeQName, arrayTypeData);
  835. map.ObjectMap = listMap;
  836. map.IsSimpleType = true;
  837. return map;
  838. }
  839. // It is an extension of a primitive or known type
  840. TypeData typeData = FindBuiltInType (typeQName, stype);
  841. return GetTypeMapping (typeData);
  842. }
  843. bool CanBeEnum (XmlSchemaSimpleType stype)
  844. {
  845. if (stype.Content is XmlSchemaSimpleTypeRestriction)
  846. {
  847. XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
  848. foreach (object ob in rest.Facets)
  849. if (!(ob is XmlSchemaEnumerationFacet)) return false;
  850. return true;
  851. }
  852. return false;
  853. }
  854. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaComplexType stype)
  855. {
  856. if (encodedFormat)
  857. {
  858. XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent;
  859. if (content == null) return false;
  860. XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction;
  861. if (rest == null) return false;
  862. return rest.BaseTypeName == arrayType;
  863. }
  864. else
  865. {
  866. if (stype.Attributes.Count > 0 || stype.AnyAttribute != null) return false;
  867. else return !stype.IsMixed && CanBeArray (typeQName, stype.Particle, false);
  868. }
  869. }
  870. bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaParticle particle, bool multiValue)
  871. {
  872. // To be an array, there can't be a direct child of type typeQName
  873. if (particle == null) return false;
  874. multiValue = multiValue || particle.MaxOccurs > 1;
  875. if (particle is XmlSchemaGroupRef)
  876. return CanBeArray (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), multiValue);
  877. if (particle is XmlSchemaElement)
  878. {
  879. XmlSchemaElement elem = (XmlSchemaElement)particle;
  880. if (!elem.RefName.IsEmpty)
  881. return CanBeArray (typeQName, FindRefElement (elem), multiValue);
  882. else
  883. return multiValue && !typeQName.Equals (((XmlSchemaElement)particle).SchemaTypeName);
  884. }
  885. if (particle is XmlSchemaAny)
  886. return multiValue;
  887. if (particle is XmlSchemaSequence)
  888. {
  889. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  890. if (seq.Items.Count != 1) return false;
  891. return CanBeArray (typeQName, (XmlSchemaParticle)seq.Items[0], multiValue);
  892. }
  893. if (particle is XmlSchemaChoice)
  894. {
  895. // Can be array if all choices have different types
  896. ArrayList types = new ArrayList ();
  897. if(!CheckChoiceType (typeQName, particle, types, ref multiValue)) return false;
  898. return multiValue;
  899. }
  900. return false;
  901. }
  902. bool CheckChoiceType (XmlQualifiedName typeQName, XmlSchemaParticle particle, ArrayList types, ref bool multiValue)
  903. {
  904. XmlQualifiedName type = null;
  905. multiValue = multiValue || particle.MaxOccurs > 1;
  906. if (particle is XmlSchemaGroupRef)
  907. return CheckChoiceType (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), types, ref multiValue);
  908. if (particle is XmlSchemaElement) {
  909. string ns;
  910. XmlSchemaElement elem = (XmlSchemaElement)particle;
  911. XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
  912. if (refElem.SchemaType != null) return true;
  913. type = refElem.SchemaTypeName;
  914. }
  915. else if (particle is XmlSchemaAny) {
  916. type = anyType;
  917. }
  918. else if (particle is XmlSchemaSequence)
  919. {
  920. XmlSchemaSequence seq = particle as XmlSchemaSequence;
  921. foreach (XmlSchemaParticle par in seq.Items)
  922. if (!CheckChoiceType (typeQName, par, types, ref multiValue)) return false;
  923. return true;
  924. }
  925. else if (particle is XmlSchemaChoice)
  926. {
  927. foreach (XmlSchemaParticle choice in ((XmlSchemaChoice)particle).Items)
  928. if (!CheckChoiceType (typeQName, choice, types, ref multiValue)) return false;
  929. return true;
  930. }
  931. if (typeQName.Equals (type)) return false;
  932. // For primitive types, compare using CLR types, since several
  933. // xml types can be mapped to a single CLR type
  934. string t;
  935. if (type.Namespace == XmlSchema.Namespace)
  936. t = TypeTranslator.GetPrimitiveTypeData (type.Name).FullTypeName + ":" + type.Namespace;
  937. else
  938. t = type.Name + ":" + type.Namespace;
  939. if (types.Contains (t)) return false;
  940. types.Add (t);
  941. return true;
  942. }
  943. bool CanBeAnyElement (XmlSchemaComplexType stype)
  944. {
  945. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  946. return (seq != null) && (seq.Items.Count == 1) && (seq.Items[0] is XmlSchemaAny);
  947. }
  948. bool CanBeIXmlSerializable (XmlSchemaComplexType stype)
  949. {
  950. XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
  951. if (seq == null) return false;
  952. if (seq.Items.Count != 2) return false;
  953. XmlSchemaElement elem = seq.Items[0] as XmlSchemaElement;
  954. if (elem == null) return false;
  955. if (elem.RefName != new XmlQualifiedName ("schema",XmlSchema.Namespace)) return false;
  956. return (seq.Items[1] is XmlSchemaAny);
  957. }
  958. void RegisterForcedBaseType (XmlQualifiedName tname, Type type)
  959. {
  960. if (forcedBaseTypes == null) forcedBaseTypes = new Hashtable ();
  961. forcedBaseTypes [tname] = type;
  962. }
  963. bool HasForcedBaseType (XmlQualifiedName tname)
  964. {
  965. if (forcedBaseTypes == null) return false;
  966. return forcedBaseTypes.ContainsKey (tname);
  967. }
  968. Type GetForcedBaseType (XmlQualifiedName tname)
  969. {
  970. if (forcedBaseTypes == null) return null;
  971. return (Type) forcedBaseTypes [tname];
  972. }
  973. XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable)
  974. {
  975. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  976. einfo.ElementName = name;
  977. einfo.Namespace = ns;
  978. einfo.IsNullable = isNillable;
  979. if (einfo.TypeData.IsComplexType)
  980. einfo.MappedType = GetTypeMapping (typeData);
  981. return einfo;
  982. }
  983. XmlTypeMapElementInfo CreateTextElementInfo (string ns, XmlTypeMapMember member, TypeData typeData)
  984. {
  985. XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
  986. einfo.IsTextElement = true;
  987. einfo.WrappedElement = false;
  988. if (typeData.IsComplexType)
  989. einfo.MappedType = GetTypeMapping (typeData);
  990. return einfo;
  991. }
  992. XmlTypeMapping CreateTypeMapping (XmlQualifiedName typeQName, SchemaTypes schemaType, XmlQualifiedName root)
  993. {
  994. string typeName = CodeIdentifier.MakeValid (typeQName.Name);
  995. typeName = typeIdentifiers.AddUnique (typeName, null);
  996. TypeData typeData = new TypeData (typeName, typeName, typeName, schemaType, null);
  997. XmlQualifiedName elemName = (root != null) ? root : typeQName;
  998. XmlTypeMapping map = new XmlTypeMapping (elemName.Name, elemName.Namespace, typeData, typeQName.Name, typeQName.Namespace);
  999. map.IncludeInSchema = true;
  1000. mappedTypes [typeQName] = map;
  1001. dataMappedTypes [typeData] = map;
  1002. return map;
  1003. }
  1004. XmlTypeMapping CreateArrayTypeMapping (XmlQualifiedName typeQName, TypeData arrayTypeData)
  1005. {
  1006. XmlTypeMapping map;
  1007. if (encodedFormat) map = new XmlTypeMapping ("Array", XmlSerializer.EncodingNamespace, arrayTypeData, "Array", XmlSerializer.EncodingNamespace);
  1008. else map = new XmlTypeMapping (arrayTypeData.XmlType, typeQName.Namespace, arrayTypeData, arrayTypeData.XmlType, typeQName.Namespace);
  1009. map.IncludeInSchema = true;
  1010. mappedTypes [typeQName] = map;
  1011. dataMappedTypes [arrayTypeData] = map;
  1012. return map;
  1013. }
  1014. XmlSchemaElement GetRefElement (XmlQualifiedName typeQName, XmlSchemaElement elem, out string ns)
  1015. {
  1016. if (!elem.RefName.IsEmpty)
  1017. {
  1018. ns = elem.RefName.Namespace;
  1019. return FindRefElement (elem);
  1020. }
  1021. else
  1022. {
  1023. ns = typeQName.Namespace;
  1024. return elem;
  1025. }
  1026. }
  1027. XmlSchemaAttribute GetRefAttribute (XmlQualifiedName typeQName, XmlSchemaAttribute attr, out string ns)
  1028. {
  1029. if (!attr.RefName.IsEmpty)
  1030. {
  1031. ns = attr.RefName.Namespace;
  1032. return (XmlSchemaAttribute) schemas.Find (attr.RefName, typeof(XmlSchemaAttribute));
  1033. }
  1034. else
  1035. {
  1036. ns = typeQName.Namespace;
  1037. return attr;
  1038. }
  1039. }
  1040. TypeData GetElementTypeData (XmlQualifiedName typeQName, XmlSchemaElement elem)
  1041. {
  1042. bool sharedAnnType = false;
  1043. XmlQualifiedName root = null;
  1044. if (!elem.RefName.IsEmpty) {
  1045. XmlSchemaElement refElem = FindRefElement (elem);
  1046. if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
  1047. root = elem.RefName;
  1048. elem = refElem;
  1049. sharedAnnType = true;
  1050. }
  1051. if (!elem.SchemaTypeName.IsEmpty) return GetTypeData (elem.SchemaTypeName, root);
  1052. else if (elem.SchemaType == null) return TypeTranslator.GetTypeData (typeof(object));
  1053. else return GetTypeData (elem.SchemaType, typeQName, elem.Name, sharedAnnType, root);
  1054. }
  1055. TypeData GetAttributeTypeData (XmlQualifiedName typeQName, XmlSchemaAttribute attr)
  1056. {
  1057. bool sharedAnnType = false;
  1058. if (!attr.RefName.IsEmpty) {
  1059. XmlSchemaAttribute refAtt = (XmlSchemaAttribute)schemas.Find (attr.RefName, typeof (XmlSchemaAttribute));
  1060. if (refAtt == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
  1061. attr = refAtt;
  1062. sharedAnnType = true;
  1063. }
  1064. if (!attr.SchemaTypeName.IsEmpty) return GetTypeData (attr.SchemaTypeName, null);
  1065. else return GetTypeData (attr.SchemaType, typeQName, attr.Name, sharedAnnType, null);
  1066. }
  1067. TypeData GetTypeData (XmlQualifiedName typeQName, XmlQualifiedName root)
  1068. {
  1069. if (typeQName.Namespace == XmlSchema.Namespace)
  1070. return TypeTranslator.GetPrimitiveTypeData (typeQName.Name);
  1071. return ImportType (typeQName, root).TypeData;
  1072. }
  1073. TypeData GetTypeData (XmlSchemaType stype, XmlQualifiedName typeQNname, string propertyName, bool sharedAnnType, XmlQualifiedName root)
  1074. {
  1075. string baseName;
  1076. if (sharedAnnType)
  1077. {
  1078. // Anonymous types defined in root elements or attributes can be shared among all elements that
  1079. // reference this root element or attribute
  1080. TypeData std = sharedAnonymousTypes [stype] as TypeData;
  1081. if (std != null) return std;
  1082. baseName = propertyName;
  1083. }
  1084. else
  1085. baseName = typeQNname.Name + typeIdentifiers.MakeRightCase (propertyName);
  1086. baseName = elemIdentifiers.AddUnique (baseName, stype);
  1087. XmlQualifiedName newName;
  1088. newName = new XmlQualifiedName (baseName, typeQNname.Namespace);
  1089. XmlTypeMapping map = ImportType (newName, stype, root);
  1090. if (sharedAnnType) sharedAnonymousTypes [stype] = map.TypeData;
  1091. return map.TypeData;
  1092. }
  1093. XmlTypeMapping GetTypeMapping (TypeData typeData)
  1094. {
  1095. XmlTypeMapping map = (XmlTypeMapping) dataMappedTypes [typeData];
  1096. if (map != null) return map;
  1097. if (map == null && typeData.IsListType)
  1098. {
  1099. // Create an array map for the type
  1100. XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
  1101. map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
  1102. map.IncludeInSchema = true;
  1103. ListMap listMap = new ListMap ();
  1104. listMap.ItemInfo = new XmlTypeMapElementInfoList();
  1105. listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false));
  1106. map.ObjectMap = listMap;
  1107. mappedTypes [new XmlQualifiedName(map.ElementName, map.Namespace)] = map;
  1108. dataMappedTypes [typeData] = map;
  1109. return map;
  1110. }
  1111. else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
  1112. {
  1113. map = new XmlTypeMapping (typeData.XmlType, XmlSchema.Namespace, typeData, typeData.XmlType, XmlSchema.Namespace);
  1114. map.IncludeInSchema = false;
  1115. dataMappedTypes [typeData] = map;
  1116. return map;
  1117. }
  1118. throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
  1119. }
  1120. XmlTypeMapping GetRegisteredTypeMapping (XmlQualifiedName typeQName)
  1121. {
  1122. return (XmlTypeMapping) mappedTypes [typeQName];
  1123. }
  1124. XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
  1125. {
  1126. XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
  1127. return grp.Particle;
  1128. }
  1129. XmlSchemaElement FindRefElement (XmlSchemaElement elem)
  1130. {
  1131. if (elem.RefName.Namespace == XmlSchema.Namespace)
  1132. {
  1133. if (anyElement != null) return anyElement;
  1134. anyElement = new XmlSchemaElement ();
  1135. anyElement.Name = "any";
  1136. anyElement.SchemaTypeName = anyType;
  1137. return anyElement;
  1138. }
  1139. return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
  1140. }
  1141. string GetDocumentation (XmlSchemaAnnotated elem)
  1142. {
  1143. string res = "";
  1144. XmlSchemaAnnotation anot = elem.Annotation;
  1145. if (anot == null || anot.Items == null) return null;
  1146. foreach (object ob in anot.Items)
  1147. {
  1148. XmlSchemaDocumentation doc = ob as XmlSchemaDocumentation;
  1149. if (doc != null && doc.Markup != null && doc.Markup.Length > 0) {
  1150. if (res != string.Empty) res += "\n";
  1151. foreach (XmlNode node in doc.Markup)
  1152. res += node.Value;
  1153. }
  1154. }
  1155. return res;
  1156. }
  1157. #endregion // Methods
  1158. }
  1159. }