2
0

XmlSchemaImporter.cs 53 KB

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