2
0

XmlSchemaImporter.cs 45 KB

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