XmlSerializationReader.cs 212 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714
  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlSerializationReader.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. //------------------------------------------------------------------------------
  7. namespace System.Xml.Serialization {
  8. using System.IO;
  9. using System;
  10. using System.Security;
  11. using System.Collections;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Xml.Schema;
  16. using System.ComponentModel;
  17. using System.Globalization;
  18. using System.CodeDom.Compiler;
  19. using System.Diagnostics;
  20. using System.Threading;
  21. using System.Configuration;
  22. using System.Xml.Serialization.Configuration;
  23. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader"]/*' />
  24. ///<internalonly/>
  25. public abstract class XmlSerializationReader : XmlSerializationGeneratedCode {
  26. XmlReader r;
  27. XmlCountingReader countingReader;
  28. XmlDocument d;
  29. Hashtable callbacks;
  30. Hashtable types;
  31. Hashtable typesReverse;
  32. XmlDeserializationEvents events;
  33. Hashtable targets;
  34. Hashtable referencedTargets;
  35. ArrayList targetsWithoutIds;
  36. ArrayList fixups;
  37. ArrayList collectionFixups;
  38. bool soap12;
  39. bool isReturnValue;
  40. bool decodeName = true;
  41. string schemaNsID;
  42. string schemaNs1999ID;
  43. string schemaNs2000ID;
  44. string schemaNonXsdTypesNsID;
  45. string instanceNsID;
  46. string instanceNs2000ID;
  47. string instanceNs1999ID;
  48. string soapNsID;
  49. string soap12NsID;
  50. string schemaID;
  51. string wsdlNsID;
  52. string wsdlArrayTypeID;
  53. string nullID;
  54. string nilID;
  55. string typeID;
  56. string arrayTypeID;
  57. string itemTypeID;
  58. string arraySizeID;
  59. string arrayID;
  60. string urTypeID;
  61. string stringID;
  62. string intID;
  63. string booleanID;
  64. string shortID;
  65. string longID;
  66. string floatID;
  67. string doubleID;
  68. string decimalID;
  69. string dateTimeID;
  70. string qnameID;
  71. string dateID;
  72. string timeID;
  73. string hexBinaryID;
  74. string base64BinaryID;
  75. string base64ID;
  76. string unsignedByteID;
  77. string byteID;
  78. string unsignedShortID;
  79. string unsignedIntID;
  80. string unsignedLongID;
  81. string oldDecimalID;
  82. string oldTimeInstantID;
  83. string anyURIID;
  84. string durationID;
  85. string ENTITYID;
  86. string ENTITIESID;
  87. string gDayID;
  88. string gMonthID;
  89. string gMonthDayID;
  90. string gYearID;
  91. string gYearMonthID;
  92. string IDID;
  93. string IDREFID;
  94. string IDREFSID;
  95. string integerID;
  96. string languageID;
  97. string NameID;
  98. string NCNameID;
  99. string NMTOKENID;
  100. string NMTOKENSID;
  101. string negativeIntegerID;
  102. string nonPositiveIntegerID;
  103. string nonNegativeIntegerID;
  104. string normalizedStringID;
  105. string NOTATIONID;
  106. string positiveIntegerID;
  107. string tokenID;
  108. string charID;
  109. string guidID;
  110. string timeSpanID;
  111. static bool checkDeserializeAdvances;
  112. #if CONFIGURATION_DEP
  113. static XmlSerializationReader()
  114. {
  115. XmlSerializerSection configSection = ConfigurationManager.GetSection(ConfigurationStrings.XmlSerializerSectionPath) as XmlSerializerSection;
  116. checkDeserializeAdvances = (configSection == null) ? false : configSection.CheckDeserializeAdvances;
  117. }
  118. #endif
  119. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.InitIDs"]/*' />
  120. protected abstract void InitIDs();
  121. // this method must be called before any generated deserialization methods are called
  122. internal void Init(XmlReader r, XmlDeserializationEvents events, string encodingStyle, TempAssembly tempAssembly) {
  123. this.events = events;
  124. if (checkDeserializeAdvances)
  125. {
  126. this.countingReader = new XmlCountingReader(r);
  127. this.r = this.countingReader;
  128. }
  129. else
  130. this.r = r;
  131. this.d = null;
  132. this.soap12 = (encodingStyle == Soap12.Encoding);
  133. Init(tempAssembly);
  134. schemaNsID = r.NameTable.Add(XmlSchema.Namespace);
  135. schemaNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema");
  136. schemaNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema");
  137. schemaNonXsdTypesNsID = r.NameTable.Add(UrtTypes.Namespace);
  138. instanceNsID = r.NameTable.Add(XmlSchema.InstanceNamespace);
  139. instanceNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema-instance");
  140. instanceNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema-instance");
  141. soapNsID = r.NameTable.Add(Soap.Encoding);
  142. soap12NsID = r.NameTable.Add(Soap12.Encoding);
  143. schemaID = r.NameTable.Add("schema");
  144. wsdlNsID = r.NameTable.Add(Wsdl.Namespace);
  145. wsdlArrayTypeID = r.NameTable.Add(Wsdl.ArrayType);
  146. nullID = r.NameTable.Add("null");
  147. nilID = r.NameTable.Add("nil");
  148. typeID = r.NameTable.Add("type");
  149. arrayTypeID = r.NameTable.Add("arrayType");
  150. itemTypeID = r.NameTable.Add("itemType");
  151. arraySizeID = r.NameTable.Add("arraySize");
  152. arrayID = r.NameTable.Add("Array");
  153. urTypeID = r.NameTable.Add(Soap.UrType);
  154. InitIDs();
  155. }
  156. /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.DecodeName"]/*' />
  157. protected bool DecodeName {
  158. get {
  159. return decodeName;
  160. }
  161. set {
  162. decodeName = value;
  163. }
  164. }
  165. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.Reader"]/*' />
  166. protected XmlReader Reader {
  167. get {
  168. return r;
  169. }
  170. }
  171. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReaderCount"]/*' />
  172. protected int ReaderCount {
  173. get {
  174. return checkDeserializeAdvances ? countingReader.AdvanceCount : 0;
  175. }
  176. }
  177. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.Document"]/*' />
  178. protected XmlDocument Document {
  179. get {
  180. if (d == null) {
  181. d = new XmlDocument(r.NameTable);
  182. d.SetBaseURI(r.BaseURI);
  183. }
  184. return d;
  185. }
  186. }
  187. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ResolveDynamicAssembly"]/*' />
  188. ///<internalonly/>
  189. protected static Assembly ResolveDynamicAssembly(string assemblyFullName){
  190. return DynamicAssemblies.Get(assemblyFullName);
  191. }
  192. void InitPrimitiveIDs() {
  193. if (tokenID != null) return;
  194. object ns = r.NameTable.Add(XmlSchema.Namespace);
  195. object ns2 = r.NameTable.Add(UrtTypes.Namespace);
  196. stringID = r.NameTable.Add("string");
  197. intID = r.NameTable.Add("int");
  198. booleanID = r.NameTable.Add("boolean");
  199. shortID = r.NameTable.Add("short");
  200. longID = r.NameTable.Add("long");
  201. floatID = r.NameTable.Add("float");
  202. doubleID = r.NameTable.Add("double");
  203. decimalID = r.NameTable.Add("decimal");
  204. dateTimeID = r.NameTable.Add("dateTime");
  205. qnameID = r.NameTable.Add("QName");
  206. dateID = r.NameTable.Add("date");
  207. timeID = r.NameTable.Add("time");
  208. hexBinaryID = r.NameTable.Add("hexBinary");
  209. base64BinaryID = r.NameTable.Add("base64Binary");
  210. unsignedByteID = r.NameTable.Add("unsignedByte");
  211. byteID = r.NameTable.Add("byte");
  212. unsignedShortID = r.NameTable.Add("unsignedShort");
  213. unsignedIntID = r.NameTable.Add("unsignedInt");
  214. unsignedLongID = r.NameTable.Add("unsignedLong");
  215. oldDecimalID = r.NameTable.Add("decimal");
  216. oldTimeInstantID = r.NameTable.Add("timeInstant");
  217. charID = r.NameTable.Add("char");
  218. guidID = r.NameTable.Add("guid");
  219. if (LocalAppContextSwitches.EnableTimeSpanSerialization)
  220. {
  221. timeSpanID = r.NameTable.Add("TimeSpan");
  222. }
  223. base64ID = r.NameTable.Add("base64");
  224. anyURIID = r.NameTable.Add("anyURI");
  225. durationID = r.NameTable.Add("duration");
  226. ENTITYID = r.NameTable.Add("ENTITY");
  227. ENTITIESID = r.NameTable.Add("ENTITIES");
  228. gDayID = r.NameTable.Add("gDay");
  229. gMonthID = r.NameTable.Add("gMonth");
  230. gMonthDayID = r.NameTable.Add("gMonthDay");
  231. gYearID = r.NameTable.Add("gYear");
  232. gYearMonthID = r.NameTable.Add("gYearMonth");
  233. IDID = r.NameTable.Add("ID");
  234. IDREFID = r.NameTable.Add("IDREF");
  235. IDREFSID = r.NameTable.Add("IDREFS");
  236. integerID = r.NameTable.Add("integer");
  237. languageID = r.NameTable.Add("language");
  238. NameID = r.NameTable.Add("Name");
  239. NCNameID = r.NameTable.Add("NCName");
  240. NMTOKENID = r.NameTable.Add("NMTOKEN");
  241. NMTOKENSID = r.NameTable.Add("NMTOKENS");
  242. negativeIntegerID = r.NameTable.Add("negativeInteger");
  243. nonNegativeIntegerID = r.NameTable.Add("nonNegativeInteger");
  244. nonPositiveIntegerID = r.NameTable.Add("nonPositiveInteger");
  245. normalizedStringID = r.NameTable.Add("normalizedString");
  246. NOTATIONID = r.NameTable.Add("NOTATION");
  247. positiveIntegerID = r.NameTable.Add("positiveInteger");
  248. tokenID = r.NameTable.Add("token");
  249. }
  250. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.GetXsiType"]/*' />
  251. /// <devdoc>
  252. /// <para>[To be supplied.]</para>
  253. /// </devdoc>
  254. protected XmlQualifiedName GetXsiType() {
  255. string type = r.GetAttribute(typeID, instanceNsID);
  256. if (type == null) {
  257. type = r.GetAttribute(typeID, instanceNs2000ID);
  258. if (type == null) {
  259. type = r.GetAttribute(typeID, instanceNs1999ID);
  260. if (type == null)
  261. return null;
  262. }
  263. }
  264. return ToXmlQualifiedName(type, false);
  265. }
  266. // throwOnUnknown flag controls whether this method throws an exception or just returns
  267. // null if typeName.Namespace is unknown. the method still throws if typeName.Namespace
  268. // is recognized but typeName.Name isn't.
  269. Type GetPrimitiveType(XmlQualifiedName typeName, bool throwOnUnknown) {
  270. InitPrimitiveIDs();
  271. if ((object)typeName.Namespace == (object)schemaNsID || (object)typeName.Namespace == (object)soapNsID || (object)typeName.Namespace == (object)soap12NsID) {
  272. if ((object) typeName.Name == (object) stringID ||
  273. (object) typeName.Name == (object) anyURIID ||
  274. (object) typeName.Name == (object) durationID ||
  275. (object) typeName.Name == (object) ENTITYID ||
  276. (object) typeName.Name == (object) ENTITIESID ||
  277. (object) typeName.Name == (object) gDayID ||
  278. (object) typeName.Name == (object) gMonthID ||
  279. (object) typeName.Name == (object) gMonthDayID ||
  280. (object) typeName.Name == (object) gYearID ||
  281. (object) typeName.Name == (object) gYearMonthID ||
  282. (object) typeName.Name == (object) IDID ||
  283. (object) typeName.Name == (object) IDREFID ||
  284. (object) typeName.Name == (object) IDREFSID ||
  285. (object) typeName.Name == (object) integerID ||
  286. (object) typeName.Name == (object) languageID ||
  287. (object) typeName.Name == (object) NameID ||
  288. (object) typeName.Name == (object) NCNameID ||
  289. (object) typeName.Name == (object) NMTOKENID ||
  290. (object) typeName.Name == (object) NMTOKENSID ||
  291. (object) typeName.Name == (object) negativeIntegerID ||
  292. (object) typeName.Name == (object) nonPositiveIntegerID ||
  293. (object) typeName.Name == (object) nonNegativeIntegerID ||
  294. (object) typeName.Name == (object) normalizedStringID ||
  295. (object) typeName.Name == (object) NOTATIONID ||
  296. (object) typeName.Name == (object) positiveIntegerID ||
  297. (object) typeName.Name == (object) tokenID)
  298. return typeof(string);
  299. else if ((object) typeName.Name == (object) intID)
  300. return typeof(int);
  301. else if ((object) typeName.Name == (object) booleanID)
  302. return typeof(bool);
  303. else if ((object) typeName.Name == (object) shortID)
  304. return typeof(short);
  305. else if ((object) typeName.Name == (object) longID)
  306. return typeof(long);
  307. else if ((object) typeName.Name == (object) floatID)
  308. return typeof(float);
  309. else if ((object) typeName.Name == (object) doubleID)
  310. return typeof(double);
  311. else if ((object) typeName.Name == (object) decimalID)
  312. return typeof(decimal);
  313. else if ((object) typeName.Name == (object) dateTimeID)
  314. return typeof(DateTime);
  315. else if ((object) typeName.Name == (object) qnameID)
  316. return typeof(XmlQualifiedName);
  317. else if ((object) typeName.Name == (object) dateID)
  318. return typeof(DateTime);
  319. else if ((object) typeName.Name == (object) timeID)
  320. return typeof(DateTime);
  321. else if ((object) typeName.Name == (object) hexBinaryID)
  322. return typeof(byte[]);
  323. else if ((object)typeName.Name == (object)base64BinaryID)
  324. return typeof(byte[]);
  325. else if ((object)typeName.Name == (object)unsignedByteID)
  326. return typeof(byte);
  327. else if ((object) typeName.Name == (object) byteID)
  328. return typeof(SByte);
  329. else if ((object) typeName.Name == (object) unsignedShortID)
  330. return typeof(UInt16);
  331. else if ((object) typeName.Name == (object) unsignedIntID)
  332. return typeof(UInt32);
  333. else if ((object) typeName.Name == (object) unsignedLongID)
  334. return typeof(UInt64);
  335. else
  336. throw CreateUnknownTypeException(typeName);
  337. }
  338. else if ((object) typeName.Namespace == (object) schemaNs2000ID || (object) typeName.Namespace == (object) schemaNs1999ID) {
  339. if ((object) typeName.Name == (object) stringID ||
  340. (object) typeName.Name == (object) anyURIID ||
  341. (object) typeName.Name == (object) durationID ||
  342. (object) typeName.Name == (object) ENTITYID ||
  343. (object) typeName.Name == (object) ENTITIESID ||
  344. (object) typeName.Name == (object) gDayID ||
  345. (object) typeName.Name == (object) gMonthID ||
  346. (object) typeName.Name == (object) gMonthDayID ||
  347. (object) typeName.Name == (object) gYearID ||
  348. (object) typeName.Name == (object) gYearMonthID ||
  349. (object) typeName.Name == (object) IDID ||
  350. (object) typeName.Name == (object) IDREFID ||
  351. (object) typeName.Name == (object) IDREFSID ||
  352. (object) typeName.Name == (object) integerID ||
  353. (object) typeName.Name == (object) languageID ||
  354. (object) typeName.Name == (object) NameID ||
  355. (object) typeName.Name == (object) NCNameID ||
  356. (object) typeName.Name == (object) NMTOKENID ||
  357. (object) typeName.Name == (object) NMTOKENSID ||
  358. (object) typeName.Name == (object) negativeIntegerID ||
  359. (object) typeName.Name == (object) nonPositiveIntegerID ||
  360. (object) typeName.Name == (object) nonNegativeIntegerID ||
  361. (object) typeName.Name == (object) normalizedStringID ||
  362. (object) typeName.Name == (object) NOTATIONID ||
  363. (object) typeName.Name == (object) positiveIntegerID ||
  364. (object) typeName.Name == (object) tokenID)
  365. return typeof(string);
  366. else if ((object) typeName.Name == (object) intID)
  367. return typeof(int);
  368. else if ((object) typeName.Name == (object) booleanID)
  369. return typeof(bool);
  370. else if ((object) typeName.Name == (object) shortID)
  371. return typeof(short);
  372. else if ((object) typeName.Name == (object) longID)
  373. return typeof(long);
  374. else if ((object) typeName.Name == (object) floatID)
  375. return typeof(float);
  376. else if ((object) typeName.Name == (object) doubleID)
  377. return typeof(double);
  378. else if ((object) typeName.Name == (object) oldDecimalID)
  379. return typeof(decimal);
  380. else if ((object) typeName.Name == (object) oldTimeInstantID)
  381. return typeof(DateTime);
  382. else if ((object) typeName.Name == (object) qnameID)
  383. return typeof(XmlQualifiedName);
  384. else if ((object) typeName.Name == (object) dateID)
  385. return typeof(DateTime);
  386. else if ((object) typeName.Name == (object) timeID)
  387. return typeof(DateTime);
  388. else if ((object) typeName.Name == (object) hexBinaryID)
  389. return typeof(byte[]);
  390. else if ((object) typeName.Name == (object) byteID)
  391. return typeof(SByte);
  392. else if ((object) typeName.Name == (object) unsignedShortID)
  393. return typeof(UInt16);
  394. else if ((object) typeName.Name == (object) unsignedIntID)
  395. return typeof(UInt32);
  396. else if ((object) typeName.Name == (object) unsignedLongID)
  397. return typeof(UInt64);
  398. else
  399. throw CreateUnknownTypeException(typeName);
  400. }
  401. else if ((object) typeName.Namespace == (object) schemaNonXsdTypesNsID) {
  402. if ((object) typeName.Name == (object) charID)
  403. return typeof(char);
  404. else if ((object) typeName.Name == (object) guidID)
  405. return typeof(Guid);
  406. else
  407. throw CreateUnknownTypeException(typeName);
  408. }
  409. else if (throwOnUnknown)
  410. throw CreateUnknownTypeException(typeName);
  411. else
  412. return null;
  413. }
  414. bool IsPrimitiveNamespace(string ns) {
  415. return (object) ns == (object) schemaNsID ||
  416. (object) ns == (object) schemaNonXsdTypesNsID ||
  417. (object) ns == (object) soapNsID ||
  418. (object) ns == (object) soap12NsID ||
  419. (object) ns == (object) schemaNs2000ID ||
  420. (object) ns == (object) schemaNs1999ID;
  421. }
  422. private string ReadStringValue(){
  423. if (r.IsEmptyElement){
  424. r.Skip();
  425. return string.Empty;
  426. }
  427. r.ReadStartElement();
  428. string retVal = r.ReadString();
  429. ReadEndElement();
  430. return retVal;
  431. }
  432. private XmlQualifiedName ReadXmlQualifiedName(){
  433. string s;
  434. bool isEmpty = false;
  435. if (r.IsEmptyElement) {
  436. s = string.Empty;
  437. isEmpty = true;
  438. }
  439. else{
  440. r.ReadStartElement();
  441. s = r.ReadString();
  442. }
  443. XmlQualifiedName retVal = ToXmlQualifiedName(s);
  444. if (isEmpty)
  445. r.Skip();
  446. else
  447. ReadEndElement();
  448. return retVal;
  449. }
  450. private byte[] ReadByteArray(bool isBase64) {
  451. ArrayList list = new ArrayList();
  452. const int MAX_ALLOC_SIZE = 64*1024;
  453. int currentSize = 1024;
  454. byte[] buffer;
  455. int bytes = -1;
  456. int offset = 0;
  457. int total = 0;
  458. buffer = new byte[currentSize];
  459. list.Add(buffer);
  460. while (bytes != 0) {
  461. if (offset == buffer.Length) {
  462. currentSize = Math.Min(currentSize*2, MAX_ALLOC_SIZE);
  463. buffer = new byte[currentSize];
  464. offset = 0;
  465. list.Add(buffer);
  466. }
  467. if (isBase64) {
  468. bytes = r.ReadElementContentAsBase64(buffer, offset, buffer.Length-offset);
  469. }
  470. else {
  471. bytes = r.ReadElementContentAsBinHex(buffer, offset, buffer.Length-offset);
  472. }
  473. offset += bytes;
  474. total += bytes;
  475. }
  476. byte[] result = new byte[total];
  477. offset = 0;
  478. foreach (byte[] block in list) {
  479. currentSize = Math.Min(block.Length, total);
  480. if (currentSize > 0) {
  481. Buffer.BlockCopy(block, 0, result, offset, currentSize);
  482. offset += currentSize;
  483. total -= currentSize;
  484. }
  485. }
  486. list.Clear();
  487. return result;
  488. }
  489. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadTypedPrimitive"]/*' />
  490. protected object ReadTypedPrimitive(XmlQualifiedName type) {
  491. return ReadTypedPrimitive(type, false);
  492. }
  493. private object ReadTypedPrimitive(XmlQualifiedName type, bool elementCanBeType) {
  494. InitPrimitiveIDs();
  495. object value = null;
  496. if (!IsPrimitiveNamespace(type.Namespace) || (object)type.Name == (object)urTypeID)
  497. return ReadXmlNodes(elementCanBeType);
  498. if ((object)type.Namespace == (object)schemaNsID || (object)type.Namespace == (object)soapNsID || (object)type.Namespace == (object)soap12NsID) {
  499. if ((object) type.Name == (object) stringID ||
  500. (object) type.Name == (object) normalizedStringID)
  501. value = ReadStringValue();
  502. else if ((object) type.Name == (object) anyURIID ||
  503. (object) type.Name == (object) durationID ||
  504. (object) type.Name == (object) ENTITYID ||
  505. (object) type.Name == (object) ENTITIESID ||
  506. (object) type.Name == (object) gDayID ||
  507. (object) type.Name == (object) gMonthID ||
  508. (object) type.Name == (object) gMonthDayID ||
  509. (object) type.Name == (object) gYearID ||
  510. (object) type.Name == (object) gYearMonthID ||
  511. (object) type.Name == (object) IDID ||
  512. (object) type.Name == (object) IDREFID ||
  513. (object) type.Name == (object) IDREFSID ||
  514. (object) type.Name == (object) integerID ||
  515. (object) type.Name == (object) languageID ||
  516. (object) type.Name == (object) NameID ||
  517. (object) type.Name == (object) NCNameID ||
  518. (object) type.Name == (object) NMTOKENID ||
  519. (object) type.Name == (object) NMTOKENSID ||
  520. (object) type.Name == (object) negativeIntegerID ||
  521. (object) type.Name == (object) nonPositiveIntegerID ||
  522. (object) type.Name == (object) nonNegativeIntegerID ||
  523. (object) type.Name == (object) NOTATIONID ||
  524. (object) type.Name == (object) positiveIntegerID ||
  525. (object) type.Name == (object) tokenID)
  526. value = CollapseWhitespace(ReadStringValue());
  527. else if ((object) type.Name == (object) intID)
  528. value = XmlConvert.ToInt32(ReadStringValue());
  529. else if ((object) type.Name == (object) booleanID)
  530. value = XmlConvert.ToBoolean(ReadStringValue());
  531. else if ((object) type.Name == (object) shortID)
  532. value = XmlConvert.ToInt16(ReadStringValue());
  533. else if ((object) type.Name == (object) longID)
  534. value = XmlConvert.ToInt64(ReadStringValue());
  535. else if ((object)type.Name == (object)floatID)
  536. value = XmlConvert.ToSingle(ReadStringValue());
  537. else if ((object)type.Name == (object)doubleID)
  538. value = XmlConvert.ToDouble(ReadStringValue());
  539. else if ((object)type.Name == (object)decimalID)
  540. value = XmlConvert.ToDecimal(ReadStringValue());
  541. else if ((object)type.Name == (object)dateTimeID)
  542. value = ToDateTime(ReadStringValue());
  543. else if ((object) type.Name == (object) qnameID)
  544. value = ReadXmlQualifiedName();
  545. else if ((object) type.Name == (object) dateID)
  546. value = ToDate(ReadStringValue());
  547. else if ((object) type.Name == (object) timeID)
  548. value = ToTime(ReadStringValue());
  549. else if ((object) type.Name == (object) unsignedByteID)
  550. value = XmlConvert.ToByte(ReadStringValue());
  551. else if ((object) type.Name == (object) byteID)
  552. value = XmlConvert.ToSByte(ReadStringValue());
  553. else if ((object) type.Name == (object) unsignedShortID)
  554. value = XmlConvert.ToUInt16(ReadStringValue());
  555. else if ((object) type.Name == (object) unsignedIntID)
  556. value = XmlConvert.ToUInt32(ReadStringValue());
  557. else if ((object) type.Name == (object) unsignedLongID)
  558. value = XmlConvert.ToUInt64(ReadStringValue());
  559. else if ((object) type.Name == (object) hexBinaryID)
  560. value = ToByteArrayHex(false);
  561. else if ((object) type.Name == (object) base64BinaryID)
  562. value = ToByteArrayBase64(false);
  563. else if ((object) type.Name == (object)base64ID && ((object)type.Namespace == (object)soapNsID || (object)type.Namespace == (object)soap12NsID))
  564. value = ToByteArrayBase64(false);
  565. else
  566. value = ReadXmlNodes(elementCanBeType);
  567. }
  568. else if ((object) type.Namespace == (object) schemaNs2000ID || (object) type.Namespace == (object) schemaNs1999ID) {
  569. if ((object) type.Name == (object) stringID ||
  570. (object) type.Name == (object) normalizedStringID)
  571. value = ReadStringValue();
  572. else if ((object) type.Name == (object) anyURIID ||
  573. (object) type.Name == (object) anyURIID ||
  574. (object) type.Name == (object) durationID ||
  575. (object) type.Name == (object) ENTITYID ||
  576. (object) type.Name == (object) ENTITIESID ||
  577. (object) type.Name == (object) gDayID ||
  578. (object) type.Name == (object) gMonthID ||
  579. (object) type.Name == (object) gMonthDayID ||
  580. (object) type.Name == (object) gYearID ||
  581. (object) type.Name == (object) gYearMonthID ||
  582. (object) type.Name == (object) IDID ||
  583. (object) type.Name == (object) IDREFID ||
  584. (object) type.Name == (object) IDREFSID ||
  585. (object) type.Name == (object) integerID ||
  586. (object) type.Name == (object) languageID ||
  587. (object) type.Name == (object) NameID ||
  588. (object) type.Name == (object) NCNameID ||
  589. (object) type.Name == (object) NMTOKENID ||
  590. (object) type.Name == (object) NMTOKENSID ||
  591. (object) type.Name == (object) negativeIntegerID ||
  592. (object) type.Name == (object) nonPositiveIntegerID ||
  593. (object) type.Name == (object) nonNegativeIntegerID ||
  594. (object) type.Name == (object) NOTATIONID ||
  595. (object) type.Name == (object) positiveIntegerID ||
  596. (object) type.Name == (object) tokenID)
  597. value = CollapseWhitespace(ReadStringValue());
  598. else if ((object) type.Name == (object) intID)
  599. value = XmlConvert.ToInt32(ReadStringValue());
  600. else if ((object) type.Name == (object) booleanID)
  601. value = XmlConvert.ToBoolean(ReadStringValue());
  602. else if ((object) type.Name == (object) shortID)
  603. value = XmlConvert.ToInt16(ReadStringValue());
  604. else if ((object) type.Name == (object) longID)
  605. value = XmlConvert.ToInt64(ReadStringValue());
  606. else if ((object)type.Name == (object)floatID)
  607. value = XmlConvert.ToSingle(ReadStringValue());
  608. else if ((object)type.Name == (object)doubleID)
  609. value = XmlConvert.ToDouble(ReadStringValue());
  610. else if ((object)type.Name == (object) oldDecimalID)
  611. value = XmlConvert.ToDecimal(ReadStringValue());
  612. else if ((object)type.Name == (object) oldTimeInstantID)
  613. value = ToDateTime(ReadStringValue());
  614. else if ((object) type.Name == (object) qnameID)
  615. value = ReadXmlQualifiedName();
  616. else if ((object) type.Name == (object) dateID)
  617. value = ToDate(ReadStringValue());
  618. else if ((object) type.Name == (object) timeID)
  619. value = ToTime(ReadStringValue());
  620. else if ((object) type.Name == (object) unsignedByteID)
  621. value = XmlConvert.ToByte(ReadStringValue());
  622. else if ((object) type.Name == (object) byteID)
  623. value = XmlConvert.ToSByte(ReadStringValue());
  624. else if ((object) type.Name == (object) unsignedShortID)
  625. value = XmlConvert.ToUInt16(ReadStringValue());
  626. else if ((object) type.Name == (object) unsignedIntID)
  627. value = XmlConvert.ToUInt32(ReadStringValue());
  628. else if ((object) type.Name == (object) unsignedLongID)
  629. value = XmlConvert.ToUInt64(ReadStringValue());
  630. else
  631. value = ReadXmlNodes(elementCanBeType);
  632. }
  633. else if ((object) type.Namespace == (object) schemaNonXsdTypesNsID) {
  634. if ((object) type.Name == (object) charID)
  635. value = ToChar(ReadStringValue());
  636. else if ((object) type.Name == (object) guidID)
  637. value = new Guid(CollapseWhitespace(ReadStringValue()));
  638. else if ((object)type.Name == (object)timeSpanID && LocalAppContextSwitches.EnableTimeSpanSerialization)
  639. value = XmlConvert.ToTimeSpan(ReadStringValue());
  640. else
  641. value = ReadXmlNodes(elementCanBeType);
  642. }
  643. else
  644. value = ReadXmlNodes(elementCanBeType);
  645. return value;
  646. }
  647. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadTypedNull"]/*' />
  648. protected object ReadTypedNull(XmlQualifiedName type) {
  649. InitPrimitiveIDs();
  650. object value = null;
  651. if (!IsPrimitiveNamespace(type.Namespace) || (object)type.Name == (object)urTypeID) {
  652. return null;
  653. }
  654. if ((object)type.Namespace == (object)schemaNsID || (object)type.Namespace == (object)soapNsID || (object)type.Namespace == (object)soap12NsID) {
  655. if ((object) type.Name == (object) stringID ||
  656. (object) type.Name == (object) anyURIID ||
  657. (object) type.Name == (object) durationID ||
  658. (object) type.Name == (object) ENTITYID ||
  659. (object) type.Name == (object) ENTITIESID ||
  660. (object) type.Name == (object) gDayID ||
  661. (object) type.Name == (object) gMonthID ||
  662. (object) type.Name == (object) gMonthDayID ||
  663. (object) type.Name == (object) gYearID ||
  664. (object) type.Name == (object) gYearMonthID ||
  665. (object) type.Name == (object) IDID ||
  666. (object) type.Name == (object) IDREFID ||
  667. (object) type.Name == (object) IDREFSID ||
  668. (object) type.Name == (object) integerID ||
  669. (object) type.Name == (object) languageID ||
  670. (object) type.Name == (object) NameID ||
  671. (object) type.Name == (object) NCNameID ||
  672. (object) type.Name == (object) NMTOKENID ||
  673. (object) type.Name == (object) NMTOKENSID ||
  674. (object) type.Name == (object) negativeIntegerID ||
  675. (object) type.Name == (object) nonPositiveIntegerID ||
  676. (object) type.Name == (object) nonNegativeIntegerID ||
  677. (object) type.Name == (object) normalizedStringID ||
  678. (object) type.Name == (object) NOTATIONID ||
  679. (object) type.Name == (object) positiveIntegerID ||
  680. (object) type.Name == (object) tokenID)
  681. value = null;
  682. else if ((object) type.Name == (object) intID) {
  683. value = default(Nullable<int>);
  684. }
  685. else if ((object) type.Name == (object) booleanID)
  686. value = default(Nullable<bool>);
  687. else if ((object) type.Name == (object) shortID)
  688. value = default(Nullable<Int16>);
  689. else if ((object) type.Name == (object) longID)
  690. value = default(Nullable<long>);
  691. else if ((object)type.Name == (object)floatID)
  692. value = default(Nullable<float>);
  693. else if ((object)type.Name == (object)doubleID)
  694. value = default(Nullable<double>);
  695. else if ((object)type.Name == (object)decimalID)
  696. value = default(Nullable<decimal>);
  697. else if ((object)type.Name == (object)dateTimeID)
  698. value = default(Nullable<DateTime>);
  699. else if ((object) type.Name == (object) qnameID)
  700. value = null;
  701. else if ((object) type.Name == (object) dateID)
  702. value = default(Nullable<DateTime>);
  703. else if ((object) type.Name == (object) timeID)
  704. value = default(Nullable<DateTime>);
  705. else if ((object) type.Name == (object) unsignedByteID)
  706. value = default(Nullable<byte>);
  707. else if ((object) type.Name == (object) byteID)
  708. value = default(Nullable<SByte>);
  709. else if ((object) type.Name == (object) unsignedShortID)
  710. value = default(Nullable<UInt16>);
  711. else if ((object) type.Name == (object) unsignedIntID)
  712. value = default(Nullable<UInt32>);
  713. else if ((object) type.Name == (object) unsignedLongID)
  714. value = default(Nullable<UInt64>);
  715. else if ((object) type.Name == (object) hexBinaryID)
  716. value = null;
  717. else if ((object) type.Name == (object) base64BinaryID)
  718. value = null;
  719. else if ((object) type.Name == (object)base64ID && ((object)type.Namespace == (object)soapNsID || (object)type.Namespace == (object)soap12NsID))
  720. value = null;
  721. else
  722. value = null;
  723. }
  724. else if ((object) type.Namespace == (object) schemaNonXsdTypesNsID) {
  725. if ((object) type.Name == (object) charID)
  726. value = default(Nullable<char>);
  727. else if ((object) type.Name == (object) guidID)
  728. value = default(Nullable<Guid>);
  729. else if ((object)type.Name == (object)timeSpanID && LocalAppContextSwitches.EnableTimeSpanSerialization)
  730. value = default(Nullable<TimeSpan>);
  731. else
  732. value = null;
  733. }
  734. else
  735. value = null;
  736. return value;
  737. }
  738. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.IsXmlnsAttribute"]/*' />
  739. protected bool IsXmlnsAttribute(string name) {
  740. if (!name.StartsWith("xmlns", StringComparison.Ordinal)) return false;
  741. if (name.Length == 5) return true;
  742. return name[5] == ':';
  743. }
  744. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.IsArrayTypeAttribute"]/*' />
  745. protected void ParseWsdlArrayType(XmlAttribute attr) {
  746. if ((object)attr.LocalName == (object)wsdlArrayTypeID && (object)attr.NamespaceURI == (object)wsdlNsID ) {
  747. int colon = attr.Value.LastIndexOf(':');
  748. if (colon < 0) {
  749. attr.Value = r.LookupNamespace("") + ":" + attr.Value;
  750. }
  751. else {
  752. attr.Value = r.LookupNamespace(attr.Value.Substring(0, colon)) + ":" + attr.Value.Substring(colon + 1);
  753. }
  754. }
  755. return;
  756. }
  757. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.IsReturnValue"]/*' />
  758. protected bool IsReturnValue {
  759. // value only valid for soap 1.1
  760. get { return isReturnValue && !soap12; }
  761. set { isReturnValue = value; }
  762. }
  763. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadNull"]/*' />
  764. protected bool ReadNull() {
  765. if (!GetNullAttr()) return false;
  766. if (r.IsEmptyElement) {
  767. r.Skip();
  768. return true;
  769. }
  770. r.ReadStartElement();
  771. int whileIterations = 0;
  772. int readerCount = ReaderCount;
  773. while (r.NodeType != XmlNodeType.EndElement)
  774. {
  775. UnknownNode(null);
  776. CheckReaderCount(ref whileIterations, ref readerCount);
  777. }
  778. ReadEndElement();
  779. return true;
  780. }
  781. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.GetNullAttr"]/*' />
  782. protected bool GetNullAttr() {
  783. string isNull = r.GetAttribute(nilID, instanceNsID);
  784. if(isNull == null)
  785. isNull = r.GetAttribute(nullID, instanceNsID);
  786. if (isNull == null) {
  787. isNull = r.GetAttribute(nullID, instanceNs2000ID);
  788. if (isNull == null)
  789. isNull = r.GetAttribute(nullID, instanceNs1999ID);
  790. }
  791. if (isNull == null || !XmlConvert.ToBoolean(isNull)) return false;
  792. return true;
  793. }
  794. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadNullableString"]/*' />
  795. protected string ReadNullableString() {
  796. if (ReadNull()) return null;
  797. return r.ReadElementString();
  798. }
  799. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadNullableQualifiedName"]/*' />
  800. /// <devdoc>
  801. /// <para>[To be supplied.]</para>
  802. /// </devdoc>
  803. protected XmlQualifiedName ReadNullableQualifiedName() {
  804. if (ReadNull()) return null;
  805. return ReadElementQualifiedName();
  806. }
  807. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadElementQualifiedName"]/*' />
  808. /// <devdoc>
  809. /// <para>[To be supplied.]</para>
  810. /// </devdoc>
  811. protected XmlQualifiedName ReadElementQualifiedName() {
  812. if (r.IsEmptyElement) {
  813. XmlQualifiedName empty = new XmlQualifiedName(string.Empty, r.LookupNamespace(""));
  814. r.Skip();
  815. return empty;
  816. }
  817. XmlQualifiedName qname = ToXmlQualifiedName(CollapseWhitespace(r.ReadString()));
  818. r.ReadEndElement();
  819. return qname;
  820. }
  821. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadXmlDocument"]/*' />
  822. protected XmlDocument ReadXmlDocument(bool wrapped) {
  823. XmlNode n = ReadXmlNode(wrapped);
  824. if (n == null)
  825. return null;
  826. XmlDocument doc = new XmlDocument();
  827. doc.AppendChild(doc.ImportNode(n, true));
  828. return doc;
  829. }
  830. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CollapseWhitespace"]/*' />
  831. protected string CollapseWhitespace(string value) {
  832. if (value == null)
  833. return null;
  834. return value.Trim();
  835. }
  836. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadXmlNode"]/*' />
  837. protected XmlNode ReadXmlNode(bool wrapped) {
  838. XmlNode node = null;
  839. if (wrapped) {
  840. if (ReadNull()) return null;
  841. r.ReadStartElement();
  842. r.MoveToContent();
  843. if (r.NodeType != XmlNodeType.EndElement)
  844. node = Document.ReadNode(r);
  845. int whileIterations = 0;
  846. int readerCount = ReaderCount;
  847. while (r.NodeType != XmlNodeType.EndElement)
  848. {
  849. UnknownNode(null);
  850. CheckReaderCount(ref whileIterations, ref readerCount);
  851. }
  852. r.ReadEndElement();
  853. }
  854. else {
  855. node = Document.ReadNode(r);
  856. }
  857. return node;
  858. }
  859. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToByteArrayBase64"]/*' />
  860. protected static byte[] ToByteArrayBase64(string value) {
  861. return XmlCustomFormatter.ToByteArrayBase64(value);
  862. }
  863. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToByteArrayBase641"]/*' />
  864. protected byte[] ToByteArrayBase64(bool isNull) {
  865. if (isNull) {
  866. return null;
  867. }
  868. return ReadByteArray(true); //means use Base64
  869. }
  870. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToByteArrayHex"]/*' />
  871. protected static byte[] ToByteArrayHex(string value) {
  872. return XmlCustomFormatter.ToByteArrayHex(value);
  873. }
  874. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToByteArrayHex1"]/*' />
  875. protected byte[] ToByteArrayHex(bool isNull) {
  876. if (isNull) {
  877. return null;
  878. }
  879. return ReadByteArray(false); //means use BinHex
  880. }
  881. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.GetArrayLength"]/*' />
  882. protected int GetArrayLength(string name, string ns) {
  883. if (GetNullAttr()) return 0;
  884. string arrayType = r.GetAttribute(arrayTypeID, soapNsID);
  885. SoapArrayInfo arrayInfo = ParseArrayType(arrayType);
  886. if (arrayInfo.dimensions != 1) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidArrayDimentions, CurrentTag()));
  887. XmlQualifiedName qname = ToXmlQualifiedName(arrayInfo.qname, false);
  888. if (qname.Name != name) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidArrayTypeName, qname.Name, name, CurrentTag()));
  889. if (qname.Namespace != ns) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidArrayTypeNamespace, qname.Namespace, ns, CurrentTag()));
  890. return arrayInfo.length;
  891. }
  892. struct SoapArrayInfo {
  893. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.qname;"]/*' />
  894. public string qname;
  895. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.dimensions;"]/*' />
  896. public int dimensions;
  897. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.length;"]/*' />
  898. public int length;
  899. public int jaggedDimensions;
  900. }
  901. private SoapArrayInfo ParseArrayType(string value) {
  902. if (value == null) {
  903. throw new ArgumentNullException(Res.GetString(Res.XmlMissingArrayType, CurrentTag()));
  904. }
  905. if (value.Length == 0) {
  906. throw new ArgumentException(Res.GetString(Res.XmlEmptyArrayType, CurrentTag()), "value");
  907. }
  908. char[] chars = value.ToCharArray();
  909. int charsLength = chars.Length;
  910. SoapArrayInfo soapArrayInfo = new SoapArrayInfo();
  911. // Parse backwards to get length first, then optional dimensions, then qname.
  912. int pos = charsLength - 1;
  913. // Must end with ]
  914. if (chars[pos] != ']') {
  915. throw new ArgumentException(Res.GetString(Res.XmlInvalidArraySyntax), "value");
  916. }
  917. pos--;
  918. // Find [
  919. while (pos != -1 && chars[pos] != '[') {
  920. if (chars[pos] == ',')
  921. throw new ArgumentException(Res.GetString(Res.XmlInvalidArrayDimentions, CurrentTag()), "value");
  922. pos--;
  923. }
  924. if (pos == -1) {
  925. throw new ArgumentException(Res.GetString(Res.XmlMismatchedArrayBrackets), "value");
  926. }
  927. int len = charsLength - pos - 2;
  928. if (len > 0) {
  929. string lengthString = new String(chars, pos + 1, len);
  930. try {
  931. soapArrayInfo.length = Int32.Parse(lengthString, CultureInfo.InvariantCulture);
  932. }
  933. catch (Exception e) {
  934. if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
  935. throw;
  936. }
  937. throw new ArgumentException(Res.GetString(Res.XmlInvalidArrayLength, lengthString), "value");
  938. }
  939. }
  940. else {
  941. soapArrayInfo.length = -1;
  942. }
  943. pos--;
  944. soapArrayInfo.jaggedDimensions = 0;
  945. while (pos != -1 && chars[pos] == ']') {
  946. pos--;
  947. if (pos < 0)
  948. throw new ArgumentException(Res.GetString(Res.XmlMismatchedArrayBrackets), "value");
  949. if (chars[pos] == ',')
  950. throw new ArgumentException(Res.GetString(Res.XmlInvalidArrayDimentions, CurrentTag()), "value");
  951. else if (chars[pos] != '[')
  952. throw new ArgumentException(Res.GetString(Res.XmlInvalidArraySyntax), "value");
  953. pos--;
  954. soapArrayInfo.jaggedDimensions++;
  955. }
  956. soapArrayInfo.dimensions = 1;
  957. // everything else is qname - validation of qnames?
  958. soapArrayInfo.qname = new String(chars, 0, pos + 1);
  959. return soapArrayInfo;
  960. }
  961. private SoapArrayInfo ParseSoap12ArrayType(string itemType, string arraySize) {
  962. SoapArrayInfo soapArrayInfo = new SoapArrayInfo();
  963. if (itemType != null && itemType.Length > 0)
  964. soapArrayInfo.qname = itemType;
  965. else
  966. soapArrayInfo.qname = "";
  967. string[] dimensions;
  968. if (arraySize != null && arraySize.Length > 0)
  969. dimensions = arraySize.Split(null);
  970. else
  971. dimensions = new string[0];
  972. soapArrayInfo.dimensions = 0;
  973. soapArrayInfo.length = -1;
  974. for (int i = 0; i < dimensions.Length; i++) {
  975. if (dimensions[i].Length > 0) {
  976. if (dimensions[i] == "*") {
  977. soapArrayInfo.dimensions++;
  978. }
  979. else {
  980. try {
  981. soapArrayInfo.length = Int32.Parse(dimensions[i], CultureInfo.InvariantCulture);
  982. soapArrayInfo.dimensions++;
  983. }
  984. catch (Exception e) {
  985. if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
  986. throw;
  987. }
  988. throw new ArgumentException(Res.GetString(Res.XmlInvalidArrayLength, dimensions[i]), "value");
  989. }
  990. }
  991. }
  992. }
  993. if (soapArrayInfo.dimensions == 0)
  994. soapArrayInfo.dimensions = 1; // default is 1D even if no arraySize is specified
  995. return soapArrayInfo;
  996. }
  997. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToDateTime"]/*' />
  998. protected static DateTime ToDateTime(string value) {
  999. return XmlCustomFormatter.ToDateTime(value);
  1000. }
  1001. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToDate"]/*' />
  1002. protected static DateTime ToDate(string value) {
  1003. return XmlCustomFormatter.ToDate(value);
  1004. }
  1005. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToTime"]/*' />
  1006. protected static DateTime ToTime(string value) {
  1007. return XmlCustomFormatter.ToTime(value);
  1008. }
  1009. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToChar"]/*' />
  1010. protected static char ToChar(string value) {
  1011. return XmlCustomFormatter.ToChar(value);
  1012. }
  1013. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToEnum"]/*' />
  1014. protected static long ToEnum(string value, Hashtable h, string typeName) {
  1015. return XmlCustomFormatter.ToEnum(value, h, typeName, true);
  1016. }
  1017. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToXmlName"]/*' />
  1018. protected static string ToXmlName(string value) {
  1019. return XmlCustomFormatter.ToXmlName(value);
  1020. }
  1021. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToXmlNCName"]/*' />
  1022. protected static string ToXmlNCName(string value) {
  1023. return XmlCustomFormatter.ToXmlNCName(value);
  1024. }
  1025. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToXmlNmToken"]/*' />
  1026. protected static string ToXmlNmToken(string value) {
  1027. return XmlCustomFormatter.ToXmlNmToken(value);
  1028. }
  1029. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToXmlNmTokens"]/*' />
  1030. protected static string ToXmlNmTokens(string value) {
  1031. return XmlCustomFormatter.ToXmlNmTokens(value);
  1032. }
  1033. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ToXmlQualifiedName"]/*' />
  1034. protected XmlQualifiedName ToXmlQualifiedName(string value) {
  1035. return ToXmlQualifiedName(value, DecodeName);
  1036. }
  1037. internal XmlQualifiedName ToXmlQualifiedName(string value, bool decodeName) {
  1038. int colon = value == null ? -1 : value.LastIndexOf(':');
  1039. string prefix = colon < 0 ? null : value.Substring(0, colon);
  1040. string localName = value.Substring(colon + 1);
  1041. if (decodeName) {
  1042. prefix = XmlConvert.DecodeName(prefix);
  1043. localName = XmlConvert.DecodeName(localName);
  1044. }
  1045. if (prefix == null || prefix.Length == 0) {
  1046. return new XmlQualifiedName(r.NameTable.Add(value), r.LookupNamespace(String.Empty));
  1047. }
  1048. else {
  1049. string ns = r.LookupNamespace(prefix);
  1050. if (ns == null) {
  1051. // Namespace prefix '{0}' is not defined.
  1052. throw new InvalidOperationException(Res.GetString(Res.XmlUndefinedAlias, prefix));
  1053. }
  1054. return new XmlQualifiedName(r.NameTable.Add(localName), ns);
  1055. }
  1056. }
  1057. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownAttribute"]/*' />
  1058. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownAttribute"]/*' />
  1059. protected void UnknownAttribute(object o, XmlAttribute attr) {
  1060. UnknownAttribute(o, attr, null);
  1061. }
  1062. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownAttribute1"]/*' />
  1063. protected void UnknownAttribute(object o, XmlAttribute attr, string qnames) {
  1064. if (events.OnUnknownAttribute != null) {
  1065. int lineNumber, linePosition;
  1066. GetCurrentPosition(out lineNumber, out linePosition);
  1067. XmlAttributeEventArgs e = new XmlAttributeEventArgs(attr, lineNumber, linePosition, o, qnames);
  1068. events.OnUnknownAttribute(events.sender, e);
  1069. }
  1070. }
  1071. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownAttribute"]/*' />
  1072. protected void UnknownElement(object o, XmlElement elem) {
  1073. UnknownElement(o, elem, null);
  1074. }
  1075. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownElement1"]/*' />
  1076. protected void UnknownElement(object o, XmlElement elem, string qnames) {
  1077. if (events.OnUnknownElement != null) {
  1078. int lineNumber, linePosition;
  1079. GetCurrentPosition(out lineNumber, out linePosition);
  1080. XmlElementEventArgs e = new XmlElementEventArgs(elem, lineNumber, linePosition, o, qnames);
  1081. events.OnUnknownElement(events.sender, e);
  1082. }
  1083. }
  1084. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownNode"]/*' />
  1085. protected void UnknownNode(object o) {
  1086. UnknownNode(o, null);
  1087. }
  1088. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnknownNode1"]/*' />
  1089. protected void UnknownNode(object o, string qnames) {
  1090. if (r.NodeType == XmlNodeType.None || r.NodeType == XmlNodeType.Whitespace) {
  1091. r.Read();
  1092. return;
  1093. }
  1094. if (r.NodeType == XmlNodeType.EndElement)
  1095. return;
  1096. if (events.OnUnknownNode != null) {
  1097. UnknownNode(Document.ReadNode(r), o, qnames);
  1098. }
  1099. else if (r.NodeType == XmlNodeType.Attribute && events.OnUnknownAttribute == null) {
  1100. return;
  1101. }
  1102. else if (r.NodeType == XmlNodeType.Element && events.OnUnknownElement == null) {
  1103. r.Skip();
  1104. return;
  1105. }
  1106. else {
  1107. UnknownNode(Document.ReadNode(r), o, qnames);
  1108. }
  1109. }
  1110. void UnknownNode(XmlNode unknownNode, object o, string qnames) {
  1111. if (unknownNode == null)
  1112. return;
  1113. if (unknownNode.NodeType != XmlNodeType.None && unknownNode.NodeType != XmlNodeType.Whitespace && events.OnUnknownNode != null) {
  1114. int lineNumber, linePosition;
  1115. GetCurrentPosition(out lineNumber, out linePosition);
  1116. XmlNodeEventArgs e = new XmlNodeEventArgs(unknownNode, lineNumber, linePosition, o);
  1117. events.OnUnknownNode(events.sender, e);
  1118. }
  1119. if (unknownNode.NodeType == XmlNodeType.Attribute) {
  1120. UnknownAttribute(o, (XmlAttribute)unknownNode, qnames);
  1121. }
  1122. else if (unknownNode.NodeType == XmlNodeType.Element) {
  1123. UnknownElement(o, (XmlElement)unknownNode, qnames);
  1124. }
  1125. }
  1126. void GetCurrentPosition(out int lineNumber, out int linePosition){
  1127. if (Reader is IXmlLineInfo){
  1128. IXmlLineInfo lineInfo = (IXmlLineInfo)Reader;
  1129. lineNumber = lineInfo.LineNumber;
  1130. linePosition = lineInfo.LinePosition;
  1131. }
  1132. else
  1133. lineNumber = linePosition = -1;
  1134. }
  1135. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.UnreferencedObject"]/*' />
  1136. protected void UnreferencedObject(string id, object o) {
  1137. if (events.OnUnreferencedObject != null) {
  1138. UnreferencedObjectEventArgs e = new UnreferencedObjectEventArgs(o, id);
  1139. events.OnUnreferencedObject(events.sender, e);
  1140. }
  1141. }
  1142. string CurrentTag() {
  1143. switch (r.NodeType) {
  1144. case XmlNodeType.Element:
  1145. return "<" + r.LocalName + " xmlns='" + r.NamespaceURI + "'>";
  1146. case XmlNodeType.EndElement:
  1147. return ">";
  1148. case XmlNodeType.Text:
  1149. return r.Value;
  1150. case XmlNodeType.CDATA:
  1151. return "CDATA";
  1152. case XmlNodeType.Comment:
  1153. return "<--";
  1154. case XmlNodeType.ProcessingInstruction:
  1155. return "<?";
  1156. default:
  1157. return "(unknown)";
  1158. }
  1159. }
  1160. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateUnknownTypeException"]/*' />
  1161. protected Exception CreateUnknownTypeException(XmlQualifiedName type) {
  1162. return new InvalidOperationException(Res.GetString(Res.XmlUnknownType, type.Name, type.Namespace, CurrentTag()));
  1163. }
  1164. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateReadOnlyCollectionException"]/*' />
  1165. protected Exception CreateReadOnlyCollectionException(string name) {
  1166. return new InvalidOperationException(Res.GetString(Res.XmlReadOnlyCollection, name));
  1167. }
  1168. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateAbstractTypeException"]/*' />
  1169. protected Exception CreateAbstractTypeException(string name, string ns) {
  1170. return new InvalidOperationException(Res.GetString(Res.XmlAbstractType, name, ns, CurrentTag()));
  1171. }
  1172. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateInaccessibleConstructorException"]/*' />
  1173. protected Exception CreateInaccessibleConstructorException(string typeName) {
  1174. return new InvalidOperationException(Res.GetString(Res.XmlConstructorInaccessible, typeName));
  1175. }
  1176. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateCtorHasSecurityException"]/*' />
  1177. protected Exception CreateCtorHasSecurityException(string typeName) {
  1178. return new InvalidOperationException(Res.GetString(Res.XmlConstructorHasSecurityAttributes, typeName));
  1179. }
  1180. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateUnknownNodeException"]/*' />
  1181. protected Exception CreateUnknownNodeException() {
  1182. return new InvalidOperationException(Res.GetString(Res.XmlUnknownNode, CurrentTag()));
  1183. }
  1184. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateUnknownConstantException"]/*' />
  1185. protected Exception CreateUnknownConstantException(string value, Type enumType) {
  1186. return new InvalidOperationException(Res.GetString(Res.XmlUnknownConstant, value, enumType.Name));
  1187. }
  1188. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateInvalidCastException"]/*' />
  1189. protected Exception CreateInvalidCastException(Type type, object value) {
  1190. return CreateInvalidCastException(type, value, null);
  1191. }
  1192. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateInvalidCastException1"]/*' />
  1193. protected Exception CreateInvalidCastException(Type type, object value, string id) {
  1194. if (value == null)
  1195. return new InvalidCastException(Res.GetString(Res.XmlInvalidNullCast, type.FullName));
  1196. else if (id == null)
  1197. return new InvalidCastException(Res.GetString(Res.XmlInvalidCast, value.GetType().FullName, type.FullName));
  1198. else
  1199. return new InvalidCastException(Res.GetString(Res.XmlInvalidCastWithId, value.GetType().FullName, type.FullName, id));
  1200. }
  1201. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateBadDerivationException"]/*' />
  1202. protected Exception CreateBadDerivationException(string xsdDerived, string nsDerived, string xsdBase, string nsBase, string clrDerived, string clrBase) {
  1203. return new InvalidOperationException(Res.GetString(Res.XmlSerializableBadDerivation, xsdDerived, nsDerived, xsdBase, nsBase, clrDerived, clrBase));
  1204. }
  1205. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CreateMissingIXmlSerializableType"]/*' />
  1206. protected Exception CreateMissingIXmlSerializableType(string name, string ns, string clrType) {
  1207. return new InvalidOperationException(Res.GetString(Res.XmlSerializableMissingClrType, name, ns, typeof(XmlIncludeAttribute).Name, clrType));
  1208. //XmlSerializableMissingClrType= Type '{0}' from namespace '{1}' doesnot have corresponding IXmlSerializable type. Please consider adding {2} to '{3}'.
  1209. }
  1210. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.EnsureArrayIndex"]/*' />
  1211. protected Array EnsureArrayIndex(Array a, int index, Type elementType) {
  1212. if (a == null) return Array.CreateInstance(elementType, 32);
  1213. if (index < a.Length) return a;
  1214. Array b = Array.CreateInstance(elementType, a.Length * 2);
  1215. Array.Copy(a, b, index);
  1216. return b;
  1217. }
  1218. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ShrinkArray"]/*' />
  1219. protected Array ShrinkArray(Array a, int length, Type elementType, bool isNullable) {
  1220. if (a == null) {
  1221. if (isNullable) return null;
  1222. return Array.CreateInstance(elementType, 0);
  1223. }
  1224. if (a.Length == length) return a;
  1225. Array b = Array.CreateInstance(elementType, length);
  1226. Array.Copy(a, b, length);
  1227. return b;
  1228. }
  1229. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadString"]/*' />
  1230. protected string ReadString(string value) {
  1231. return ReadString(value, false);
  1232. }
  1233. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadString1"]/*' />
  1234. protected string ReadString(string value, bool trim) {
  1235. string str = r.ReadString();
  1236. if (str != null && trim)
  1237. str = str.Trim();
  1238. if (value == null || value.Length == 0)
  1239. return str;
  1240. return value + str;
  1241. }
  1242. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadSerializable"]/*' />
  1243. protected IXmlSerializable ReadSerializable(IXmlSerializable serializable) {
  1244. return ReadSerializable(serializable, false);
  1245. }
  1246. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadSerializable"]/*' />
  1247. protected IXmlSerializable ReadSerializable(IXmlSerializable serializable, bool wrappedAny)
  1248. {
  1249. string name = null;
  1250. string ns = null;
  1251. if (wrappedAny) {
  1252. name = r.LocalName;
  1253. ns = r.NamespaceURI;
  1254. r.Read();
  1255. r.MoveToContent();
  1256. }
  1257. serializable.ReadXml(r);
  1258. if (wrappedAny) {
  1259. while (r.NodeType == XmlNodeType.Whitespace) r.Skip();
  1260. if (r.NodeType == XmlNodeType.None) r.Skip();
  1261. if (r.NodeType == XmlNodeType.EndElement && r.LocalName == name && r.NamespaceURI == ns) {
  1262. Reader.Read();
  1263. }
  1264. }
  1265. return serializable;
  1266. }
  1267. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReference"]/*' />
  1268. protected bool ReadReference(out string fixupReference) {
  1269. string href = soap12 ? r.GetAttribute("ref", Soap12.Encoding) : r.GetAttribute("href");
  1270. if (href == null) {
  1271. fixupReference = null;
  1272. return false;
  1273. }
  1274. if (!soap12) {
  1275. // soap 1.1 href starts with '#'; soap 1.2 ref does not
  1276. if (!href.StartsWith("#", StringComparison.Ordinal)) throw new InvalidOperationException(Res.GetString(Res.XmlMissingHref, href));
  1277. fixupReference = href.Substring(1);
  1278. }
  1279. else
  1280. fixupReference = href;
  1281. if (r.IsEmptyElement) {
  1282. r.Skip();
  1283. }
  1284. else {
  1285. r.ReadStartElement();
  1286. ReadEndElement();
  1287. }
  1288. return true;
  1289. }
  1290. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.AddTarget"]/*' />
  1291. protected void AddTarget(string id, object o) {
  1292. if (id == null) {
  1293. if (targetsWithoutIds == null)
  1294. targetsWithoutIds = new ArrayList();
  1295. if (o != null)
  1296. targetsWithoutIds.Add(o);
  1297. }
  1298. else {
  1299. if (targets == null) targets = new Hashtable();
  1300. if (!targets.Contains(id))
  1301. targets.Add(id, o);
  1302. }
  1303. }
  1304. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.AddFixup"]/*' />
  1305. protected void AddFixup(Fixup fixup) {
  1306. if (fixups == null) fixups = new ArrayList();
  1307. fixups.Add(fixup);
  1308. }
  1309. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.AddFixup2"]/*' />
  1310. protected void AddFixup(CollectionFixup fixup) {
  1311. if (collectionFixups == null) collectionFixups = new ArrayList();
  1312. collectionFixups.Add(fixup);
  1313. }
  1314. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.GetTarget"]/*' />
  1315. protected object GetTarget(string id) {
  1316. object target = targets != null ? targets[id] : null;
  1317. if (target == null) {
  1318. throw new InvalidOperationException(Res.GetString(Res.XmlInvalidHref, id));
  1319. }
  1320. Referenced(target);
  1321. return target;
  1322. }
  1323. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.Referenced"]/*' />
  1324. protected void Referenced(object o) {
  1325. if (o == null) return;
  1326. if (referencedTargets == null) referencedTargets = new Hashtable();
  1327. referencedTargets[o] = o;
  1328. }
  1329. void HandleUnreferencedObjects() {
  1330. if (targets != null) {
  1331. foreach (DictionaryEntry target in targets) {
  1332. if (referencedTargets == null || !referencedTargets.Contains(target.Value)) {
  1333. UnreferencedObject((string)target.Key, target.Value);
  1334. }
  1335. }
  1336. }
  1337. if (targetsWithoutIds != null) {
  1338. foreach (object o in targetsWithoutIds) {
  1339. if (referencedTargets == null || !referencedTargets.Contains(o)) {
  1340. UnreferencedObject(null, o);
  1341. }
  1342. }
  1343. }
  1344. }
  1345. void DoFixups() {
  1346. if (fixups == null) return;
  1347. for (int i = 0; i < fixups.Count; i++) {
  1348. Fixup fixup = (Fixup)fixups[i];
  1349. fixup.Callback(fixup);
  1350. }
  1351. if (collectionFixups == null) return;
  1352. for (int i = 0; i < collectionFixups.Count; i++) {
  1353. CollectionFixup collectionFixup = (CollectionFixup)collectionFixups[i];
  1354. collectionFixup.Callback(collectionFixup.Collection, collectionFixup.CollectionItems);
  1355. }
  1356. }
  1357. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.FixupArrayRefs"]/*' />
  1358. protected void FixupArrayRefs(object fixup) {
  1359. Fixup f = (Fixup)fixup;
  1360. Array array = (Array)f.Source;
  1361. for (int i = 0; i < array.Length; i++) {
  1362. string id = f.Ids[i];
  1363. if (id == null) continue;
  1364. object o = GetTarget(id);
  1365. try {
  1366. array.SetValue(o, i);
  1367. }
  1368. catch (InvalidCastException) {
  1369. throw new InvalidOperationException(Res.GetString(Res.XmlInvalidArrayRef, id, o.GetType().FullName, i.ToString(CultureInfo.InvariantCulture)));
  1370. }
  1371. }
  1372. }
  1373. object ReadArray(string typeName, string typeNs) {
  1374. SoapArrayInfo arrayInfo;
  1375. Type fallbackElementType = null;
  1376. if (soap12) {
  1377. string itemType = r.GetAttribute(itemTypeID, soap12NsID);
  1378. string arraySize = r.GetAttribute(arraySizeID, soap12NsID);
  1379. Type arrayType = (Type)types[new XmlQualifiedName(typeName, typeNs)];
  1380. // no indication that this is an array?
  1381. if (itemType == null && arraySize == null && (arrayType == null || !arrayType.IsArray))
  1382. return null;
  1383. arrayInfo = ParseSoap12ArrayType(itemType, arraySize);
  1384. if (arrayType != null)
  1385. fallbackElementType = TypeScope.GetArrayElementType(arrayType, null);
  1386. }
  1387. else {
  1388. string arrayType = r.GetAttribute(arrayTypeID, soapNsID);
  1389. if (arrayType == null)
  1390. return null;
  1391. arrayInfo = ParseArrayType(arrayType);
  1392. }
  1393. if (arrayInfo.dimensions != 1) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidArrayDimentions, CurrentTag()));
  1394. // NOTE: don't use the array size that is specified since an evil client might pass
  1395. // a number larger than the actual number of items in an attempt to harm the server.
  1396. XmlQualifiedName qname;
  1397. bool isPrimitive;
  1398. Type elementType = null;
  1399. XmlQualifiedName urTypeName = new XmlQualifiedName(urTypeID, schemaNsID);
  1400. if (arrayInfo.qname.Length > 0) {
  1401. qname = ToXmlQualifiedName(arrayInfo.qname, false);
  1402. elementType = (Type)types[qname];
  1403. }
  1404. else
  1405. qname = urTypeName;
  1406. // try again if the best we could come up with was object
  1407. if (soap12 && elementType == typeof(object))
  1408. elementType = null;
  1409. if (elementType == null) {
  1410. if (!soap12) {
  1411. elementType = GetPrimitiveType(qname, true);
  1412. isPrimitive = true;
  1413. }
  1414. else {
  1415. // try it as a primitive
  1416. if (qname != urTypeName)
  1417. elementType = GetPrimitiveType(qname, false);
  1418. if (elementType != null) {
  1419. isPrimitive = true;
  1420. }
  1421. else {
  1422. // still nothing: go with fallback type or object
  1423. if (fallbackElementType == null) {
  1424. elementType = typeof(object);
  1425. isPrimitive = false;
  1426. }
  1427. else {
  1428. elementType = fallbackElementType;
  1429. XmlQualifiedName newQname = (XmlQualifiedName)typesReverse[elementType];
  1430. if (newQname == null) {
  1431. newQname = XmlSerializationWriter.GetPrimitiveTypeNameInternal(elementType);
  1432. isPrimitive = true;
  1433. }
  1434. else
  1435. isPrimitive = elementType.IsPrimitive;
  1436. if (newQname != null) qname = newQname;
  1437. }
  1438. }
  1439. }
  1440. }
  1441. else
  1442. isPrimitive = elementType.IsPrimitive;
  1443. if (!soap12 && arrayInfo.jaggedDimensions > 0) {
  1444. for (int i = 0; i < arrayInfo.jaggedDimensions; i++)
  1445. elementType = elementType.MakeArrayType();
  1446. }
  1447. if (r.IsEmptyElement) {
  1448. r.Skip();
  1449. return Array.CreateInstance(elementType, 0);
  1450. }
  1451. r.ReadStartElement();
  1452. r.MoveToContent();
  1453. int arrayLength = 0;
  1454. Array array = null;
  1455. if (elementType.IsValueType) {
  1456. if (!isPrimitive && !elementType.IsEnum) {
  1457. throw new NotSupportedException(Res.GetString(Res.XmlRpcArrayOfValueTypes, elementType.FullName));
  1458. }
  1459. //
  1460. int whileIterations = 0;
  1461. int readerCount = ReaderCount;
  1462. while (r.NodeType != XmlNodeType.EndElement) {
  1463. array = EnsureArrayIndex(array, arrayLength, elementType);
  1464. array.SetValue(ReadReferencedElement(qname.Name, qname.Namespace), arrayLength);
  1465. arrayLength++;
  1466. r.MoveToContent();
  1467. CheckReaderCount(ref whileIterations, ref readerCount);
  1468. }
  1469. array = ShrinkArray(array, arrayLength, elementType, false);
  1470. }
  1471. else {
  1472. string type;
  1473. string typens;
  1474. string[] ids = null;
  1475. int idsLength = 0;
  1476. int whileIterations = 0;
  1477. int readerCount = ReaderCount;
  1478. while (r.NodeType != XmlNodeType.EndElement) {
  1479. array = EnsureArrayIndex(array, arrayLength, elementType);
  1480. ids = (string[])EnsureArrayIndex(ids, idsLength, typeof(string));
  1481. //
  1482. if (r.NamespaceURI.Length != 0){
  1483. type = r.LocalName;
  1484. if ((object)r.NamespaceURI == (object)soapNsID)
  1485. typens = XmlSchema.Namespace;
  1486. else
  1487. typens = r.NamespaceURI;
  1488. }
  1489. else {
  1490. type = qname.Name;
  1491. typens = qname.Namespace;
  1492. }
  1493. array.SetValue(ReadReferencingElement(type, typens, out ids[idsLength]), arrayLength);
  1494. arrayLength++;
  1495. idsLength++;
  1496. //
  1497. r.MoveToContent();
  1498. CheckReaderCount(ref whileIterations, ref readerCount);
  1499. }
  1500. // special case for soap 1.2: try to get a better fit than object[] when no metadata is known
  1501. // this applies in the doc/enc/bare case
  1502. if (soap12 && elementType == typeof(object)) {
  1503. Type itemType = null;
  1504. for (int i = 0; i < arrayLength; i++) {
  1505. object currItem = array.GetValue(i);
  1506. if (currItem != null) {
  1507. Type currItemType = currItem.GetType();
  1508. if (currItemType.IsValueType) {
  1509. itemType = null;
  1510. break;
  1511. }
  1512. if (itemType == null || currItemType.IsAssignableFrom(itemType)) {
  1513. itemType = currItemType;
  1514. }
  1515. else if (!itemType.IsAssignableFrom(currItemType)) {
  1516. itemType = null;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. if (itemType != null)
  1522. elementType = itemType;
  1523. }
  1524. ids = (string[])ShrinkArray(ids, idsLength, typeof(string), false);
  1525. array = ShrinkArray(array, arrayLength, elementType, false);
  1526. Fixup fixupArray = new Fixup(array, new XmlSerializationFixupCallback(this.FixupArrayRefs), ids);
  1527. AddFixup(fixupArray);
  1528. }
  1529. //
  1530. ReadEndElement();
  1531. return array;
  1532. }
  1533. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.InitCallbacks"]/*' />
  1534. protected abstract void InitCallbacks();
  1535. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencedElements"]/*' />
  1536. protected void ReadReferencedElements() {
  1537. r.MoveToContent();
  1538. string dummy;
  1539. int whileIterations = 0;
  1540. int readerCount = ReaderCount;
  1541. while (r.NodeType != XmlNodeType.EndElement && r.NodeType != XmlNodeType.None) {
  1542. ReadReferencingElement(null, null, true, out dummy);
  1543. r.MoveToContent();
  1544. CheckReaderCount(ref whileIterations, ref readerCount);
  1545. }
  1546. DoFixups();
  1547. HandleUnreferencedObjects();
  1548. }
  1549. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencedElement"]/*' />
  1550. protected object ReadReferencedElement() {
  1551. return ReadReferencedElement(null, null);
  1552. }
  1553. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencedElement1"]/*' />
  1554. protected object ReadReferencedElement(string name, string ns) {
  1555. string dummy;
  1556. return ReadReferencingElement(name, ns, out dummy);
  1557. }
  1558. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencingElement"]/*' />
  1559. protected object ReadReferencingElement(out string fixupReference) {
  1560. return ReadReferencingElement(null, null, out fixupReference);
  1561. }
  1562. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencingElement1"]/*' />
  1563. protected object ReadReferencingElement(string name, string ns, out string fixupReference) {
  1564. return ReadReferencingElement(name, ns, false, out fixupReference);
  1565. }
  1566. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadReferencingElement2"]/*' />
  1567. protected object ReadReferencingElement(string name, string ns, bool elementCanBeType, out string fixupReference) {
  1568. object o = null;
  1569. if (callbacks == null) {
  1570. callbacks = new Hashtable();
  1571. types = new Hashtable();
  1572. XmlQualifiedName urType = new XmlQualifiedName(urTypeID, r.NameTable.Add(XmlSchema.Namespace));
  1573. types.Add(urType, typeof(object));
  1574. typesReverse = new Hashtable();
  1575. typesReverse.Add(typeof(object), urType);
  1576. InitCallbacks();
  1577. }
  1578. r.MoveToContent();
  1579. if (ReadReference(out fixupReference)) return null;
  1580. if (ReadNull()) return null;
  1581. string id = soap12 ? r.GetAttribute("id", Soap12.Encoding) : r.GetAttribute("id", null);
  1582. if ((o = ReadArray(name, ns)) == null) {
  1583. XmlQualifiedName typeId = GetXsiType();
  1584. if (typeId == null) {
  1585. if (name == null)
  1586. typeId = new XmlQualifiedName(r.NameTable.Add(r.LocalName), r.NameTable.Add(r.NamespaceURI));
  1587. else
  1588. typeId = new XmlQualifiedName(r.NameTable.Add(name), r.NameTable.Add(ns));
  1589. }
  1590. XmlSerializationReadCallback callback = (XmlSerializationReadCallback)callbacks[typeId];
  1591. if (callback != null) {
  1592. o = callback();
  1593. }
  1594. else
  1595. o = ReadTypedPrimitive(typeId, elementCanBeType);
  1596. }
  1597. AddTarget(id, o);
  1598. return o;
  1599. }
  1600. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.AddReadCallback"]/*' />
  1601. protected void AddReadCallback(string name, string ns, Type type, XmlSerializationReadCallback read) {
  1602. XmlQualifiedName typeName = new XmlQualifiedName(r.NameTable.Add(name), r.NameTable.Add(ns));
  1603. callbacks[typeName] = read;
  1604. types[typeName] = type;
  1605. typesReverse[type] = typeName;
  1606. }
  1607. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.ReadEndElement"]/*' />
  1608. protected void ReadEndElement() {
  1609. while (r.NodeType == XmlNodeType.Whitespace) r.Skip();
  1610. if (r.NodeType == XmlNodeType.None) r.Skip();
  1611. else r.ReadEndElement();
  1612. }
  1613. object ReadXmlNodes(bool elementCanBeType) {
  1614. ArrayList xmlNodeList = new ArrayList();
  1615. string elemLocalName = Reader.LocalName;
  1616. string elemNs = Reader.NamespaceURI;
  1617. string elemName = Reader.Name;
  1618. string xsiTypeName = null;
  1619. string xsiTypeNs = null;
  1620. int skippableNodeCount = 0;
  1621. int lineNumber = -1, linePosition=-1;
  1622. XmlNode unknownNode = null;
  1623. if(Reader.NodeType == XmlNodeType.Attribute){
  1624. XmlAttribute attr = Document.CreateAttribute(elemName, elemNs);
  1625. attr.Value = Reader.Value;
  1626. unknownNode = attr;
  1627. }
  1628. else
  1629. unknownNode = Document.CreateElement(elemName, elemNs);
  1630. GetCurrentPosition(out lineNumber, out linePosition);
  1631. XmlElement unknownElement = unknownNode as XmlElement;
  1632. while (Reader.MoveToNextAttribute()) {
  1633. if (IsXmlnsAttribute(Reader.Name) || (Reader.Name == "id" && (!soap12 || Reader.NamespaceURI == Soap12.Encoding)))
  1634. skippableNodeCount++;
  1635. if ( (object)Reader.LocalName == (object)typeID &&
  1636. ( (object)Reader.NamespaceURI == (object)instanceNsID ||
  1637. (object)Reader.NamespaceURI == (object)instanceNs2000ID ||
  1638. (object)Reader.NamespaceURI == (object)instanceNs1999ID
  1639. )
  1640. ){
  1641. string value = Reader.Value;
  1642. int colon = value.LastIndexOf(':');
  1643. xsiTypeName = (colon >= 0) ? value.Substring(colon+1) : value;
  1644. xsiTypeNs = Reader.LookupNamespace((colon >= 0) ? value.Substring(0, colon) : "");
  1645. }
  1646. XmlAttribute xmlAttribute = (XmlAttribute)Document.ReadNode(r);
  1647. xmlNodeList.Add(xmlAttribute);
  1648. if (unknownElement != null) unknownElement.SetAttributeNode(xmlAttribute);
  1649. }
  1650. // If the node is referenced (or in case of paramStyle = bare) and if xsi:type is not
  1651. // specified then the element name is used as the type name. Reveal this to the user
  1652. // by adding an extra attribute node "xsi:type" with value as the element name.
  1653. if(elementCanBeType && xsiTypeName == null){
  1654. xsiTypeName = elemLocalName;
  1655. xsiTypeNs = elemNs;
  1656. XmlAttribute xsiTypeAttribute = Document.CreateAttribute(typeID, instanceNsID);
  1657. xsiTypeAttribute.Value = elemName;
  1658. xmlNodeList.Add(xsiTypeAttribute);
  1659. }
  1660. if( xsiTypeName == Soap.UrType &&
  1661. ( (object)xsiTypeNs == (object)schemaNsID ||
  1662. (object)xsiTypeNs == (object)schemaNs1999ID ||
  1663. (object)xsiTypeNs == (object)schemaNs2000ID
  1664. )
  1665. )
  1666. skippableNodeCount++;
  1667. Reader.MoveToElement();
  1668. if (Reader.IsEmptyElement) {
  1669. Reader.Skip();
  1670. }
  1671. else {
  1672. Reader.ReadStartElement();
  1673. Reader.MoveToContent();
  1674. int whileIterations = 0;
  1675. int readerCount = ReaderCount;
  1676. while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) {
  1677. XmlNode xmlNode = Document.ReadNode(r);
  1678. xmlNodeList.Add(xmlNode);
  1679. if (unknownElement != null) unknownElement.AppendChild(xmlNode);
  1680. Reader.MoveToContent();
  1681. CheckReaderCount(ref whileIterations, ref readerCount);
  1682. }
  1683. ReadEndElement();
  1684. }
  1685. if(xmlNodeList.Count <= skippableNodeCount)
  1686. return new object();
  1687. XmlNode[] childNodes = (XmlNode[])xmlNodeList.ToArray(typeof(XmlNode));
  1688. UnknownNode(unknownNode, null, null);
  1689. return childNodes;
  1690. }
  1691. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReader.CheckReaderCount"]/*' />
  1692. protected void CheckReaderCount(ref int whileIterations, ref int readerCount)
  1693. {
  1694. if (checkDeserializeAdvances)
  1695. {
  1696. whileIterations++;
  1697. if ((whileIterations & 0x80) == 0x80)
  1698. {
  1699. if (readerCount == ReaderCount)
  1700. throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorReaderAdvance));
  1701. readerCount = ReaderCount;
  1702. }
  1703. }
  1704. }
  1705. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup"]/*' />
  1706. ///<internalonly/>
  1707. protected class Fixup {
  1708. XmlSerializationFixupCallback callback;
  1709. object source;
  1710. string[] ids;
  1711. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup.Fixup1"]/*' />
  1712. public Fixup(object o, XmlSerializationFixupCallback callback, int count)
  1713. : this (o, callback, new string[count]) {
  1714. }
  1715. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup.Fixup2"]/*' />
  1716. public Fixup(object o, XmlSerializationFixupCallback callback, string[] ids) {
  1717. this.callback = callback;
  1718. this.Source = o;
  1719. this.ids = ids;
  1720. }
  1721. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup.Callback"]/*' />
  1722. public XmlSerializationFixupCallback Callback {
  1723. get { return callback; }
  1724. }
  1725. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup.Source"]/*' />
  1726. public object Source {
  1727. get { return source; }
  1728. set { source = value; }
  1729. }
  1730. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="Fixup.Ids"]/*' />
  1731. public string[] Ids {
  1732. get { return ids; }
  1733. }
  1734. }
  1735. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="CollectionFixup"]/*' />
  1736. protected class CollectionFixup {
  1737. XmlSerializationCollectionFixupCallback callback;
  1738. object collection;
  1739. object collectionItems;
  1740. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="CollectionFixup.CollectionFixup"]/*' />
  1741. public CollectionFixup(object collection, XmlSerializationCollectionFixupCallback callback, object collectionItems) {
  1742. this.callback = callback;
  1743. this.collection = collection;
  1744. this.collectionItems = collectionItems;
  1745. }
  1746. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="CollectionFixup.Callback"]/*' />
  1747. public XmlSerializationCollectionFixupCallback Callback {
  1748. get { return callback; }
  1749. }
  1750. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="CollectionFixup.Collection"]/*' />
  1751. public object Collection {
  1752. get { return collection; }
  1753. }
  1754. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="CollectionFixup.CollectionItems"]/*' />
  1755. public object CollectionItems {
  1756. get { return collectionItems; }
  1757. }
  1758. }
  1759. }
  1760. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationFixupCallback"]/*' />
  1761. ///<internalonly/>
  1762. public delegate void XmlSerializationFixupCallback(object fixup);
  1763. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationCollectionFixupCallback"]/*' />
  1764. ///<internalonly/>
  1765. public delegate void XmlSerializationCollectionFixupCallback(object collection, object collectionItems);
  1766. /// <include file='doc\XmlSerializationReader.uex' path='docs/doc[@for="XmlSerializationReadCallback"]/*' />
  1767. ///<internalonly/>
  1768. public delegate object XmlSerializationReadCallback();
  1769. internal class XmlSerializationReaderCodeGen : XmlSerializationCodeGen {
  1770. Hashtable idNames = new Hashtable();
  1771. Hashtable enums;
  1772. Hashtable createMethods = new Hashtable();
  1773. int nextCreateMethodNumber = 0;
  1774. int nextIdNumber = 0;
  1775. int nextWhileLoopIndex = 0;
  1776. internal Hashtable Enums {
  1777. get {
  1778. if (enums == null) {
  1779. enums = new Hashtable();
  1780. }
  1781. return enums;
  1782. }
  1783. }
  1784. class CreateCollectionInfo {
  1785. string name;
  1786. TypeDesc td;
  1787. internal CreateCollectionInfo(string name, TypeDesc td) {
  1788. this.name = name;
  1789. this.td = td;
  1790. }
  1791. internal string Name {
  1792. get { return name; }
  1793. }
  1794. internal TypeDesc TypeDesc {
  1795. get { return td; }
  1796. }
  1797. }
  1798. class Member {
  1799. string source;
  1800. string arrayName;
  1801. string arraySource;
  1802. string choiceArrayName;
  1803. string choiceSource;
  1804. string choiceArraySource;
  1805. MemberMapping mapping;
  1806. bool isArray;
  1807. bool isList;
  1808. bool isNullable;
  1809. bool multiRef;
  1810. int fixupIndex = -1;
  1811. string paramsReadSource;
  1812. string checkSpecifiedSource;
  1813. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arrayName, int i, MemberMapping mapping)
  1814. : this(outerClass, source, null, arrayName, i, mapping, false, null) {
  1815. }
  1816. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arrayName, int i, MemberMapping mapping, string choiceSource)
  1817. : this(outerClass, source, null, arrayName, i, mapping, false, choiceSource) {
  1818. }
  1819. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping)
  1820. : this (outerClass, source, arraySource, arrayName, i, mapping, false, null) {
  1821. }
  1822. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, string choiceSource)
  1823. : this (outerClass, source, arraySource, arrayName, i, mapping, false, choiceSource) {
  1824. }
  1825. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arrayName, int i, MemberMapping mapping, bool multiRef)
  1826. : this(outerClass, source, null, arrayName, i, mapping, multiRef, null) {
  1827. }
  1828. internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, bool multiRef, string choiceSource) {
  1829. this.source = source;
  1830. this.arrayName = arrayName + "_" + i.ToString(CultureInfo.InvariantCulture);
  1831. this.choiceArrayName = "choice_" + this.arrayName;
  1832. this.choiceSource = choiceSource;
  1833. ElementAccessor[] elements = mapping.Elements;
  1834. if (mapping.TypeDesc.IsArrayLike) {
  1835. if (arraySource != null)
  1836. this.arraySource = arraySource;
  1837. else
  1838. this.arraySource = outerClass.GetArraySource(mapping.TypeDesc, this.arrayName, multiRef);
  1839. isArray = mapping.TypeDesc.IsArray;
  1840. isList = !isArray;
  1841. if (mapping.ChoiceIdentifier != null) {
  1842. this.choiceArraySource = outerClass.GetArraySource(mapping.TypeDesc, this.choiceArrayName, multiRef);
  1843. string a = choiceArrayName;
  1844. string c = "c" + a;
  1845. bool choiceUseReflection = mapping.ChoiceIdentifier.Mapping.TypeDesc.UseReflection;
  1846. string choiceTypeFullName = mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName;
  1847. string castString = choiceUseReflection?"":"(" + choiceTypeFullName + "[])";
  1848. string init = a + " = " + castString +
  1849. "EnsureArrayIndex(" + a + ", " + c + ", " + outerClass.RaCodeGen.GetStringForTypeof(choiceTypeFullName, choiceUseReflection) + ");";
  1850. this.choiceArraySource = init + outerClass.RaCodeGen.GetStringForArrayMember(a, c + "++", mapping.ChoiceIdentifier.Mapping.TypeDesc);
  1851. }
  1852. else {
  1853. this.choiceArraySource = this.choiceSource;
  1854. }
  1855. }
  1856. else {
  1857. this.arraySource = arraySource == null ? source : arraySource;
  1858. this.choiceArraySource = this.choiceSource;
  1859. }
  1860. this.mapping = mapping;
  1861. }
  1862. internal MemberMapping Mapping {
  1863. get { return mapping; }
  1864. }
  1865. internal string Source {
  1866. get { return source; }
  1867. }
  1868. internal string ArrayName {
  1869. get { return arrayName; }
  1870. }
  1871. internal string ArraySource {
  1872. get { return arraySource; }
  1873. }
  1874. internal bool IsList {
  1875. get { return isList; }
  1876. }
  1877. internal bool IsArrayLike {
  1878. get { return (isArray || isList); }
  1879. }
  1880. internal bool IsNullable {
  1881. get { return isNullable; }
  1882. set { isNullable = value; }
  1883. }
  1884. internal bool MultiRef {
  1885. get { return multiRef; }
  1886. set { multiRef = value; }
  1887. }
  1888. internal int FixupIndex {
  1889. get { return fixupIndex; }
  1890. set { fixupIndex = value; }
  1891. }
  1892. internal string ParamsReadSource {
  1893. get { return paramsReadSource; }
  1894. set { paramsReadSource = value; }
  1895. }
  1896. internal string CheckSpecifiedSource {
  1897. get { return checkSpecifiedSource; }
  1898. set { checkSpecifiedSource = value; }
  1899. }
  1900. internal string ChoiceSource {
  1901. get { return choiceSource; }
  1902. }
  1903. internal string ChoiceArrayName {
  1904. get { return choiceArrayName; }
  1905. }
  1906. internal string ChoiceArraySource {
  1907. get { return choiceArraySource; }
  1908. }
  1909. }
  1910. internal XmlSerializationReaderCodeGen(IndentedWriter writer, TypeScope[] scopes, string access, string className) : base(writer, scopes, access, className) {
  1911. }
  1912. internal void GenerateBegin() {
  1913. Writer.Write(Access);
  1914. Writer.Write(" class ");
  1915. Writer.Write(ClassName);
  1916. Writer.Write(" : ");
  1917. Writer.Write(typeof(XmlSerializationReader).FullName);
  1918. Writer.WriteLine(" {");
  1919. Writer.Indent++;
  1920. foreach (TypeScope scope in Scopes) {
  1921. foreach (TypeMapping mapping in scope.TypeMappings) {
  1922. if (mapping is StructMapping || mapping is EnumMapping || mapping is NullableMapping)
  1923. MethodNames.Add(mapping, NextMethodName(mapping.TypeDesc.Name));
  1924. }
  1925. RaCodeGen.WriteReflectionInit(scope);
  1926. }
  1927. // pre-generate read methods only for the encoded soap
  1928. foreach (TypeScope scope in Scopes) {
  1929. foreach (TypeMapping mapping in scope.TypeMappings) {
  1930. if (!mapping.IsSoap)
  1931. continue;
  1932. if (mapping is StructMapping)
  1933. WriteStructMethod((StructMapping)mapping);
  1934. else if (mapping is EnumMapping)
  1935. WriteEnumMethod((EnumMapping)mapping);
  1936. else if (mapping is NullableMapping) {
  1937. WriteNullableMethod((NullableMapping)mapping);
  1938. }
  1939. }
  1940. }
  1941. }
  1942. internal override void GenerateMethod(TypeMapping mapping) {
  1943. if (GeneratedMethods.Contains(mapping))
  1944. return;
  1945. GeneratedMethods[mapping] = mapping;
  1946. if (mapping is StructMapping) {
  1947. WriteStructMethod((StructMapping)mapping);
  1948. }
  1949. else if (mapping is EnumMapping) {
  1950. WriteEnumMethod((EnumMapping)mapping);
  1951. }
  1952. else if (mapping is NullableMapping) {
  1953. WriteNullableMethod((NullableMapping)mapping);
  1954. }
  1955. }
  1956. internal void GenerateEnd() {
  1957. GenerateEnd(new string[0], new XmlMapping[0], new Type[0]);
  1958. }
  1959. internal void GenerateEnd(string[] methods, XmlMapping[] xmlMappings, Type[] types) {
  1960. GenerateReferencedMethods();
  1961. GenerateInitCallbacksMethod();
  1962. foreach (CreateCollectionInfo c in createMethods.Values) {
  1963. WriteCreateCollectionMethod(c);
  1964. }
  1965. Writer.WriteLine();
  1966. foreach (string idName in idNames.Values) {
  1967. Writer.Write("string ");
  1968. Writer.Write(idName);
  1969. Writer.WriteLine(";");
  1970. }
  1971. Writer.WriteLine();
  1972. Writer.WriteLine("protected override void InitIDs() {");
  1973. Writer.Indent++;
  1974. foreach (string id in idNames.Keys) {
  1975. //
  1976. string idName = (string)idNames[id];
  1977. Writer.Write(idName);
  1978. Writer.Write(" = Reader.NameTable.Add(");
  1979. WriteQuotedCSharpString(id);
  1980. Writer.WriteLine(");");
  1981. }
  1982. Writer.Indent--;
  1983. Writer.WriteLine("}");
  1984. Writer.Indent--;
  1985. Writer.WriteLine("}");
  1986. }
  1987. internal string GenerateElement(XmlMapping xmlMapping) {
  1988. if (!xmlMapping.IsReadable)
  1989. return null;
  1990. if (!xmlMapping.GenerateSerializer)
  1991. throw new ArgumentException(Res.GetString(Res.XmlInternalError), "xmlMapping");
  1992. if (xmlMapping is XmlTypeMapping)
  1993. return GenerateTypeElement((XmlTypeMapping)xmlMapping);
  1994. else if (xmlMapping is XmlMembersMapping)
  1995. return GenerateMembersElement((XmlMembersMapping)xmlMapping);
  1996. else
  1997. throw new ArgumentException(Res.GetString(Res.XmlInternalError), "xmlMapping");
  1998. }
  1999. void WriteIsStartTag(string name, string ns) {
  2000. Writer.Write("if (Reader.IsStartElement(");
  2001. WriteID(name);
  2002. Writer.Write(", ");
  2003. WriteID(ns);
  2004. Writer.WriteLine(")) {");
  2005. Writer.Indent++;
  2006. }
  2007. void WriteUnknownNode(string func, string node, ElementAccessor e, bool anyIfs) {
  2008. if (anyIfs) {
  2009. Writer.WriteLine("else {");
  2010. Writer.Indent++;
  2011. }
  2012. Writer.Write(func);
  2013. Writer.Write("(");
  2014. Writer.Write(node);
  2015. if (e != null) {
  2016. Writer.Write(", ");
  2017. string expectedElement = e.Form == XmlSchemaForm.Qualified ? e.Namespace : "";
  2018. expectedElement += ":";
  2019. expectedElement += e.Name;
  2020. ReflectionAwareCodeGen.WriteQuotedCSharpString(Writer, expectedElement);
  2021. }
  2022. Writer.WriteLine(");");
  2023. if (anyIfs) {
  2024. Writer.Indent--;
  2025. Writer.WriteLine("}");
  2026. }
  2027. }
  2028. void GenerateInitCallbacksMethod() {
  2029. Writer.WriteLine();
  2030. Writer.WriteLine("protected override void InitCallbacks() {");
  2031. Writer.Indent++;
  2032. string dummyArrayMethodName = NextMethodName("Array");
  2033. bool needDummyArrayMethod = false;
  2034. foreach (TypeScope scope in Scopes) {
  2035. foreach (TypeMapping mapping in scope.TypeMappings) {
  2036. if (mapping.IsSoap &&
  2037. (mapping is StructMapping || mapping is EnumMapping || mapping is ArrayMapping || mapping is NullableMapping) &&
  2038. !mapping.TypeDesc.IsRoot) {
  2039. string methodName;
  2040. if (mapping is ArrayMapping) {
  2041. methodName = dummyArrayMethodName;
  2042. needDummyArrayMethod = true;
  2043. }
  2044. else
  2045. methodName = (string)MethodNames[mapping];
  2046. Writer.Write("AddReadCallback(");
  2047. WriteID(mapping.TypeName);
  2048. Writer.Write(", ");
  2049. WriteID(mapping.Namespace);
  2050. Writer.Write(", ");
  2051. Writer.Write(RaCodeGen.GetStringForTypeof(mapping.TypeDesc.CSharpName,mapping.TypeDesc.UseReflection));
  2052. Writer.Write(", new ");
  2053. Writer.Write(typeof(XmlSerializationReadCallback).FullName);
  2054. Writer.Write("(this.");
  2055. Writer.Write(methodName);
  2056. Writer.WriteLine("));");
  2057. }
  2058. }
  2059. }
  2060. Writer.Indent--;
  2061. Writer.WriteLine("}");
  2062. if (needDummyArrayMethod) {
  2063. Writer.WriteLine();
  2064. Writer.Write("object ");
  2065. Writer.Write(dummyArrayMethodName);
  2066. Writer.WriteLine("() {");
  2067. Writer.Indent++;
  2068. Writer.WriteLine("// dummy array method");
  2069. Writer.WriteLine("UnknownNode(null);");
  2070. Writer.WriteLine("return null;");
  2071. Writer.Indent--;
  2072. Writer.WriteLine("}");
  2073. }
  2074. }
  2075. string GenerateMembersElement(XmlMembersMapping xmlMembersMapping) {
  2076. if (xmlMembersMapping.Accessor.IsSoap)
  2077. return GenerateEncodedMembersElement(xmlMembersMapping);
  2078. else
  2079. return GenerateLiteralMembersElement(xmlMembersMapping);
  2080. }
  2081. string GetChoiceIdentifierSource(MemberMapping[] mappings, MemberMapping member) {
  2082. string choiceSource = null;
  2083. if (member.ChoiceIdentifier != null) {
  2084. for (int j = 0; j < mappings.Length; j++) {
  2085. if (mappings[j].Name == member.ChoiceIdentifier.MemberName) {
  2086. choiceSource = "p[" + j.ToString(CultureInfo.InvariantCulture) + "]";
  2087. break;
  2088. }
  2089. }
  2090. #if DEBUG
  2091. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  2092. if (choiceSource == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Can not find " + member.ChoiceIdentifier.MemberName + " in the members mapping."));
  2093. #endif
  2094. }
  2095. return choiceSource;
  2096. }
  2097. string GetChoiceIdentifierSource(MemberMapping mapping, string parent, TypeDesc parentTypeDesc) {
  2098. if (mapping.ChoiceIdentifier == null) return "";
  2099. CodeIdentifier.CheckValidIdentifier(mapping.ChoiceIdentifier.MemberName);
  2100. return RaCodeGen.GetStringForMember(parent, mapping.ChoiceIdentifier.MemberName, parentTypeDesc);
  2101. }
  2102. string GenerateLiteralMembersElement(XmlMembersMapping xmlMembersMapping) {
  2103. ElementAccessor element = xmlMembersMapping.Accessor;
  2104. MemberMapping[] mappings = ((MembersMapping)element.Mapping).Members;
  2105. bool hasWrapperElement = ((MembersMapping)element.Mapping).HasWrapperElement;
  2106. string methodName = NextMethodName(element.Name);
  2107. Writer.WriteLine();
  2108. Writer.Write("public object[] ");
  2109. Writer.Write(methodName);
  2110. Writer.WriteLine("() {");
  2111. Writer.Indent++;
  2112. Writer.WriteLine("Reader.MoveToContent();");
  2113. Writer.Write("object[] p = new object[");
  2114. Writer.Write(mappings.Length.ToString(CultureInfo.InvariantCulture));
  2115. Writer.WriteLine("];");
  2116. InitializeValueTypes("p", mappings);
  2117. int wrapperLoopIndex = 0;
  2118. if (hasWrapperElement) {
  2119. wrapperLoopIndex = WriteWhileNotLoopStart();
  2120. Writer.Indent++;
  2121. WriteIsStartTag(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : "");
  2122. }
  2123. Member anyText = null;
  2124. Member anyElement = null;
  2125. Member anyAttribute = null;
  2126. ArrayList membersList = new ArrayList();
  2127. ArrayList textOrArrayMembersList = new ArrayList();
  2128. ArrayList attributeMembersList = new ArrayList();
  2129. for (int i = 0; i < mappings.Length; i++) {
  2130. MemberMapping mapping = mappings[i];
  2131. string source = "p[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2132. string arraySource = source;
  2133. if (mapping.Xmlns != null) {
  2134. arraySource = "((" + mapping.TypeDesc.CSharpName + ")" + source + ")";
  2135. }
  2136. string choiceSource = GetChoiceIdentifierSource(mappings, mapping);
  2137. Member member = new Member(this, source, arraySource, "a", i, mapping, choiceSource);
  2138. Member anyMember = new Member(this, source, null, "a", i, mapping, choiceSource);
  2139. if (!mapping.IsSequence)
  2140. member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2141. if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite) {
  2142. string nameSpecified = mapping.Name + "Specified";
  2143. for (int j = 0; j < mappings.Length; j++) {
  2144. if (mappings[j].Name == nameSpecified) {
  2145. member.CheckSpecifiedSource = "p[" + j.ToString(CultureInfo.InvariantCulture) + "]";
  2146. break;
  2147. }
  2148. }
  2149. }
  2150. bool foundAnyElement = false;
  2151. if (mapping.Text != null) anyText = anyMember;
  2152. if (mapping.Attribute != null && mapping.Attribute.Any)
  2153. anyAttribute = anyMember;
  2154. if (mapping.Attribute != null || mapping.Xmlns != null)
  2155. attributeMembersList.Add(member);
  2156. else if (mapping.Text != null)
  2157. textOrArrayMembersList.Add(member);
  2158. if (!mapping.IsSequence) {
  2159. for (int j = 0; j < mapping.Elements.Length; j++) {
  2160. if (mapping.Elements[j].Any && mapping.Elements[j].Name.Length == 0) {
  2161. anyElement = anyMember;
  2162. if (mapping.Attribute == null && mapping.Text == null)
  2163. textOrArrayMembersList.Add(anyMember);
  2164. foundAnyElement = true;
  2165. break;
  2166. }
  2167. }
  2168. }
  2169. if (mapping.Attribute != null || mapping.Text != null || foundAnyElement)
  2170. membersList.Add(anyMember);
  2171. else if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping)) {
  2172. membersList.Add(anyMember);
  2173. textOrArrayMembersList.Add(anyMember);
  2174. }
  2175. else {
  2176. if (mapping.TypeDesc.IsArrayLike && !mapping.TypeDesc.IsArray)
  2177. member.ParamsReadSource = null; // collection
  2178. membersList.Add(member);
  2179. }
  2180. }
  2181. Member[] members = (Member[]) membersList.ToArray(typeof(Member));
  2182. Member[] textOrArrayMembers = (Member[]) textOrArrayMembersList.ToArray(typeof(Member));
  2183. if (members.Length > 0 && members[0].Mapping.IsReturnValue) Writer.WriteLine("IsReturnValue = true;");
  2184. WriteParamsRead(mappings.Length);
  2185. if (attributeMembersList.Count > 0) {
  2186. Member[] attributeMembers = (Member[]) attributeMembersList.ToArray(typeof(Member));
  2187. WriteMemberBegin(attributeMembers);
  2188. WriteAttributes(attributeMembers, anyAttribute, "UnknownNode", "(object)p");
  2189. WriteMemberEnd(attributeMembers);
  2190. Writer.WriteLine("Reader.MoveToElement();");
  2191. }
  2192. WriteMemberBegin(textOrArrayMembers);
  2193. if (hasWrapperElement) {
  2194. Writer.WriteLine("if (Reader.IsEmptyElement) { Reader.Skip(); Reader.MoveToContent(); continue; }");
  2195. Writer.WriteLine("Reader.ReadStartElement();");
  2196. }
  2197. if (IsSequence(members)) {
  2198. Writer.WriteLine("int state = 0;");
  2199. }
  2200. int loopIndex = WriteWhileNotLoopStart();
  2201. Writer.Indent++;
  2202. string unknownNode = "UnknownNode((object)p, " + ExpectedElements(members) + ");";
  2203. WriteMemberElements(members, unknownNode, unknownNode, anyElement, anyText, null);
  2204. Writer.WriteLine("Reader.MoveToContent();");
  2205. WriteWhileLoopEnd(loopIndex);
  2206. WriteMemberEnd(textOrArrayMembers);
  2207. if (hasWrapperElement) {
  2208. Writer.WriteLine("ReadEndElement();");
  2209. Writer.Indent--;
  2210. Writer.WriteLine("}");
  2211. WriteUnknownNode("UnknownNode", "null", element, true);
  2212. Writer.WriteLine("Reader.MoveToContent();");
  2213. WriteWhileLoopEnd(wrapperLoopIndex);
  2214. }
  2215. Writer.WriteLine("return p;");
  2216. Writer.Indent--;
  2217. Writer.WriteLine("}");
  2218. return methodName;
  2219. }
  2220. void InitializeValueTypes(string arrayName, MemberMapping[] mappings) {
  2221. for (int i = 0; i < mappings.Length; i++) {
  2222. if (!mappings[i].TypeDesc.IsValueType)
  2223. continue;
  2224. Writer.Write(arrayName);
  2225. Writer.Write("[");
  2226. Writer.Write(i.ToString(CultureInfo.InvariantCulture));
  2227. Writer.Write("] = ");
  2228. if (mappings[i].TypeDesc.IsOptionalValue && mappings[i].TypeDesc.BaseTypeDesc.UseReflection) {
  2229. Writer.Write("null");
  2230. }
  2231. else {
  2232. Writer.Write(RaCodeGen.GetStringForCreateInstance(mappings[i].TypeDesc.CSharpName, mappings[i].TypeDesc.UseReflection, false, false));
  2233. }
  2234. Writer.WriteLine(";");
  2235. }
  2236. }
  2237. string GenerateEncodedMembersElement(XmlMembersMapping xmlMembersMapping) {
  2238. ElementAccessor element = xmlMembersMapping.Accessor;
  2239. MembersMapping membersMapping = (MembersMapping)element.Mapping;
  2240. MemberMapping[] mappings = membersMapping.Members;
  2241. bool hasWrapperElement = membersMapping.HasWrapperElement;
  2242. bool writeAccessors = membersMapping.WriteAccessors;
  2243. string methodName = NextMethodName(element.Name);
  2244. Writer.WriteLine();
  2245. Writer.Write("public object[] ");
  2246. Writer.Write(methodName);
  2247. Writer.WriteLine("() {");
  2248. Writer.Indent++;
  2249. Writer.WriteLine("Reader.MoveToContent();");
  2250. Writer.Write("object[] p = new object[");
  2251. Writer.Write(mappings.Length.ToString(CultureInfo.InvariantCulture));
  2252. Writer.WriteLine("];");
  2253. InitializeValueTypes("p", mappings);
  2254. if (hasWrapperElement) {
  2255. WriteReadNonRoots();
  2256. if (membersMapping.ValidateRpcWrapperElement) {
  2257. Writer.Write("if (!");
  2258. WriteXmlNodeEqual("Reader", element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : "");
  2259. Writer.WriteLine(") throw CreateUnknownNodeException();");
  2260. }
  2261. Writer.WriteLine("bool isEmptyWrapper = Reader.IsEmptyElement;");
  2262. Writer.WriteLine("Reader.ReadStartElement();");
  2263. }
  2264. Member[] members = new Member[mappings.Length];
  2265. for (int i = 0; i < mappings.Length; i++) {
  2266. MemberMapping mapping = mappings[i];
  2267. string source = "p[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2268. string arraySource = source;
  2269. if (mapping.Xmlns != null) {
  2270. arraySource = "((" + mapping.TypeDesc.CSharpName + ")" + source + ")";
  2271. }
  2272. Member member = new Member(this,source, arraySource, "a", i, mapping);
  2273. if (!mapping.IsSequence)
  2274. member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2275. members[i] = member;
  2276. if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite) {
  2277. string nameSpecified = mapping.Name + "Specified";
  2278. for (int j = 0; j < mappings.Length; j++) {
  2279. if (mappings[j].Name == nameSpecified) {
  2280. member.CheckSpecifiedSource = "p[" + j.ToString(CultureInfo.InvariantCulture) + "]";
  2281. break;
  2282. }
  2283. }
  2284. }
  2285. }
  2286. string fixupMethodName = "fixup_" + methodName;
  2287. bool anyFixups = WriteMemberFixupBegin(members, fixupMethodName, "p");
  2288. if (members.Length > 0 && members[0].Mapping.IsReturnValue) Writer.WriteLine("IsReturnValue = true;");
  2289. string checkTypeHrefSource = (!hasWrapperElement && !writeAccessors) ? "hrefList" : null;
  2290. if (checkTypeHrefSource != null)
  2291. WriteInitCheckTypeHrefList(checkTypeHrefSource);
  2292. WriteParamsRead(mappings.Length);
  2293. int loopIndex = WriteWhileNotLoopStart();
  2294. Writer.Indent++;
  2295. string unrecognizedElementSource = checkTypeHrefSource == null ? "UnknownNode((object)p);" : "if (Reader.GetAttribute(\"id\", null) != null) { ReadReferencedElement(); } else { UnknownNode((object)p); }";
  2296. WriteMemberElements(members, unrecognizedElementSource, "UnknownNode((object)p);", null, null, checkTypeHrefSource);
  2297. Writer.WriteLine("Reader.MoveToContent();");
  2298. WriteWhileLoopEnd(loopIndex);
  2299. if (hasWrapperElement)
  2300. Writer.WriteLine("if (!isEmptyWrapper) ReadEndElement();");
  2301. if (checkTypeHrefSource != null)
  2302. WriteHandleHrefList(members, checkTypeHrefSource);
  2303. Writer.WriteLine("ReadReferencedElements();");
  2304. Writer.WriteLine("return p;");
  2305. Writer.Indent--;
  2306. Writer.WriteLine("}");
  2307. if (anyFixups) WriteFixupMethod(fixupMethodName, members, "object[]", false, false, "p");
  2308. return methodName;
  2309. }
  2310. void WriteCreateCollection(TypeDesc td, string source) {
  2311. bool useReflection = td.UseReflection;
  2312. string item = (td.ArrayElementTypeDesc == null ? "object" : td.ArrayElementTypeDesc.CSharpName) + "[]";
  2313. bool arrayElementUseReflection = td.ArrayElementTypeDesc == null?false:td.ArrayElementTypeDesc.UseReflection;
  2314. //cannot call WriteArrayLocalDecl since 'ci' is always
  2315. //array and 'td' corresponds to 'c'
  2316. if (arrayElementUseReflection)
  2317. item = typeof(Array).FullName;
  2318. Writer.Write(item);
  2319. Writer.Write(" ");
  2320. Writer.Write("ci =");
  2321. Writer.Write("("+item+")");
  2322. Writer.Write(source);
  2323. Writer.WriteLine(";");
  2324. Writer.WriteLine("for (int i = 0; i < ci.Length; i++) {");
  2325. Writer.Indent++;
  2326. Writer.Write(RaCodeGen.GetStringForMethod("c", td.CSharpName,"Add",useReflection));
  2327. //cannot call GetStringForArrayMember since 'ci' is always
  2328. //array and 'td' corresponds to 'c'
  2329. if (!arrayElementUseReflection)
  2330. Writer.Write( "ci[i]");
  2331. else
  2332. Writer.Write(RaCodeGen.GetReflectionVariable(typeof(Array).FullName, "0") + "[ci , i]");
  2333. if (useReflection) Writer.WriteLine("}");
  2334. Writer.WriteLine(");");
  2335. Writer.Indent--;
  2336. Writer.WriteLine("}");
  2337. }
  2338. string GenerateTypeElement(XmlTypeMapping xmlTypeMapping) {
  2339. ElementAccessor element = xmlTypeMapping.Accessor;
  2340. TypeMapping mapping = element.Mapping;
  2341. string methodName = NextMethodName(element.Name);
  2342. Writer.WriteLine();
  2343. Writer.Write("public object ");
  2344. Writer.Write(methodName);
  2345. Writer.WriteLine("() {");
  2346. Writer.Indent++;
  2347. Writer.WriteLine("object o = null;");
  2348. MemberMapping member = new MemberMapping();
  2349. member.TypeDesc = mapping.TypeDesc;
  2350. //member.ReadOnly = !mapping.TypeDesc.HasDefaultConstructor;
  2351. member.Elements = new ElementAccessor[] { element };
  2352. Member[] members = new Member[] { new Member(this,"o", "o", "a", 0, member) };
  2353. Writer.WriteLine("Reader.MoveToContent();");
  2354. string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");";
  2355. WriteMemberElements(members, "throw CreateUnknownNodeException();", unknownNode, element.Any ? members[0] : null, null, null);
  2356. if (element.IsSoap) {
  2357. Writer.WriteLine("Referenced(o);");
  2358. Writer.WriteLine("ReadReferencedElements();");
  2359. }
  2360. Writer.WriteLine("return (object)o;");
  2361. Writer.Indent--;
  2362. Writer.WriteLine("}");
  2363. return methodName;
  2364. }
  2365. string NextMethodName(string name) {
  2366. return "Read" + (++NextMethodNumber).ToString(CultureInfo.InvariantCulture) + "_" + CodeIdentifier.MakeValidInternal(name);
  2367. }
  2368. string NextIdName(string name) {
  2369. return "id" + (++nextIdNumber).ToString(CultureInfo.InvariantCulture) + "_" + CodeIdentifier.MakeValidInternal(name);
  2370. }
  2371. void WritePrimitive(TypeMapping mapping, string source) {
  2372. if (mapping is EnumMapping) {
  2373. string enumMethodName = ReferenceMapping(mapping);
  2374. if (enumMethodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlMissingMethodEnum, mapping.TypeDesc.Name));
  2375. if (mapping.IsSoap) {
  2376. // SOAP methods are not strongly-typed (the return object), so we need to add a cast
  2377. Writer.Write("(");
  2378. Writer.Write(mapping.TypeDesc.CSharpName);
  2379. Writer.Write(")");
  2380. }
  2381. Writer.Write(enumMethodName);
  2382. Writer.Write("(");
  2383. if (!mapping.IsSoap) Writer.Write(source);
  2384. Writer.Write(")");
  2385. }
  2386. else if (mapping.TypeDesc == StringTypeDesc) {
  2387. Writer.Write(source);
  2388. }
  2389. else if (mapping.TypeDesc.FormatterName == "String") {
  2390. if (mapping.TypeDesc.CollapseWhitespace) {
  2391. Writer.Write("CollapseWhitespace(");
  2392. Writer.Write(source);
  2393. Writer.Write(")");
  2394. }
  2395. else {
  2396. Writer.Write(source);
  2397. }
  2398. }
  2399. else {
  2400. if (!mapping.TypeDesc.HasCustomFormatter) {
  2401. Writer.Write(typeof(XmlConvert).FullName);
  2402. Writer.Write(".");
  2403. }
  2404. Writer.Write("To");
  2405. Writer.Write(mapping.TypeDesc.FormatterName);
  2406. Writer.Write("(");
  2407. Writer.Write(source);
  2408. Writer.Write(")");
  2409. }
  2410. }
  2411. string MakeUnique(EnumMapping mapping, string name) {
  2412. string uniqueName = name;
  2413. object m = Enums[uniqueName];
  2414. if (m != null) {
  2415. if (m == mapping) {
  2416. // we already have created the hashtable
  2417. return null;
  2418. }
  2419. int i = 0;
  2420. while (m != null) {
  2421. i++;
  2422. uniqueName = name + i.ToString(CultureInfo.InvariantCulture);
  2423. m = Enums[uniqueName];
  2424. }
  2425. }
  2426. Enums.Add(uniqueName, mapping);
  2427. return uniqueName;
  2428. }
  2429. string WriteHashtable(EnumMapping mapping, string typeName) {
  2430. CodeIdentifier.CheckValidIdentifier(typeName);
  2431. string propName = MakeUnique(mapping, typeName + "Values");
  2432. if (propName == null) return CodeIdentifier.GetCSharpName(typeName);
  2433. string memberName = MakeUnique(mapping, "_" + propName);
  2434. propName = CodeIdentifier.GetCSharpName(propName);
  2435. Writer.WriteLine();
  2436. Writer.Write(typeof(Hashtable).FullName);
  2437. Writer.Write(" ");
  2438. Writer.Write(memberName);
  2439. Writer.WriteLine(";");
  2440. Writer.WriteLine();
  2441. Writer.Write("internal ");
  2442. Writer.Write(typeof(Hashtable).FullName);
  2443. Writer.Write(" ");
  2444. Writer.Write(propName);
  2445. Writer.WriteLine(" {");
  2446. Writer.Indent++;
  2447. Writer.WriteLine("get {");
  2448. Writer.Indent++;
  2449. Writer.Write("if ((object)");
  2450. Writer.Write(memberName);
  2451. Writer.WriteLine(" == null) {");
  2452. Writer.Indent++;
  2453. Writer.Write(typeof(Hashtable).FullName);
  2454. Writer.Write(" h = new ");
  2455. Writer.Write(typeof(Hashtable).FullName);
  2456. Writer.WriteLine("();");
  2457. ConstantMapping[] constants = mapping.Constants;
  2458. for (int i = 0; i < constants.Length; i++) {
  2459. Writer.Write("h.Add(");
  2460. WriteQuotedCSharpString(constants[i].XmlName);
  2461. if (!mapping.TypeDesc.UseReflection){
  2462. Writer.Write(", (long)");
  2463. Writer.Write(mapping.TypeDesc.CSharpName);
  2464. Writer.Write(".@");
  2465. CodeIdentifier.CheckValidIdentifier(constants[i].Name);
  2466. Writer.Write(constants[i].Name);
  2467. }
  2468. else{
  2469. Writer.Write(", ");
  2470. Writer.Write(constants[i].Value.ToString(CultureInfo.InvariantCulture)+"L");
  2471. }
  2472. Writer.WriteLine(");");
  2473. }
  2474. Writer.Write(memberName);
  2475. Writer.WriteLine(" = h;");
  2476. Writer.Indent--;
  2477. Writer.WriteLine("}");
  2478. Writer.Write("return ");
  2479. Writer.Write(memberName);
  2480. Writer.WriteLine(";");
  2481. Writer.Indent--;
  2482. Writer.WriteLine("}");
  2483. Writer.Indent--;
  2484. Writer.WriteLine("}");
  2485. return propName;
  2486. }
  2487. void WriteEnumMethod(EnumMapping mapping) {
  2488. string tableName = null;
  2489. if (mapping.IsFlags)
  2490. tableName = WriteHashtable(mapping, mapping.TypeDesc.Name);
  2491. string methodName = (string)MethodNames[mapping];
  2492. Writer.WriteLine();
  2493. bool useReflection = mapping.TypeDesc.UseReflection;
  2494. string fullTypeName = mapping.TypeDesc.CSharpName;
  2495. if (mapping.IsSoap) {
  2496. Writer.Write("object");
  2497. Writer.Write(" ");
  2498. Writer.Write(methodName);
  2499. Writer.WriteLine("() {");
  2500. Writer.Indent++;
  2501. Writer.WriteLine("string s = Reader.ReadElementString();");
  2502. }
  2503. else {
  2504. Writer.Write(useReflection?"object":fullTypeName);
  2505. Writer.Write(" ");
  2506. Writer.Write(methodName);
  2507. Writer.WriteLine("(string s) {");
  2508. Writer.Indent++;
  2509. }
  2510. ConstantMapping[] constants = mapping.Constants;
  2511. if (mapping.IsFlags) {
  2512. if (useReflection){
  2513. Writer.Write("return ");
  2514. Writer.Write(typeof(Enum).FullName);
  2515. Writer.Write(".ToObject(");
  2516. Writer.Write(RaCodeGen.GetStringForTypeof(fullTypeName, useReflection));
  2517. Writer.Write(", ToEnum(s, ");
  2518. Writer.Write(tableName);
  2519. Writer.Write(", ");
  2520. WriteQuotedCSharpString(fullTypeName);
  2521. Writer.WriteLine("));");
  2522. }
  2523. else{
  2524. Writer.Write("return (");
  2525. Writer.Write(fullTypeName);
  2526. Writer.Write(")ToEnum(s, ");
  2527. Writer.Write(tableName);
  2528. Writer.Write(", ");
  2529. WriteQuotedCSharpString(fullTypeName);
  2530. Writer.WriteLine(");");
  2531. }
  2532. }
  2533. else {
  2534. Writer.WriteLine("switch (s) {");
  2535. Writer.Indent++;
  2536. Hashtable cases = new Hashtable();
  2537. for (int i = 0; i < constants.Length; i++) {
  2538. ConstantMapping c = constants[i];
  2539. CodeIdentifier.CheckValidIdentifier(c.Name);
  2540. if (cases[c.XmlName] == null) {
  2541. Writer.Write("case ");
  2542. WriteQuotedCSharpString(c.XmlName);
  2543. Writer.Write(": return ");
  2544. Writer.Write(RaCodeGen.GetStringForEnumMember(fullTypeName, c.Name, useReflection));
  2545. Writer.WriteLine(";");
  2546. cases[c.XmlName] = c.XmlName;
  2547. }
  2548. }
  2549. Writer.Write("default: throw CreateUnknownConstantException(s, ");
  2550. Writer.Write(RaCodeGen.GetStringForTypeof(fullTypeName, useReflection));
  2551. Writer.WriteLine(");");
  2552. Writer.Indent--;
  2553. Writer.WriteLine("}");
  2554. }
  2555. Writer.Indent--;
  2556. Writer.WriteLine("}");
  2557. }
  2558. void WriteDerivedTypes(StructMapping mapping, bool isTypedReturn, string returnTypeName) {
  2559. for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
  2560. Writer.Write("else if (");
  2561. WriteQNameEqual("xsiType", derived.TypeName, derived.Namespace);
  2562. Writer.WriteLine(")");
  2563. Writer.Indent++;
  2564. string methodName = ReferenceMapping(derived);
  2565. #if DEBUG
  2566. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  2567. if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, derived.TypeDesc.Name));
  2568. #endif
  2569. Writer.Write("return ");
  2570. if (derived.TypeDesc.UseReflection && isTypedReturn)
  2571. Writer.Write("(" + returnTypeName + ")");
  2572. Writer.Write(methodName);
  2573. Writer.Write("(");
  2574. if (derived.TypeDesc.IsNullable)
  2575. Writer.Write("isNullable, ");
  2576. Writer.WriteLine("false);");
  2577. Writer.Indent--;
  2578. WriteDerivedTypes(derived, isTypedReturn, returnTypeName);
  2579. }
  2580. }
  2581. void WriteEnumAndArrayTypes() {
  2582. foreach (TypeScope scope in Scopes) {
  2583. foreach (Mapping m in scope.TypeMappings) {
  2584. if (m.IsSoap)
  2585. continue;
  2586. if (m is EnumMapping) {
  2587. EnumMapping mapping = (EnumMapping)m;
  2588. Writer.Write("else if (");
  2589. WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace);
  2590. Writer.WriteLine(") {");
  2591. Writer.Indent++;
  2592. Writer.WriteLine("Reader.ReadStartElement();");
  2593. string methodName = ReferenceMapping(mapping);
  2594. #if DEBUG
  2595. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  2596. if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name));
  2597. #endif
  2598. Writer.Write("object e = ");
  2599. Writer.Write(methodName);
  2600. Writer.WriteLine("(CollapseWhitespace(Reader.ReadString()));");
  2601. Writer.WriteLine("ReadEndElement();");
  2602. Writer.WriteLine("return e;");
  2603. Writer.Indent--;
  2604. Writer.WriteLine("}");
  2605. }
  2606. else if (m is ArrayMapping) {
  2607. ArrayMapping mapping = (ArrayMapping) m;
  2608. if (mapping.TypeDesc.HasDefaultConstructor) {
  2609. Writer.Write("else if (");
  2610. WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace);
  2611. Writer.WriteLine(") {");
  2612. Writer.Indent++;
  2613. MemberMapping memberMapping = new MemberMapping();
  2614. memberMapping.TypeDesc = mapping.TypeDesc;
  2615. memberMapping.Elements = mapping.Elements;
  2616. Member member = new Member(this,"a", "z", 0, memberMapping);
  2617. TypeDesc td = mapping.TypeDesc;
  2618. string fullTypeName = mapping.TypeDesc.CSharpName;
  2619. if (td.UseReflection){
  2620. if (td.IsArray)
  2621. Writer.Write(typeof(Array).FullName);
  2622. else
  2623. Writer.Write("object");
  2624. }
  2625. else
  2626. Writer.Write(fullTypeName);
  2627. Writer.Write(" a = ");
  2628. if (mapping.TypeDesc.IsValueType) {
  2629. Writer.Write(RaCodeGen.GetStringForCreateInstance(fullTypeName, td.UseReflection, false, false));
  2630. Writer.WriteLine(";");
  2631. }
  2632. else
  2633. Writer.WriteLine("null;");
  2634. WriteArray(member.Source, member.ArrayName, mapping, false, false, -1);
  2635. Writer.WriteLine("return a;");
  2636. Writer.Indent--;
  2637. Writer.WriteLine("}");
  2638. }
  2639. }
  2640. }
  2641. }
  2642. }
  2643. void WriteNullableMethod(NullableMapping nullableMapping) {
  2644. string methodName = (string)MethodNames[nullableMapping];
  2645. bool useReflection = nullableMapping.BaseMapping.TypeDesc.UseReflection;
  2646. string typeName = useReflection ? "object" : nullableMapping.TypeDesc.CSharpName;
  2647. Writer.WriteLine();
  2648. Writer.Write(typeName);
  2649. Writer.Write(" ");
  2650. Writer.Write(methodName);
  2651. Writer.WriteLine("(bool checkType) {");
  2652. Writer.Indent++;
  2653. Writer.Write(typeName);
  2654. Writer.Write(" o = ");
  2655. if (useReflection) {
  2656. Writer.Write("null");
  2657. }
  2658. else {
  2659. Writer.Write("default(");
  2660. Writer.Write(typeName);
  2661. Writer.Write(")");
  2662. }
  2663. Writer.WriteLine(";");
  2664. Writer.WriteLine("if (ReadNull())");
  2665. Writer.Indent++;
  2666. Writer.WriteLine("return o;");
  2667. Writer.Indent--;
  2668. ElementAccessor element = new ElementAccessor();
  2669. element.Mapping = nullableMapping.BaseMapping;
  2670. element.Any = false;
  2671. element.IsNullable = nullableMapping.BaseMapping.TypeDesc.IsNullable;
  2672. WriteElement("o", null, null, element, null, null, false, false, -1, -1);
  2673. Writer.WriteLine("return o;");
  2674. Writer.Indent--;
  2675. Writer.WriteLine("}");
  2676. }
  2677. void WriteStructMethod(StructMapping structMapping) {
  2678. if (structMapping.IsSoap)
  2679. WriteEncodedStructMethod(structMapping);
  2680. else
  2681. WriteLiteralStructMethod(structMapping);
  2682. }
  2683. void WriteLiteralStructMethod(StructMapping structMapping) {
  2684. string methodName = (string)MethodNames[structMapping];
  2685. bool useReflection = structMapping.TypeDesc.UseReflection;
  2686. string typeName = useReflection ? "object" : structMapping.TypeDesc.CSharpName;
  2687. Writer.WriteLine();
  2688. Writer.Write(typeName);
  2689. Writer.Write(" ");
  2690. Writer.Write(methodName);
  2691. Writer.Write("(");
  2692. if (structMapping.TypeDesc.IsNullable)
  2693. Writer.Write("bool isNullable, ");
  2694. Writer.WriteLine("bool checkType) {");
  2695. Writer.Indent++;
  2696. Writer.Write(typeof(XmlQualifiedName).FullName);
  2697. Writer.WriteLine(" xsiType = checkType ? GetXsiType() : null;");
  2698. Writer.WriteLine("bool isNull = false;");
  2699. if (structMapping.TypeDesc.IsNullable)
  2700. Writer.WriteLine("if (isNullable) isNull = ReadNull();");
  2701. Writer.WriteLine("if (checkType) {");
  2702. if (structMapping.TypeDesc.IsRoot) {
  2703. Writer.Indent++;
  2704. Writer.WriteLine("if (isNull) {");
  2705. Writer.Indent++;
  2706. Writer.WriteLine("if (xsiType != null) return (" + typeName + ")ReadTypedNull(xsiType);");
  2707. Writer.Write("else return ");
  2708. if (structMapping.TypeDesc.IsValueType) {
  2709. Writer.Write(RaCodeGen.GetStringForCreateInstance(structMapping.TypeDesc.CSharpName, useReflection, false, false));
  2710. Writer.WriteLine(";");
  2711. }
  2712. else
  2713. Writer.WriteLine("null;");
  2714. Writer.Indent--;
  2715. Writer.WriteLine("}");
  2716. }
  2717. Writer.Write("if (xsiType == null");
  2718. if (!structMapping.TypeDesc.IsRoot) {
  2719. Writer.Write(" || ");
  2720. WriteQNameEqual("xsiType", structMapping.TypeName, structMapping.Namespace);
  2721. }
  2722. Writer.WriteLine(") {");
  2723. if (structMapping.TypeDesc.IsRoot) {
  2724. Writer.Indent++;
  2725. Writer.WriteLine("return ReadTypedPrimitive(new System.Xml.XmlQualifiedName(\"" + Soap.UrType + "\", \"" + XmlSchema.Namespace + "\"));");
  2726. Writer.Indent--;
  2727. }
  2728. Writer.WriteLine("}");
  2729. WriteDerivedTypes(structMapping, !useReflection && !structMapping.TypeDesc.IsRoot, typeName);
  2730. if (structMapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes();
  2731. Writer.WriteLine("else");
  2732. Writer.Indent++;
  2733. if (structMapping.TypeDesc.IsRoot)
  2734. Writer.Write("return ReadTypedPrimitive((");
  2735. else
  2736. Writer.Write("throw CreateUnknownTypeException((");
  2737. Writer.Write(typeof(XmlQualifiedName).FullName);
  2738. Writer.WriteLine(")xsiType);");
  2739. Writer.Indent--;
  2740. Writer.WriteLine("}");
  2741. if (structMapping.TypeDesc.IsNullable)
  2742. Writer.WriteLine("if (isNull) return null;");
  2743. if (structMapping.TypeDesc.IsAbstract) {
  2744. Writer.Write("throw CreateAbstractTypeException(");
  2745. WriteQuotedCSharpString(structMapping.TypeName);
  2746. Writer.Write(", ");
  2747. WriteQuotedCSharpString(structMapping.Namespace);
  2748. Writer.WriteLine(");");
  2749. }
  2750. else {
  2751. if (structMapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(structMapping.TypeDesc.Type)) {
  2752. Writer.WriteLine("DecodeName = false;");
  2753. }
  2754. WriteCreateMapping(structMapping, "o");
  2755. MemberMapping[] mappings = TypeScope.GetSettableMembers(structMapping);
  2756. Member anyText = null;
  2757. Member anyElement = null;
  2758. Member anyAttribute = null;
  2759. bool isSequence = structMapping.HasExplicitSequence();
  2760. ArrayList arraysToDeclareList = new ArrayList(mappings.Length);
  2761. ArrayList arraysToSetList = new ArrayList(mappings.Length);
  2762. ArrayList allMembersList = new ArrayList(mappings.Length);
  2763. for (int i = 0; i < mappings.Length; i++) {
  2764. MemberMapping mapping = mappings[i];
  2765. CodeIdentifier.CheckValidIdentifier(mapping.Name);
  2766. string source = RaCodeGen.GetStringForMember("o", mapping.Name, structMapping.TypeDesc);
  2767. Member member = new Member(this, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
  2768. if (!mapping.IsSequence)
  2769. member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2770. member.IsNullable = mapping.TypeDesc.IsNullable;
  2771. if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite)
  2772. member.CheckSpecifiedSource = RaCodeGen.GetStringForMember("o", mapping.Name + "Specified", structMapping.TypeDesc);
  2773. if (mapping.Text != null)
  2774. anyText = member;
  2775. if (mapping.Attribute != null && mapping.Attribute.Any)
  2776. anyAttribute = member;
  2777. if (!isSequence) {
  2778. // find anyElement if present.
  2779. for (int j = 0; j < mapping.Elements.Length; j++) {
  2780. if (mapping.Elements[j].Any && (mapping.Elements[j].Name == null || mapping.Elements[j].Name.Length == 0)) {
  2781. anyElement = member;
  2782. break;
  2783. }
  2784. }
  2785. }
  2786. else if (mapping.IsParticle && !mapping.IsSequence) {
  2787. StructMapping declaringMapping;
  2788. structMapping.FindDeclaringMapping(mapping, out declaringMapping, structMapping.TypeName);
  2789. throw new InvalidOperationException(Res.GetString(Res.XmlSequenceHierarchy, structMapping.TypeDesc.FullName, mapping.Name, declaringMapping.TypeDesc.FullName, "Order"));
  2790. }
  2791. if (mapping.Attribute == null && mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping) {
  2792. Member arrayMember = new Member(this, source, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
  2793. arrayMember.CheckSpecifiedSource = member.CheckSpecifiedSource;
  2794. allMembersList.Add(arrayMember);
  2795. }
  2796. else {
  2797. allMembersList.Add(member);
  2798. }
  2799. if (mapping.TypeDesc.IsArrayLike) {
  2800. arraysToDeclareList.Add(member);
  2801. if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping)) {
  2802. member.ParamsReadSource = null; // flat arrays -- don't want to count params read.
  2803. if (member != anyText && member != anyElement) {
  2804. arraysToSetList.Add(member);
  2805. }
  2806. }
  2807. else if (!mapping.TypeDesc.IsArray) {
  2808. member.ParamsReadSource = null; // collection
  2809. }
  2810. }
  2811. }
  2812. if (anyElement != null) arraysToSetList.Add(anyElement);
  2813. if (anyText != null && anyText != anyElement) arraysToSetList.Add(anyText);
  2814. Member[] arraysToDeclare = (Member[]) arraysToDeclareList.ToArray(typeof(Member));
  2815. Member[] arraysToSet = (Member[]) arraysToSetList.ToArray(typeof(Member));
  2816. Member[] allMembers = (Member[]) allMembersList.ToArray(typeof(Member));
  2817. WriteMemberBegin(arraysToDeclare);
  2818. WriteParamsRead(mappings.Length);
  2819. WriteAttributes(allMembers, anyAttribute, "UnknownNode", "(object)o");
  2820. if (anyAttribute != null)
  2821. WriteMemberEnd(arraysToDeclare);
  2822. Writer.WriteLine("Reader.MoveToElement();");
  2823. Writer.WriteLine("if (Reader.IsEmptyElement) {");
  2824. Writer.Indent++;
  2825. Writer.WriteLine("Reader.Skip();");
  2826. WriteMemberEnd(arraysToSet);
  2827. Writer.WriteLine("return o;");
  2828. Writer.Indent--;
  2829. Writer.WriteLine("}");
  2830. Writer.WriteLine("Reader.ReadStartElement();");
  2831. if (IsSequence(allMembers)) {
  2832. Writer.WriteLine("int state = 0;");
  2833. }
  2834. int loopIndex = WriteWhileNotLoopStart();
  2835. Writer.Indent++;
  2836. string unknownNode = "UnknownNode((object)o, " + ExpectedElements(allMembers) + ");";
  2837. WriteMemberElements(allMembers, unknownNode, unknownNode, anyElement, anyText, null);
  2838. Writer.WriteLine("Reader.MoveToContent();");
  2839. WriteWhileLoopEnd(loopIndex);
  2840. WriteMemberEnd(arraysToSet);
  2841. Writer.WriteLine("ReadEndElement();");
  2842. Writer.WriteLine("return o;");
  2843. }
  2844. Writer.Indent--;
  2845. Writer.WriteLine("}");
  2846. }
  2847. void WriteEncodedStructMethod(StructMapping structMapping) {
  2848. if(structMapping.TypeDesc.IsRoot)
  2849. return;
  2850. bool useReflection = structMapping.TypeDesc.UseReflection;
  2851. string methodName = (string)MethodNames[structMapping];
  2852. Writer.WriteLine();
  2853. Writer.Write("object");
  2854. Writer.Write(" ");
  2855. Writer.Write(methodName);
  2856. Writer.Write("(");
  2857. Writer.WriteLine(") {");
  2858. Writer.Indent++;
  2859. Member[] members;
  2860. bool anyFixups;
  2861. string fixupMethodName;
  2862. if (structMapping.TypeDesc.IsAbstract) {
  2863. Writer.Write("throw CreateAbstractTypeException(");
  2864. WriteQuotedCSharpString(structMapping.TypeName);
  2865. Writer.Write(", ");
  2866. WriteQuotedCSharpString(structMapping.Namespace);
  2867. Writer.WriteLine(");");
  2868. members = new Member[0];
  2869. anyFixups = false;
  2870. fixupMethodName = null;
  2871. }
  2872. else {
  2873. WriteCreateMapping(structMapping, "o");
  2874. MemberMapping[] mappings = TypeScope.GetSettableMembers(structMapping);
  2875. members = new Member[mappings.Length];
  2876. for (int i = 0; i < mappings.Length; i++) {
  2877. MemberMapping mapping = mappings[i];
  2878. CodeIdentifier.CheckValidIdentifier(mapping.Name);
  2879. string source = RaCodeGen.GetStringForMember("o", mapping.Name, structMapping.TypeDesc);
  2880. Member member = new Member(this,source, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
  2881. if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite)
  2882. member.CheckSpecifiedSource = RaCodeGen.GetStringForMember("o", mapping.Name + "Specified", structMapping.TypeDesc);
  2883. if (!mapping.IsSequence)
  2884. member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
  2885. members[i] = member;
  2886. }
  2887. fixupMethodName = "fixup_" + methodName;
  2888. anyFixups = WriteMemberFixupBegin(members, fixupMethodName, "o");
  2889. // we're able to not do WriteMemberBegin here because we don't allow arrays as attributes
  2890. WriteParamsRead(mappings.Length);
  2891. WriteAttributes(members, null, "UnknownNode", "(object)o");
  2892. Writer.WriteLine("Reader.MoveToElement();");
  2893. Writer.WriteLine("if (Reader.IsEmptyElement) { Reader.Skip(); return o; }");
  2894. Writer.WriteLine("Reader.ReadStartElement();");
  2895. int loopIndex = WriteWhileNotLoopStart();
  2896. Writer.Indent++;
  2897. WriteMemberElements(members, "UnknownNode((object)o);", "UnknownNode((object)o);", null, null, null);
  2898. Writer.WriteLine("Reader.MoveToContent();");
  2899. WriteWhileLoopEnd(loopIndex);
  2900. Writer.WriteLine("ReadEndElement();");
  2901. Writer.WriteLine("return o;");
  2902. }
  2903. Writer.Indent--;
  2904. Writer.WriteLine("}");
  2905. if (anyFixups) WriteFixupMethod(fixupMethodName, members, structMapping.TypeDesc.CSharpName, structMapping.TypeDesc.UseReflection, true, "o");
  2906. }
  2907. void WriteFixupMethod(string fixupMethodName, Member[] members, string typeName, bool useReflection, bool typed, string source) {
  2908. Writer.WriteLine();
  2909. Writer.Write("void ");
  2910. Writer.Write(fixupMethodName);
  2911. Writer.WriteLine("(object objFixup) {");
  2912. Writer.Indent++;
  2913. Writer.WriteLine("Fixup fixup = (Fixup)objFixup;");
  2914. WriteLocalDecl(typeName, source, "fixup.Source", useReflection);
  2915. Writer.WriteLine("string[] ids = fixup.Ids;");
  2916. for (int i = 0; i < members.Length; i++) {
  2917. Member member = members[i];
  2918. if (member.MultiRef) {
  2919. string fixupIndex = member.FixupIndex.ToString(CultureInfo.InvariantCulture);
  2920. Writer.Write("if (ids[");
  2921. Writer.Write(fixupIndex);
  2922. Writer.WriteLine("] != null) {");
  2923. Writer.Indent++;
  2924. string memberSource = /*member.IsList ? source + ".Add(" :*/ member.ArraySource;
  2925. string targetSource = "GetTarget(ids[" + fixupIndex + "])";
  2926. TypeDesc td = member.Mapping.TypeDesc;
  2927. if (td.IsCollection || td.IsEnumerable) {
  2928. WriteAddCollectionFixup(td, member.Mapping.ReadOnly, memberSource, targetSource);
  2929. }
  2930. else {
  2931. if (typed) {
  2932. Writer.WriteLine("try {");
  2933. Writer.Indent++;
  2934. WriteSourceBeginTyped(memberSource, member.Mapping.TypeDesc);
  2935. }
  2936. else
  2937. WriteSourceBegin(memberSource);
  2938. Writer.Write(targetSource);
  2939. WriteSourceEnd(memberSource);
  2940. Writer.WriteLine(";");
  2941. if (member.Mapping.CheckSpecified == SpecifiedAccessor.ReadWrite && member.CheckSpecifiedSource != null && member.CheckSpecifiedSource.Length > 0) {
  2942. Writer.Write(member.CheckSpecifiedSource);
  2943. Writer.WriteLine(" = true;");
  2944. }
  2945. if (typed) {
  2946. WriteCatchCastException(member.Mapping.TypeDesc, targetSource, "ids[" + fixupIndex + "]");
  2947. }
  2948. }
  2949. Writer.Indent--;
  2950. Writer.WriteLine("}");
  2951. }
  2952. }
  2953. Writer.Indent--;
  2954. Writer.WriteLine("}");
  2955. }
  2956. void WriteAddCollectionFixup(TypeDesc typeDesc, bool readOnly, string memberSource, string targetSource) {
  2957. Writer.WriteLine("// get array of the collection items");
  2958. bool useReflection = typeDesc.UseReflection;
  2959. CreateCollectionInfo create = (CreateCollectionInfo)createMethods[typeDesc];
  2960. if (create == null) {
  2961. string createName = "create" + (++nextCreateMethodNumber).ToString(CultureInfo.InvariantCulture) + "_" + typeDesc.Name;
  2962. create = new CreateCollectionInfo(createName, typeDesc);
  2963. createMethods.Add(typeDesc, create);
  2964. }
  2965. Writer.Write("if ((object)(");
  2966. Writer.Write(memberSource);
  2967. Writer.WriteLine(") == null) {");
  2968. Writer.Indent++;
  2969. if (readOnly) {
  2970. Writer.Write("throw CreateReadOnlyCollectionException(");
  2971. WriteQuotedCSharpString(typeDesc.CSharpName);
  2972. Writer.WriteLine(");");
  2973. }
  2974. else {
  2975. Writer.Write(memberSource);
  2976. Writer.Write(" = ");
  2977. Writer.Write(RaCodeGen.GetStringForCreateInstance(typeDesc.CSharpName, typeDesc.UseReflection, typeDesc.CannotNew, true));
  2978. Writer.WriteLine(";");
  2979. }
  2980. Writer.Indent--;
  2981. Writer.WriteLine("}");
  2982. Writer.Write("CollectionFixup collectionFixup = new CollectionFixup(");
  2983. Writer.Write(memberSource);
  2984. Writer.Write(", ");
  2985. Writer.Write("new ");
  2986. Writer.Write(typeof(XmlSerializationCollectionFixupCallback).FullName);
  2987. Writer.Write("(this.");
  2988. Writer.Write(create.Name);
  2989. Writer.Write("), ");
  2990. Writer.Write(targetSource);
  2991. Writer.WriteLine(");");
  2992. Writer.WriteLine("AddFixup(collectionFixup);");
  2993. }
  2994. void WriteCreateCollectionMethod(CreateCollectionInfo c) {
  2995. Writer.Write("void ");
  2996. Writer.Write(c.Name);
  2997. Writer.WriteLine("(object collection, object collectionItems) {");
  2998. Writer.Indent++;
  2999. Writer.WriteLine("if (collectionItems == null) return;");
  3000. Writer.WriteLine("if (collection == null) return;");
  3001. TypeDesc td = c.TypeDesc;
  3002. bool useReflection = td.UseReflection;
  3003. string fullTypeName = td.CSharpName;
  3004. WriteLocalDecl(fullTypeName, "c", "collection", useReflection);
  3005. WriteCreateCollection(td, "collectionItems");
  3006. Writer.Indent--;
  3007. Writer.WriteLine("}");
  3008. }
  3009. void WriteQNameEqual(string source, string name, string ns) {
  3010. Writer.Write("((object) ((");
  3011. Writer.Write(typeof(XmlQualifiedName).FullName);
  3012. Writer.Write(")");
  3013. Writer.Write(source);
  3014. Writer.Write(").Name == (object)");
  3015. WriteID(name);
  3016. Writer.Write(" && (object) ((");
  3017. Writer.Write(typeof(XmlQualifiedName).FullName);
  3018. Writer.Write(")");
  3019. Writer.Write(source);
  3020. Writer.Write(").Namespace == (object)");
  3021. WriteID(ns);
  3022. Writer.Write(")");
  3023. }
  3024. void WriteXmlNodeEqual(string source, string name, string ns) {
  3025. Writer.Write("(");
  3026. if (name != null && name.Length > 0) {
  3027. Writer.Write("(object) ");
  3028. Writer.Write(source);
  3029. Writer.Write(".LocalName == (object)");
  3030. WriteID(name);
  3031. Writer.Write(" && ");
  3032. }
  3033. Writer.Write("(object) ");
  3034. Writer.Write(source);
  3035. Writer.Write(".NamespaceURI == (object)");
  3036. WriteID(ns);
  3037. Writer.Write(")");
  3038. }
  3039. void WriteID(string name) {
  3040. if (name == null) {
  3041. //Writer.Write("null");
  3042. //return;
  3043. name = "";
  3044. }
  3045. string idName = (string)idNames[name];
  3046. if (idName == null) {
  3047. idName = NextIdName(name);
  3048. idNames.Add(name, idName);
  3049. }
  3050. Writer.Write(idName);
  3051. }
  3052. void WriteAttributes(Member[] members, Member anyAttribute, string elseCall, string firstParam) {
  3053. int count = 0;
  3054. Member xmlnsMember = null;
  3055. ArrayList attributes = new ArrayList();
  3056. Writer.WriteLine("while (Reader.MoveToNextAttribute()) {");
  3057. Writer.Indent++;
  3058. for (int i = 0; i < members.Length; i++) {
  3059. Member member = (Member)members[i];
  3060. if (member.Mapping.Xmlns != null) {
  3061. xmlnsMember = member;
  3062. continue;
  3063. }
  3064. if (member.Mapping.Ignore)
  3065. continue;
  3066. AttributeAccessor attribute = member.Mapping.Attribute;
  3067. if (attribute == null) continue;
  3068. if (attribute.Any) continue;
  3069. attributes.Add(attribute);
  3070. if (count++ > 0)
  3071. Writer.Write("else ");
  3072. Writer.Write("if (");
  3073. if (member.ParamsReadSource != null) {
  3074. Writer.Write("!");
  3075. Writer.Write(member.ParamsReadSource);
  3076. Writer.Write(" && ");
  3077. }
  3078. if (attribute.IsSpecialXmlNamespace) {
  3079. WriteXmlNodeEqual("Reader", attribute.Name, XmlReservedNs.NsXml);
  3080. }
  3081. else
  3082. WriteXmlNodeEqual("Reader", attribute.Name, attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : "");
  3083. Writer.WriteLine(") {");
  3084. Writer.Indent++;
  3085. WriteAttribute(member);
  3086. Writer.Indent--;
  3087. Writer.WriteLine("}");
  3088. }
  3089. if (count > 0)
  3090. Writer.Write("else ");
  3091. if (xmlnsMember != null) {
  3092. Writer.WriteLine("if (IsXmlnsAttribute(Reader.Name)) {");
  3093. Writer.Indent++;
  3094. Writer.Write("if (");
  3095. Writer.Write(xmlnsMember.Source);
  3096. Writer.Write(" == null) ");
  3097. Writer.Write(xmlnsMember.Source);
  3098. Writer.Write(" = new ");
  3099. Writer.Write(xmlnsMember.Mapping.TypeDesc.CSharpName);
  3100. Writer.WriteLine("();");
  3101. //Writer.Write(xmlnsMember.ArraySource);
  3102. Writer.Write("(("+xmlnsMember.Mapping.TypeDesc.CSharpName+")"+ xmlnsMember.ArraySource+")");
  3103. Writer.WriteLine(".Add(Reader.Name.Length == 5 ? \"\" : Reader.LocalName, Reader.Value);");
  3104. Writer.Indent--;
  3105. Writer.WriteLine("}");
  3106. Writer.WriteLine("else {");
  3107. Writer.Indent++;
  3108. }
  3109. else {
  3110. Writer.WriteLine("if (!IsXmlnsAttribute(Reader.Name)) {");
  3111. Writer.Indent++;
  3112. }
  3113. if (anyAttribute != null) {
  3114. Writer.Write(typeof(XmlAttribute).FullName);
  3115. Writer.Write(" attr = ");
  3116. Writer.Write("(");
  3117. Writer.Write(typeof(XmlAttribute).FullName);
  3118. Writer.WriteLine(") Document.ReadNode(Reader);");
  3119. Writer.WriteLine("ParseWsdlArrayType(attr);");
  3120. WriteAttribute(anyAttribute);
  3121. }
  3122. else {
  3123. Writer.Write(elseCall);
  3124. Writer.Write("(");
  3125. Writer.Write(firstParam);
  3126. if (attributes.Count > 0) {
  3127. Writer.Write(", ");
  3128. string qnames = "";
  3129. for (int i = 0; i < attributes.Count; i++) {
  3130. AttributeAccessor attribute = (AttributeAccessor)attributes[i];
  3131. if (i > 0)
  3132. qnames += ", ";
  3133. qnames += attribute.IsSpecialXmlNamespace ? XmlReservedNs.NsXml : (attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : "") + ":" + attribute.Name;
  3134. }
  3135. WriteQuotedCSharpString(qnames);
  3136. }
  3137. Writer.WriteLine(");");
  3138. }
  3139. Writer.Indent--;
  3140. Writer.WriteLine("}");
  3141. Writer.Indent--;
  3142. Writer.WriteLine("}");
  3143. }
  3144. void WriteAttribute(Member member) {
  3145. AttributeAccessor attribute = member.Mapping.Attribute;
  3146. if (attribute.Mapping is SpecialMapping) {
  3147. SpecialMapping special = (SpecialMapping)attribute.Mapping;
  3148. if (special.TypeDesc.Kind == TypeKind.Attribute) {
  3149. WriteSourceBegin(member.ArraySource);
  3150. Writer.Write("attr");
  3151. WriteSourceEnd(member.ArraySource);
  3152. Writer.WriteLine(";");
  3153. }
  3154. else if (special.TypeDesc.CanBeAttributeValue) {
  3155. Writer.Write("if (attr is ");
  3156. Writer.Write(typeof(XmlAttribute).FullName);
  3157. Writer.WriteLine(") {");
  3158. Writer.Indent++;
  3159. WriteSourceBegin(member.ArraySource);
  3160. Writer.Write("(");
  3161. Writer.Write(typeof(XmlAttribute).FullName);
  3162. Writer.Write(")attr");
  3163. WriteSourceEnd(member.ArraySource);
  3164. Writer.WriteLine(";");
  3165. Writer.Indent--;
  3166. Writer.WriteLine("}");
  3167. }
  3168. else
  3169. throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
  3170. }
  3171. else {
  3172. if (attribute.IsList) {
  3173. Writer.WriteLine("string listValues = Reader.Value;");
  3174. Writer.WriteLine("string[] vals = listValues.Split(null);");
  3175. Writer.WriteLine("for (int i = 0; i < vals.Length; i++) {");
  3176. Writer.Indent++;
  3177. string attributeSource = GetArraySource(member.Mapping.TypeDesc, member.ArrayName);
  3178. WriteSourceBegin(attributeSource);
  3179. WritePrimitive(attribute.Mapping, "vals[i]");
  3180. WriteSourceEnd(attributeSource);
  3181. Writer.WriteLine(";");
  3182. Writer.Indent--;
  3183. Writer.WriteLine("}");
  3184. }
  3185. else {
  3186. WriteSourceBegin(member.ArraySource);
  3187. WritePrimitive(attribute.Mapping, attribute.IsList ? "vals[i]" : "Reader.Value");
  3188. WriteSourceEnd(member.ArraySource);
  3189. Writer.WriteLine(";");
  3190. }
  3191. }
  3192. if (member.Mapping.CheckSpecified == SpecifiedAccessor.ReadWrite && member.CheckSpecifiedSource != null && member.CheckSpecifiedSource.Length > 0) {
  3193. Writer.Write(member.CheckSpecifiedSource);
  3194. Writer.WriteLine(" = true;");
  3195. }
  3196. if (member.ParamsReadSource != null) {
  3197. Writer.Write(member.ParamsReadSource);
  3198. Writer.WriteLine(" = true;");
  3199. }
  3200. }
  3201. bool WriteMemberFixupBegin(Member[] members, string fixupMethodName, string source) {
  3202. int fixupCount = 0;
  3203. for (int i = 0; i < members.Length; i++) {
  3204. Member member = (Member)members[i];
  3205. if (member.Mapping.Elements.Length == 0)
  3206. continue;
  3207. TypeMapping mapping = member.Mapping.Elements[0].Mapping;
  3208. if (mapping is StructMapping || mapping is ArrayMapping || mapping is PrimitiveMapping || mapping is NullableMapping) {
  3209. member.MultiRef = true;
  3210. member.FixupIndex = fixupCount++;
  3211. }
  3212. }
  3213. if (fixupCount > 0) {
  3214. Writer.Write("Fixup fixup = new Fixup(");
  3215. Writer.Write(source);
  3216. Writer.Write(", ");
  3217. Writer.Write("new ");
  3218. Writer.Write(typeof(XmlSerializationFixupCallback).FullName);
  3219. Writer.Write("(this.");
  3220. Writer.Write(fixupMethodName);
  3221. Writer.Write("), ");
  3222. Writer.Write(fixupCount.ToString(CultureInfo.InvariantCulture));
  3223. Writer.WriteLine(");");
  3224. Writer.WriteLine("AddFixup(fixup);");
  3225. return true;
  3226. }
  3227. return false;
  3228. }
  3229. void WriteMemberBegin(Member[] members) {
  3230. for (int i = 0; i < members.Length; i++) {
  3231. Member member = (Member)members[i];
  3232. if (member.IsArrayLike) {
  3233. string a = member.ArrayName;
  3234. string c = "c" + a;
  3235. TypeDesc typeDesc = member.Mapping.TypeDesc;
  3236. string typeDescFullName = typeDesc.CSharpName;
  3237. if (member.Mapping.TypeDesc.IsArray) {
  3238. WriteArrayLocalDecl(typeDesc.CSharpName,
  3239. a, "null", typeDesc);
  3240. Writer.Write("int ");
  3241. Writer.Write(c);
  3242. Writer.WriteLine(" = 0;");
  3243. if (member.Mapping.ChoiceIdentifier != null) {
  3244. WriteArrayLocalDecl(member.Mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName+"[]",
  3245. member.ChoiceArrayName, "null",
  3246. member.Mapping.ChoiceIdentifier.Mapping.TypeDesc);
  3247. Writer.Write("int c");
  3248. Writer.Write(member.ChoiceArrayName);
  3249. Writer.WriteLine(" = 0;");
  3250. }
  3251. }
  3252. else {
  3253. bool useReflection = typeDesc.UseReflection;
  3254. if (member.Source[member.Source.Length - 1] == '(' || member.Source[member.Source.Length - 1] == '{') {
  3255. WriteCreateInstance(typeDescFullName, a, useReflection, typeDesc.CannotNew);
  3256. Writer.Write(member.Source);
  3257. Writer.Write(a);
  3258. if (member.Source[member.Source.Length - 1] == '{')
  3259. Writer.WriteLine("});");
  3260. else
  3261. Writer.WriteLine(");");
  3262. }
  3263. else {
  3264. if (member.IsList && !member.Mapping.ReadOnly && member.Mapping.TypeDesc.IsNullable) {
  3265. // we need to new the Collections and ArrayLists
  3266. Writer.Write("if ((object)(");
  3267. Writer.Write(member.Source);
  3268. Writer.Write(") == null) ");
  3269. if (!member.Mapping.TypeDesc.HasDefaultConstructor) {
  3270. Writer.Write("throw CreateReadOnlyCollectionException(");
  3271. WriteQuotedCSharpString(member.Mapping.TypeDesc.CSharpName);
  3272. Writer.WriteLine(");");
  3273. }
  3274. else {
  3275. Writer.Write(member.Source);
  3276. Writer.Write(" = ");
  3277. Writer.Write(RaCodeGen.GetStringForCreateInstance(typeDescFullName, useReflection, typeDesc.CannotNew, true));
  3278. Writer.WriteLine(";");
  3279. }
  3280. }
  3281. WriteLocalDecl(typeDescFullName, a, member.Source, useReflection);
  3282. }
  3283. }
  3284. }
  3285. }
  3286. }
  3287. string ExpectedElements(Member[] members) {
  3288. if (IsSequence(members))
  3289. return "null";
  3290. string qnames = string.Empty;
  3291. bool firstElement = true;
  3292. for (int i = 0; i < members.Length; i++) {
  3293. Member member = (Member)members[i];
  3294. if (member.Mapping.Xmlns != null)
  3295. continue;
  3296. if (member.Mapping.Ignore)
  3297. continue;
  3298. if (member.Mapping.IsText || member.Mapping.IsAttribute)
  3299. continue;
  3300. ElementAccessor[] elements = member.Mapping.Elements;
  3301. for (int j = 0; j < elements.Length; j++) {
  3302. ElementAccessor e = elements[j];
  3303. string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : "";
  3304. if (e.Any && (e.Name == null || e.Name.Length == 0)) continue;
  3305. if (!firstElement)
  3306. qnames += ", ";
  3307. qnames += ns + ":" + e.Name;
  3308. firstElement = false;
  3309. }
  3310. }
  3311. StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
  3312. ReflectionAwareCodeGen.WriteQuotedCSharpString(new IndentedWriter(writer, true), qnames);
  3313. return writer.ToString();
  3314. }
  3315. void WriteMemberElements(Member[] members, string elementElseString, string elseString, Member anyElement, Member anyText, string checkTypeHrefsSource) {
  3316. bool checkType = (checkTypeHrefsSource != null && checkTypeHrefsSource.Length > 0);
  3317. if (anyText != null) {
  3318. Writer.WriteLine("string tmp = null;");
  3319. }
  3320. Writer.Write("if (Reader.NodeType == ");
  3321. Writer.Write(typeof(XmlNodeType).FullName);
  3322. Writer.WriteLine(".Element) {");
  3323. Writer.Indent++;
  3324. if (checkType) {
  3325. WriteIfNotSoapRoot(elementElseString + " continue;");
  3326. WriteMemberElementsCheckType(checkTypeHrefsSource);
  3327. }
  3328. else {
  3329. WriteMemberElementsIf(members, anyElement, elementElseString, null);
  3330. }
  3331. Writer.Indent--;
  3332. Writer.WriteLine("}");
  3333. if (anyText != null)
  3334. WriteMemberText(anyText, elseString);
  3335. Writer.WriteLine("else {");
  3336. Writer.Indent++;
  3337. Writer.WriteLine(elseString);
  3338. Writer.Indent--;
  3339. Writer.WriteLine("}");
  3340. }
  3341. void WriteMemberText(Member anyText, string elseString) {
  3342. Writer.Write("else if (Reader.NodeType == ");
  3343. Writer.Write(typeof(XmlNodeType).FullName);
  3344. Writer.WriteLine(".Text || ");
  3345. Writer.Write("Reader.NodeType == ");
  3346. Writer.Write(typeof(XmlNodeType).FullName);
  3347. Writer.WriteLine(".CDATA || ");
  3348. Writer.Write("Reader.NodeType == ");
  3349. Writer.Write(typeof(XmlNodeType).FullName);
  3350. Writer.WriteLine(".Whitespace || ");
  3351. Writer.Write("Reader.NodeType == ");
  3352. Writer.Write(typeof(XmlNodeType).FullName);
  3353. Writer.WriteLine(".SignificantWhitespace) {");
  3354. Writer.Indent++;
  3355. if (anyText != null) {
  3356. WriteText(anyText);
  3357. }
  3358. else {
  3359. Writer.Write(elseString);
  3360. Writer.WriteLine(";");
  3361. }
  3362. Writer.Indent--;
  3363. Writer.WriteLine("}");
  3364. }
  3365. void WriteText(Member member) {
  3366. TextAccessor text = member.Mapping.Text;
  3367. if (text.Mapping is SpecialMapping) {
  3368. SpecialMapping special = (SpecialMapping)text.Mapping;
  3369. WriteSourceBeginTyped(member.ArraySource, special.TypeDesc);
  3370. switch (special.TypeDesc.Kind) {
  3371. case TypeKind.Node:
  3372. Writer.Write("Document.CreateTextNode(Reader.ReadString())");
  3373. break;
  3374. default:
  3375. throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
  3376. }
  3377. WriteSourceEnd(member.ArraySource);
  3378. }
  3379. else {
  3380. if (member.IsArrayLike) {
  3381. WriteSourceBegin(member.ArraySource);
  3382. if (text.Mapping.TypeDesc.CollapseWhitespace) {
  3383. Writer.Write("CollapseWhitespace(Reader.ReadString())");
  3384. }
  3385. else {
  3386. Writer.Write("Reader.ReadString()");
  3387. }
  3388. }
  3389. else {
  3390. if (text.Mapping.TypeDesc == StringTypeDesc || text.Mapping.TypeDesc.FormatterName == "String") {
  3391. Writer.Write("tmp = ReadString(tmp, ");
  3392. if (text.Mapping.TypeDesc.CollapseWhitespace)
  3393. Writer.WriteLine("true);");
  3394. else
  3395. Writer.WriteLine("false);");
  3396. WriteSourceBegin(member.ArraySource);
  3397. Writer.Write("tmp");
  3398. }
  3399. else {
  3400. WriteSourceBegin(member.ArraySource);
  3401. WritePrimitive(text.Mapping, "Reader.ReadString()");
  3402. }
  3403. }
  3404. WriteSourceEnd(member.ArraySource);
  3405. }
  3406. Writer.WriteLine(";");
  3407. }
  3408. void WriteMemberElementsCheckType(string checkTypeHrefsSource) {
  3409. Writer.WriteLine("string refElemId = null;");
  3410. Writer.WriteLine("object refElem = ReadReferencingElement(null, null, true, out refElemId);");
  3411. Writer.WriteLine("if (refElemId != null) {");
  3412. Writer.Indent++;
  3413. Writer.Write(checkTypeHrefsSource);
  3414. Writer.WriteLine(".Add(refElemId);");
  3415. Writer.Write(checkTypeHrefsSource);
  3416. Writer.WriteLine("IsObject.Add(false);");
  3417. Writer.Indent--;
  3418. Writer.WriteLine("}");
  3419. Writer.WriteLine("else if (refElem != null) {");
  3420. Writer.Indent++;
  3421. Writer.Write(checkTypeHrefsSource);
  3422. Writer.WriteLine(".Add(refElem);");
  3423. Writer.Write(checkTypeHrefsSource);
  3424. Writer.WriteLine("IsObject.Add(true);");
  3425. Writer.Indent--;
  3426. Writer.WriteLine("}");
  3427. }
  3428. void WriteMemberElementsElse(Member anyElement, string elementElseString) {
  3429. if (anyElement != null) {
  3430. ElementAccessor[] elements = anyElement.Mapping.Elements;
  3431. for (int i = 0; i < elements.Length; i++) {
  3432. ElementAccessor element = elements[i];
  3433. if (element.Any && element.Name.Length == 0) {
  3434. WriteElement(anyElement.ArraySource, anyElement.ArrayName, anyElement.ChoiceArraySource, element, anyElement.Mapping.ChoiceIdentifier, anyElement.Mapping.CheckSpecified == SpecifiedAccessor.ReadWrite ? anyElement.CheckSpecifiedSource : null, false, false, -1, i);
  3435. break;
  3436. }
  3437. }
  3438. }
  3439. else {
  3440. Writer.WriteLine(elementElseString);
  3441. }
  3442. }
  3443. bool IsSequence(Member[] members) {
  3444. for (int i = 0; i < members.Length; i++) {
  3445. if (members[i].Mapping.IsParticle && members[i].Mapping.IsSequence)
  3446. return true;
  3447. }
  3448. return false;
  3449. }
  3450. void WriteMemberElementsIf(Member[] members, Member anyElement, string elementElseString, string checkTypeSource) {
  3451. bool checkType = checkTypeSource != null && checkTypeSource.Length > 0;
  3452. //int count = checkType ? 1 : 0;
  3453. int count = 0;
  3454. bool isSequence = IsSequence(members);
  3455. if (isSequence) {
  3456. Writer.WriteLine("switch (state) {");
  3457. }
  3458. int cases = 0;
  3459. for (int i = 0; i < members.Length; i++) {
  3460. Member member = (Member)members[i];
  3461. if (member.Mapping.Xmlns != null)
  3462. continue;
  3463. if (member.Mapping.Ignore)
  3464. continue;
  3465. if (isSequence && (member.Mapping.IsText || member.Mapping.IsAttribute))
  3466. continue;
  3467. bool firstElement = true;
  3468. ChoiceIdentifierAccessor choice = member.Mapping.ChoiceIdentifier;
  3469. ElementAccessor[] elements = member.Mapping.Elements;
  3470. for (int j = 0; j < elements.Length; j++) {
  3471. ElementAccessor e = elements[j];
  3472. string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : "";
  3473. if (!isSequence && e.Any && (e.Name == null || e.Name.Length == 0)) continue;
  3474. if (!firstElement || (!isSequence && count > 0)) {
  3475. Writer.Write("else ");
  3476. }
  3477. else if (isSequence) {
  3478. Writer.Write("case ");
  3479. Writer.Write(cases.ToString(CultureInfo.InvariantCulture));
  3480. Writer.WriteLine(":");
  3481. Writer.Indent++;
  3482. }
  3483. count++;
  3484. firstElement = false;
  3485. Writer.Write("if (");
  3486. if (member.ParamsReadSource != null) {
  3487. Writer.Write("!");
  3488. Writer.Write(member.ParamsReadSource);
  3489. Writer.Write(" && ");
  3490. }
  3491. if (checkType) {
  3492. if (e.Mapping is NullableMapping) {
  3493. TypeDesc td = ((NullableMapping)e.Mapping).BaseMapping.TypeDesc;
  3494. Writer.Write(RaCodeGen.GetStringForTypeof(td.CSharpName, td.UseReflection));
  3495. }
  3496. else {
  3497. Writer.Write(RaCodeGen.GetStringForTypeof(e.Mapping.TypeDesc.CSharpName, e.Mapping.TypeDesc.UseReflection));
  3498. }
  3499. Writer.Write(".IsAssignableFrom(");
  3500. Writer.Write(checkTypeSource);
  3501. Writer.Write("Type)");
  3502. }
  3503. else {
  3504. if (member.Mapping.IsReturnValue)
  3505. Writer.Write("(IsReturnValue || ");
  3506. if (isSequence && e.Any && e.AnyNamespaces == null) {
  3507. Writer.Write("true");
  3508. }
  3509. else {
  3510. WriteXmlNodeEqual("Reader", e.Name, ns);
  3511. }
  3512. if (member.Mapping.IsReturnValue)
  3513. Writer.Write(")");
  3514. }
  3515. Writer.WriteLine(") {");
  3516. Writer.Indent++;
  3517. if (checkType) {
  3518. if (e.Mapping.TypeDesc.IsValueType || e.Mapping is NullableMapping) {
  3519. Writer.Write("if (");
  3520. Writer.Write(checkTypeSource);
  3521. Writer.WriteLine(" != null) {");
  3522. Writer.Indent++;
  3523. }
  3524. if (e.Mapping is NullableMapping) {
  3525. WriteSourceBegin(member.ArraySource);
  3526. TypeDesc td = ((NullableMapping)e.Mapping).BaseMapping.TypeDesc;
  3527. Writer.Write(RaCodeGen.GetStringForCreateInstance(e.Mapping.TypeDesc.CSharpName, e.Mapping.TypeDesc.UseReflection, false, true, "(" + td.CSharpName + ")" + checkTypeSource));
  3528. }
  3529. else {
  3530. WriteSourceBeginTyped(member.ArraySource, e.Mapping.TypeDesc);
  3531. Writer.Write(checkTypeSource);
  3532. }
  3533. WriteSourceEnd(member.ArraySource);
  3534. Writer.WriteLine(";");
  3535. if (e.Mapping.TypeDesc.IsValueType) {
  3536. Writer.Indent--;
  3537. Writer.WriteLine("}");
  3538. }
  3539. if (member.FixupIndex >= 0) {
  3540. Writer.Write("fixup.Ids[");
  3541. Writer.Write(member.FixupIndex.ToString(CultureInfo.InvariantCulture));
  3542. Writer.Write("] = ");
  3543. Writer.Write(checkTypeSource);
  3544. Writer.WriteLine("Id;");
  3545. }
  3546. }
  3547. else {
  3548. WriteElement(member.ArraySource, member.ArrayName, member.ChoiceArraySource, e, choice, member.Mapping.CheckSpecified == SpecifiedAccessor.ReadWrite ? member.CheckSpecifiedSource : null, member.IsList && member.Mapping.TypeDesc.IsNullable, member.Mapping.ReadOnly, member.FixupIndex, j);
  3549. }
  3550. if (member.Mapping.IsReturnValue)
  3551. Writer.WriteLine("IsReturnValue = false;");
  3552. if (member.ParamsReadSource != null) {
  3553. Writer.Write(member.ParamsReadSource);
  3554. Writer.WriteLine(" = true;");
  3555. }
  3556. Writer.Indent--;
  3557. Writer.WriteLine("}");
  3558. }
  3559. if (isSequence) {
  3560. if (member.IsArrayLike) {
  3561. Writer.WriteLine("else {");
  3562. Writer.Indent++;
  3563. }
  3564. cases++;
  3565. Writer.Write("state = ");
  3566. Writer.Write(cases.ToString(CultureInfo.InvariantCulture));
  3567. Writer.WriteLine(";");
  3568. if (member.IsArrayLike) {
  3569. Writer.Indent--;
  3570. Writer.WriteLine("}");
  3571. }
  3572. Writer.WriteLine("break;");
  3573. Writer.Indent--;
  3574. }
  3575. }
  3576. if (count > 0) {
  3577. if (isSequence)
  3578. Writer.WriteLine("default:");
  3579. else
  3580. Writer.WriteLine("else {");
  3581. Writer.Indent++;
  3582. }
  3583. WriteMemberElementsElse(anyElement, elementElseString);
  3584. if (count > 0) {
  3585. if (isSequence) {
  3586. Writer.WriteLine("break;");
  3587. }
  3588. Writer.Indent--;
  3589. Writer.WriteLine("}");
  3590. }
  3591. }
  3592. string GetArraySource(TypeDesc typeDesc, string arrayName) {
  3593. return GetArraySource(typeDesc, arrayName, false);
  3594. }
  3595. string GetArraySource(TypeDesc typeDesc, string arrayName, bool multiRef) {
  3596. string a = arrayName;
  3597. string c = "c" + a;
  3598. string init = "";
  3599. if (multiRef) {
  3600. init = "soap = (System.Object[])EnsureArrayIndex(soap, " + c + "+2, typeof(System.Object)); ";
  3601. }
  3602. bool useReflection = typeDesc.UseReflection;
  3603. if (typeDesc.IsArray) {
  3604. string arrayTypeFullName = typeDesc.ArrayElementTypeDesc.CSharpName;
  3605. bool arrayUseReflection = typeDesc.ArrayElementTypeDesc.UseReflection;
  3606. string castString = useReflection?"":"(" + arrayTypeFullName + "[])";
  3607. init = init + a + " = " + castString +
  3608. "EnsureArrayIndex(" + a + ", " + c + ", "+ RaCodeGen.GetStringForTypeof(arrayTypeFullName, arrayUseReflection) + ");";
  3609. string arraySource = RaCodeGen.GetStringForArrayMember(a, c+"++", typeDesc);
  3610. if (multiRef) {
  3611. init = init + " soap[1] = " + a + ";";
  3612. init = init + " if (ReadReference(out soap[" + c + "+2])) " + arraySource + " = null; else ";
  3613. }
  3614. return init + arraySource;
  3615. }
  3616. else {
  3617. return RaCodeGen.GetStringForMethod(arrayName,typeDesc.CSharpName,"Add",useReflection);
  3618. }
  3619. }
  3620. void WriteMemberEnd(Member[] members) {
  3621. WriteMemberEnd(members, false);
  3622. }
  3623. void WriteMemberEnd(Member[] members, bool soapRefs) {
  3624. for (int i = 0; i < members.Length; i++) {
  3625. Member member = (Member)members[i];
  3626. if (member.IsArrayLike) {
  3627. TypeDesc typeDesc = member.Mapping.TypeDesc;
  3628. if (typeDesc.IsArray) {
  3629. WriteSourceBegin(member.Source);
  3630. if (soapRefs)
  3631. Writer.Write(" soap[1] = ");
  3632. string a = member.ArrayName;
  3633. string c = "c" + a;
  3634. bool arrayUseReflection = typeDesc.ArrayElementTypeDesc.UseReflection;
  3635. string arrayTypeFullName = typeDesc.ArrayElementTypeDesc.CSharpName;
  3636. if (!arrayUseReflection)
  3637. Writer.Write("(" +arrayTypeFullName +"[])");
  3638. Writer.Write("ShrinkArray(");
  3639. Writer.Write(a);
  3640. Writer.Write(", ");
  3641. Writer.Write(c);
  3642. Writer.Write(", ");
  3643. Writer.Write(RaCodeGen.GetStringForTypeof(arrayTypeFullName, arrayUseReflection));
  3644. Writer.Write(", ");
  3645. WriteBooleanValue(member.IsNullable);
  3646. Writer.Write(")");
  3647. WriteSourceEnd(member.Source);
  3648. Writer.WriteLine(";");
  3649. if (member.Mapping.ChoiceIdentifier != null) {
  3650. WriteSourceBegin(member.ChoiceSource);
  3651. a = member.ChoiceArrayName;
  3652. c = "c" + a;
  3653. bool choiceUseReflection = member.Mapping.ChoiceIdentifier.Mapping.TypeDesc.UseReflection;
  3654. string choiceTypeName = member.Mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName;
  3655. if(!choiceUseReflection)
  3656. Writer.Write("(" +choiceTypeName+"[])");
  3657. Writer.Write("ShrinkArray(");
  3658. Writer.Write(a);
  3659. Writer.Write(", ");
  3660. Writer.Write(c);
  3661. Writer.Write(", ");
  3662. Writer.Write(RaCodeGen.GetStringForTypeof(choiceTypeName, choiceUseReflection));
  3663. Writer.Write(", ");
  3664. WriteBooleanValue(member.IsNullable);
  3665. Writer.Write(")");
  3666. WriteSourceEnd(member.ChoiceSource);
  3667. Writer.WriteLine(";");
  3668. }
  3669. }
  3670. else if (typeDesc.IsValueType) {
  3671. Writer.Write(member.Source);
  3672. Writer.Write(" = ");
  3673. Writer.Write(member.ArrayName);
  3674. Writer.WriteLine(";");
  3675. }
  3676. }
  3677. }
  3678. }
  3679. void WriteSourceBeginTyped(string source, TypeDesc typeDesc) {
  3680. WriteSourceBegin(source);
  3681. if (typeDesc != null && !typeDesc.UseReflection) {
  3682. Writer.Write("(");
  3683. Writer.Write(typeDesc.CSharpName);
  3684. Writer.Write(")");
  3685. }
  3686. }
  3687. void WriteSourceBegin(string source) {
  3688. Writer.Write(source);
  3689. if (source[source.Length - 1] != '(' && source[source.Length - 1] != '{')
  3690. Writer.Write(" = ");
  3691. }
  3692. void WriteSourceEnd(string source) {
  3693. // source could be of the form "var", "arrayVar[i]",
  3694. // "collection.Add(" or "methodInfo.Invoke(collection, new object[] {"
  3695. if (source[source.Length - 1] == '(' )
  3696. Writer.Write(")");
  3697. else if( source[source.Length - 1] == '{')
  3698. Writer.Write("})");
  3699. }
  3700. void WriteArray(string source, string arrayName, ArrayMapping arrayMapping, bool readOnly, bool isNullable, int fixupIndex) {
  3701. if (arrayMapping.IsSoap) {
  3702. Writer.Write("object rre = ");
  3703. Writer.Write(fixupIndex >= 0 ? "ReadReferencingElement" : "ReadReferencedElement");
  3704. Writer.Write("(");
  3705. WriteID(arrayMapping.TypeName);
  3706. Writer.Write(", ");
  3707. WriteID(arrayMapping.Namespace);
  3708. if (fixupIndex >= 0) {
  3709. Writer.Write(", ");
  3710. Writer.Write("out fixup.Ids[");
  3711. Writer.Write((fixupIndex).ToString(CultureInfo.InvariantCulture));
  3712. Writer.Write("]");
  3713. }
  3714. Writer.WriteLine(");");
  3715. TypeDesc td = arrayMapping.TypeDesc;
  3716. if (td.IsEnumerable || td.IsCollection) {
  3717. Writer.WriteLine("if (rre != null) {");
  3718. Writer.Indent++;
  3719. WriteAddCollectionFixup(td, readOnly, source, "rre");
  3720. Writer.Indent--;
  3721. Writer.WriteLine("}");
  3722. }
  3723. else {
  3724. Writer.WriteLine("try {");
  3725. Writer.Indent++;
  3726. WriteSourceBeginTyped(source, arrayMapping.TypeDesc);
  3727. Writer.Write("rre");
  3728. WriteSourceEnd(source);
  3729. Writer.WriteLine(";");
  3730. WriteCatchCastException(arrayMapping.TypeDesc, "rre", null);
  3731. }
  3732. }
  3733. else {
  3734. Writer.WriteLine("if (!ReadNull()) {");
  3735. Writer.Indent++;
  3736. MemberMapping memberMapping = new MemberMapping();
  3737. memberMapping.Elements = arrayMapping.Elements;
  3738. memberMapping.TypeDesc = arrayMapping.TypeDesc;
  3739. memberMapping.ReadOnly = readOnly;
  3740. Member member = new Member(this, source, arrayName, 0, memberMapping, false);
  3741. member.IsNullable = false;//Note, Microsoft: IsNullable is set to false since null condition (xsi:nil) is already handled by 'ReadNull()'
  3742. Member[] members = new Member[] { member };
  3743. WriteMemberBegin(members);
  3744. if (readOnly) {
  3745. Writer.Write("if (((object)(");
  3746. Writer.Write(member.ArrayName);
  3747. Writer.Write(") == null) || ");
  3748. }
  3749. else {
  3750. Writer.Write("if (");
  3751. }
  3752. Writer.WriteLine("(Reader.IsEmptyElement)) {");
  3753. Writer.Indent++;
  3754. Writer.WriteLine("Reader.Skip();");
  3755. Writer.Indent--;
  3756. Writer.WriteLine("}");
  3757. Writer.WriteLine("else {");
  3758. Writer.Indent++;
  3759. Writer.WriteLine("Reader.ReadStartElement();");
  3760. int loopIndex = WriteWhileNotLoopStart();
  3761. Writer.Indent++;
  3762. string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");";
  3763. WriteMemberElements(members, unknownNode, unknownNode, null, null, null);
  3764. Writer.WriteLine("Reader.MoveToContent();");
  3765. WriteWhileLoopEnd(loopIndex);
  3766. Writer.Indent--;
  3767. Writer.WriteLine("ReadEndElement();");
  3768. Writer.WriteLine("}");
  3769. WriteMemberEnd(members, false);
  3770. Writer.Indent--;
  3771. Writer.WriteLine("}");
  3772. if (isNullable) {
  3773. Writer.WriteLine("else {");
  3774. Writer.Indent++;
  3775. member.IsNullable = true;
  3776. WriteMemberBegin(members);
  3777. WriteMemberEnd(members);
  3778. Writer.Indent--;
  3779. Writer.WriteLine("}");
  3780. }
  3781. }
  3782. }
  3783. void WriteElement(string source, string arrayName, string choiceSource, ElementAccessor element, ChoiceIdentifierAccessor choice, string checkSpecified, bool checkForNull, bool readOnly, int fixupIndex, int elementIndex) {
  3784. if (checkSpecified != null && checkSpecified.Length > 0) {
  3785. Writer.Write(checkSpecified);
  3786. Writer.WriteLine(" = true;");
  3787. }
  3788. if (element.Mapping is ArrayMapping) {
  3789. WriteArray(source, arrayName, (ArrayMapping)element.Mapping, readOnly, element.IsNullable, fixupIndex);
  3790. }
  3791. else if (element.Mapping is NullableMapping) {
  3792. string methodName = ReferenceMapping(element.Mapping);
  3793. #if DEBUG
  3794. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  3795. if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, element.Mapping.TypeDesc.Name));
  3796. #endif
  3797. WriteSourceBegin(source);
  3798. Writer.Write(methodName);
  3799. Writer.Write("(true)");
  3800. WriteSourceEnd(source);
  3801. Writer.WriteLine(";");
  3802. }
  3803. else if (!element.Mapping.IsSoap && (element.Mapping is PrimitiveMapping)) {
  3804. if (element.IsNullable) {
  3805. Writer.WriteLine("if (ReadNull()) {");
  3806. Writer.Indent++;
  3807. WriteSourceBegin(source);
  3808. if (element.Mapping.TypeDesc.IsValueType) {
  3809. Writer.Write(RaCodeGen.GetStringForCreateInstance(element.Mapping.TypeDesc.CSharpName, element.Mapping.TypeDesc.UseReflection, false, false));
  3810. }
  3811. else {
  3812. Writer.Write("null");
  3813. }
  3814. WriteSourceEnd(source);
  3815. Writer.WriteLine(";");
  3816. Writer.Indent--;
  3817. Writer.WriteLine("}");
  3818. Writer.Write("else ");
  3819. }
  3820. if (element.Default != null && element.Default != DBNull.Value && element.Mapping.TypeDesc.IsValueType) {
  3821. Writer.WriteLine("if (Reader.IsEmptyElement) {");
  3822. Writer.Indent++;
  3823. Writer.WriteLine("Reader.Skip();");
  3824. Writer.Indent--;
  3825. Writer.WriteLine("}");
  3826. Writer.WriteLine("else {");
  3827. }
  3828. else {
  3829. Writer.WriteLine("{");
  3830. }
  3831. Writer.Indent++;
  3832. //For backward compatibiity
  3833. //When using old serializer, the serialized TimeSpan value is empty string
  3834. if (element.Mapping.TypeDesc.Type == typeof(TimeSpan) && LocalAppContextSwitches.EnableTimeSpanSerialization)
  3835. {
  3836. Writer.WriteLine("if (Reader.IsEmptyElement) {");
  3837. Writer.Indent++;
  3838. Writer.WriteLine("Reader.Skip();");
  3839. WriteSourceBegin(source);
  3840. Writer.Write("default(System.TimeSpan)");
  3841. WriteSourceEnd(source);
  3842. Writer.WriteLine(";");
  3843. Writer.Indent--;
  3844. Writer.WriteLine("}");
  3845. Writer.WriteLine("else {");
  3846. Writer.Indent++;
  3847. WriteSourceBegin(source);
  3848. WritePrimitive(element.Mapping, "Reader.ReadElementString()");
  3849. WriteSourceEnd(source);
  3850. Writer.WriteLine(";");
  3851. Writer.Indent--;
  3852. Writer.WriteLine("}");
  3853. }
  3854. else
  3855. {
  3856. WriteSourceBegin(source);
  3857. if (element.Mapping.TypeDesc == QnameTypeDesc)
  3858. Writer.Write("ReadElementQualifiedName()");
  3859. else {
  3860. string readFunc;
  3861. switch (element.Mapping.TypeDesc.FormatterName) {
  3862. case "ByteArrayBase64":
  3863. case "ByteArrayHex":
  3864. readFunc = "false";
  3865. break;
  3866. default:
  3867. readFunc = "Reader.ReadElementString()";
  3868. break;
  3869. }
  3870. WritePrimitive(element.Mapping, readFunc);
  3871. }
  3872. WriteSourceEnd(source);
  3873. Writer.WriteLine(";");
  3874. }
  3875. Writer.Indent--;
  3876. Writer.WriteLine("}");
  3877. }
  3878. else if (element.Mapping is StructMapping || (element.Mapping.IsSoap && element.Mapping is PrimitiveMapping)) {
  3879. TypeMapping mapping = element.Mapping;
  3880. if (mapping.IsSoap) {
  3881. Writer.Write("object rre = ");
  3882. Writer.Write(fixupIndex >= 0 ? "ReadReferencingElement" : "ReadReferencedElement");
  3883. Writer.Write("(");
  3884. WriteID(mapping.TypeName);
  3885. Writer.Write(", ");
  3886. WriteID(mapping.Namespace);
  3887. if (fixupIndex >= 0) {
  3888. Writer.Write(", out fixup.Ids[");
  3889. Writer.Write((fixupIndex).ToString(CultureInfo.InvariantCulture));
  3890. Writer.Write("]");
  3891. }
  3892. Writer.Write(")");
  3893. WriteSourceEnd(source);
  3894. Writer.WriteLine(";");
  3895. if (mapping.TypeDesc.IsValueType) {
  3896. Writer.WriteLine("if (rre != null) {");
  3897. Writer.Indent++;
  3898. }
  3899. Writer.WriteLine("try {");
  3900. Writer.Indent++;
  3901. WriteSourceBeginTyped(source, mapping.TypeDesc);
  3902. Writer.Write("rre");
  3903. WriteSourceEnd(source);
  3904. Writer.WriteLine(";");
  3905. WriteCatchCastException(mapping.TypeDesc, "rre", null);
  3906. Writer.Write("Referenced(");
  3907. Writer.Write(source);
  3908. Writer.WriteLine(");");
  3909. if (mapping.TypeDesc.IsValueType) {
  3910. Writer.Indent--;
  3911. Writer.WriteLine("}");
  3912. }
  3913. }
  3914. else {
  3915. string methodName = ReferenceMapping(mapping);
  3916. #if DEBUG
  3917. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  3918. if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name));
  3919. #endif
  3920. if (checkForNull) {
  3921. Writer.Write("if ((object)(");
  3922. Writer.Write(arrayName);
  3923. Writer.Write(") == null) Reader.Skip(); else ");
  3924. }
  3925. WriteSourceBegin(source);
  3926. Writer.Write(methodName);
  3927. Writer.Write("(");
  3928. if (mapping.TypeDesc.IsNullable) {
  3929. WriteBooleanValue(element.IsNullable);
  3930. Writer.Write(", ");
  3931. }
  3932. Writer.Write("true");
  3933. Writer.Write(")");
  3934. WriteSourceEnd(source);
  3935. Writer.WriteLine(";");
  3936. }
  3937. }
  3938. else if (element.Mapping is SpecialMapping) {
  3939. SpecialMapping special = (SpecialMapping)element.Mapping;
  3940. switch (special.TypeDesc.Kind) {
  3941. case TypeKind.Node:
  3942. bool isDoc = special.TypeDesc.FullName == typeof(XmlDocument).FullName;
  3943. WriteSourceBeginTyped(source, special.TypeDesc);
  3944. Writer.Write(isDoc ? "ReadXmlDocument(" : "ReadXmlNode(");
  3945. Writer.Write(element.Any ? "false" : "true");
  3946. Writer.Write(")");
  3947. WriteSourceEnd(source);
  3948. Writer.WriteLine(";");
  3949. break;
  3950. case TypeKind.Serializable:
  3951. SerializableMapping sm = (SerializableMapping)element.Mapping;
  3952. // check to see if we need to do the derivation
  3953. if (sm.DerivedMappings != null) {
  3954. Writer.Write(typeof(XmlQualifiedName).FullName);
  3955. Writer.WriteLine(" tser = GetXsiType();");
  3956. Writer.Write("if (tser == null");
  3957. Writer.Write(" || ");
  3958. WriteQNameEqual("tser", sm.XsiType.Name, sm.XsiType.Namespace);
  3959. Writer.WriteLine(") {");
  3960. Writer.Indent++;
  3961. }
  3962. WriteSourceBeginTyped(source, sm.TypeDesc);
  3963. Writer.Write("ReadSerializable(( ");
  3964. Writer.Write(typeof(IXmlSerializable).FullName);
  3965. Writer.Write(")");
  3966. Writer.Write(RaCodeGen.GetStringForCreateInstance(sm.TypeDesc.CSharpName, sm.TypeDesc.UseReflection, sm.TypeDesc.CannotNew, false));
  3967. bool isWrappedAny = !element.Any && IsWildcard(sm);
  3968. if (isWrappedAny) {
  3969. Writer.WriteLine(", true");
  3970. }
  3971. Writer.Write(")");
  3972. WriteSourceEnd(source);
  3973. Writer.WriteLine(";");
  3974. if (sm.DerivedMappings != null) {
  3975. Writer.Indent--;
  3976. Writer.WriteLine("}");
  3977. WriteDerivedSerializable(sm, sm, source, isWrappedAny);
  3978. WriteUnknownNode("UnknownNode", "null", null, true);
  3979. }
  3980. break;
  3981. default:
  3982. throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
  3983. }
  3984. }
  3985. else {
  3986. throw new InvalidOperationException(Res.GetString(Res.XmlInternalError));
  3987. }
  3988. if (choice != null) {
  3989. #if DEBUG
  3990. // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
  3991. if (choiceSource == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "need parent for the " + source));
  3992. #endif
  3993. string enumTypeName = choice.Mapping.TypeDesc.CSharpName;
  3994. Writer.Write(choiceSource);
  3995. Writer.Write(" = ");
  3996. CodeIdentifier.CheckValidIdentifier(choice.MemberIds[elementIndex]);
  3997. Writer.Write(RaCodeGen.GetStringForEnumMember(enumTypeName, choice.MemberIds[elementIndex], choice.Mapping.TypeDesc.UseReflection));
  3998. Writer.WriteLine(";");
  3999. }
  4000. }
  4001. void WriteDerivedSerializable(SerializableMapping head, SerializableMapping mapping, string source, bool isWrappedAny) {
  4002. if (mapping == null)
  4003. return;
  4004. for (SerializableMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
  4005. Writer.Write("else if (tser == null");
  4006. Writer.Write(" || ");
  4007. WriteQNameEqual("tser", derived.XsiType.Name, derived.XsiType.Namespace);
  4008. Writer.WriteLine(") {");
  4009. Writer.Indent++;
  4010. if (derived.Type != null) {
  4011. if (head.Type.IsAssignableFrom(derived.Type)) {
  4012. WriteSourceBeginTyped(source, head.TypeDesc);
  4013. Writer.Write("ReadSerializable(( ");
  4014. Writer.Write(typeof(IXmlSerializable).FullName);
  4015. Writer.Write(")");
  4016. Writer.Write(RaCodeGen.GetStringForCreateInstance(derived.TypeDesc.CSharpName, derived.TypeDesc.UseReflection, derived.TypeDesc.CannotNew, false));
  4017. if (isWrappedAny) {
  4018. Writer.WriteLine(", true");
  4019. }
  4020. Writer.Write(")");
  4021. WriteSourceEnd(source);
  4022. Writer.WriteLine(";");
  4023. }
  4024. else {
  4025. Writer.Write("throw CreateBadDerivationException(");
  4026. WriteQuotedCSharpString(derived.XsiType.Name);
  4027. Writer.Write(", ");
  4028. WriteQuotedCSharpString(derived.XsiType.Namespace);
  4029. Writer.Write(", ");
  4030. WriteQuotedCSharpString(head.XsiType.Name);
  4031. Writer.Write(", ");
  4032. WriteQuotedCSharpString(head.XsiType.Namespace);
  4033. Writer.Write(", ");
  4034. WriteQuotedCSharpString(derived.Type.FullName);
  4035. Writer.Write(", ");
  4036. WriteQuotedCSharpString(head.Type.FullName);
  4037. Writer.WriteLine(");");
  4038. }
  4039. }
  4040. else {
  4041. Writer.WriteLine("// " + "missing real mapping for " + derived.XsiType);
  4042. Writer.Write("throw CreateMissingIXmlSerializableType(");
  4043. WriteQuotedCSharpString(derived.XsiType.Name);
  4044. Writer.Write(", ");
  4045. WriteQuotedCSharpString(derived.XsiType.Namespace);
  4046. Writer.Write(", ");
  4047. WriteQuotedCSharpString(head.Type.FullName);
  4048. Writer.WriteLine(");");
  4049. }
  4050. Writer.Indent--;
  4051. Writer.WriteLine("}");
  4052. WriteDerivedSerializable(head, derived, source, isWrappedAny);
  4053. }
  4054. }
  4055. int WriteWhileNotLoopStart()
  4056. {
  4057. Writer.WriteLine("Reader.MoveToContent();");
  4058. int loopIndex = WriteWhileLoopStartCheck();
  4059. Writer.Write("while (Reader.NodeType != ");
  4060. Writer.Write(typeof(XmlNodeType).FullName);
  4061. Writer.Write(".EndElement && Reader.NodeType != ");
  4062. Writer.Write(typeof(XmlNodeType).FullName);
  4063. Writer.WriteLine(".None) {");
  4064. return loopIndex;
  4065. }
  4066. void WriteWhileLoopEnd(int loopIndex)
  4067. {
  4068. WriteWhileLoopEndCheck(loopIndex);
  4069. Writer.Indent--;
  4070. Writer.WriteLine("}");
  4071. }
  4072. int WriteWhileLoopStartCheck()
  4073. {
  4074. Writer.WriteLine(String.Format(CultureInfo.InvariantCulture, "int whileIterations{0} = 0;", nextWhileLoopIndex));
  4075. Writer.WriteLine(String.Format(CultureInfo.InvariantCulture, "int readerCount{0} = ReaderCount;", nextWhileLoopIndex));
  4076. return nextWhileLoopIndex++;
  4077. }
  4078. void WriteWhileLoopEndCheck(int loopIndex)
  4079. {
  4080. Writer.WriteLine(String.Format(CultureInfo.InvariantCulture, "CheckReaderCount(ref whileIterations{0}, ref readerCount{1});", loopIndex, loopIndex));
  4081. }
  4082. void WriteParamsRead(int length) {
  4083. Writer.Write("bool[] paramsRead = new bool[");
  4084. Writer.Write(length.ToString(CultureInfo.InvariantCulture));
  4085. Writer.WriteLine("];");
  4086. }
  4087. void WriteReadNonRoots() {
  4088. Writer.WriteLine("Reader.MoveToContent();");
  4089. int loopIndex = WriteWhileLoopStartCheck();
  4090. Writer.Write("while (Reader.NodeType == ");
  4091. Writer.Write(typeof(XmlNodeType).FullName);
  4092. Writer.WriteLine(".Element) {");
  4093. Writer.Indent++;
  4094. Writer.Write("string root = Reader.GetAttribute(\"root\", \"");
  4095. Writer.Write(Soap.Encoding);
  4096. Writer.WriteLine("\");");
  4097. Writer.Write("if (root == null || ");
  4098. Writer.Write(typeof(XmlConvert).FullName);
  4099. Writer.WriteLine(".ToBoolean(root)) break;");
  4100. Writer.WriteLine("ReadReferencedElement();");
  4101. Writer.WriteLine("Reader.MoveToContent();");
  4102. WriteWhileLoopEnd(loopIndex);
  4103. }
  4104. void WriteBooleanValue(bool value) {
  4105. Writer.Write(value ? "true" : "false");
  4106. }
  4107. void WriteInitCheckTypeHrefList(string source) {
  4108. Writer.Write(typeof(ArrayList).FullName);
  4109. Writer.Write(" ");
  4110. Writer.Write(source);
  4111. Writer.Write(" = new ");
  4112. Writer.Write(typeof(ArrayList).FullName);
  4113. Writer.WriteLine("();");
  4114. Writer.Write(typeof(ArrayList).FullName);
  4115. Writer.Write(" ");
  4116. Writer.Write(source);
  4117. Writer.Write("IsObject = new ");
  4118. Writer.Write(typeof(ArrayList).FullName);
  4119. Writer.WriteLine("();");
  4120. }
  4121. void WriteHandleHrefList(Member[] members, string listSource) {
  4122. Writer.WriteLine("int isObjectIndex = 0;");
  4123. Writer.Write("foreach (object obj in ");
  4124. Writer.Write(listSource);
  4125. Writer.WriteLine(") {");
  4126. Writer.Indent++;
  4127. Writer.WriteLine("bool isReferenced = true;");
  4128. Writer.Write("bool isObject = (bool)");
  4129. Writer.Write(listSource);
  4130. Writer.WriteLine("IsObject[isObjectIndex++];");
  4131. Writer.WriteLine("object refObj = isObject ? obj : GetTarget((string)obj);");
  4132. Writer.WriteLine("if (refObj == null) continue;");
  4133. Writer.Write(typeof(Type).FullName);
  4134. Writer.WriteLine(" refObjType = refObj.GetType();");
  4135. Writer.WriteLine("string refObjId = null;");
  4136. WriteMemberElementsIf(members, null, "isReferenced = false;", "refObj");
  4137. Writer.WriteLine("if (isObject && isReferenced) Referenced(refObj); // need to mark this obj as ref'd since we didn't do GetTarget");
  4138. Writer.Indent--;
  4139. Writer.WriteLine("}");
  4140. }
  4141. void WriteIfNotSoapRoot(string source) {
  4142. Writer.Write("if (Reader.GetAttribute(\"root\", \"");
  4143. Writer.Write(Soap.Encoding);
  4144. Writer.WriteLine("\") == \"0\") {");
  4145. Writer.Indent++;
  4146. Writer.WriteLine(source);
  4147. Writer.Indent--;
  4148. Writer.WriteLine("}");
  4149. }
  4150. void WriteCreateMapping(TypeMapping mapping, string local) {
  4151. string fullTypeName = mapping.TypeDesc.CSharpName;
  4152. bool useReflection = mapping.TypeDesc.UseReflection;
  4153. bool ctorInaccessible = mapping.TypeDesc.CannotNew;
  4154. Writer.Write(useReflection ? "object" : fullTypeName);
  4155. Writer.Write(" ");
  4156. Writer.Write(local);
  4157. Writer.WriteLine(";");
  4158. if (ctorInaccessible) {
  4159. Writer.WriteLine("try {");
  4160. Writer.Indent++;
  4161. }
  4162. Writer.Write(local);
  4163. Writer.Write(" = ");
  4164. Writer.Write(RaCodeGen.GetStringForCreateInstance(fullTypeName, useReflection, mapping.TypeDesc.CannotNew, true));
  4165. Writer.WriteLine(";");
  4166. if (ctorInaccessible) {
  4167. WriteCatchException(typeof(MissingMethodException));
  4168. Writer.Indent++;
  4169. Writer.Write("throw CreateInaccessibleConstructorException(");
  4170. WriteQuotedCSharpString(fullTypeName);
  4171. Writer.WriteLine(");");
  4172. WriteCatchException(typeof(SecurityException));
  4173. Writer.Indent++;
  4174. Writer.Write("throw CreateCtorHasSecurityException(");
  4175. WriteQuotedCSharpString(fullTypeName);
  4176. Writer.WriteLine(");");
  4177. Writer.Indent--;
  4178. Writer.WriteLine("}");
  4179. }
  4180. }
  4181. void WriteCatchException(Type exceptionType) {
  4182. Writer.Indent--;
  4183. Writer.WriteLine("}");
  4184. Writer.Write("catch (");
  4185. Writer.Write(exceptionType.FullName);
  4186. Writer.WriteLine(") {");
  4187. }
  4188. void WriteCatchCastException(TypeDesc typeDesc, string source, string id) {
  4189. WriteCatchException(typeof(InvalidCastException));
  4190. Writer.Indent++;
  4191. Writer.Write("throw CreateInvalidCastException(");
  4192. Writer.Write(RaCodeGen.GetStringForTypeof(typeDesc.CSharpName, typeDesc.UseReflection));
  4193. Writer.Write(", ");
  4194. Writer.Write(source);
  4195. if (id == null)
  4196. Writer.WriteLine(", null);");
  4197. else {
  4198. Writer.Write(", (string)");
  4199. Writer.Write(id);
  4200. Writer.WriteLine(");");
  4201. }
  4202. Writer.Indent--;
  4203. Writer.WriteLine("}");
  4204. }
  4205. void WriteArrayLocalDecl( string typeName, string variableName, string initValue, TypeDesc arrayTypeDesc) {
  4206. RaCodeGen.WriteArrayLocalDecl(typeName, variableName, initValue, arrayTypeDesc);
  4207. }
  4208. void WriteCreateInstance(string escapedName, string source, bool useReflection, bool ctorInaccessible){
  4209. RaCodeGen.WriteCreateInstance(escapedName, source, useReflection, ctorInaccessible);
  4210. }
  4211. void WriteLocalDecl(string typeFullName, string variableName, string initValue, bool useReflection) {
  4212. RaCodeGen.WriteLocalDecl(typeFullName, variableName, initValue, useReflection);
  4213. }
  4214. }
  4215. }