XmlSchemaImporter.cs 51 KB

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