XmlSchemaImporter.cs 57 KB

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