XmlSchemaImporter.cs 54 KB

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