XmlSerializationReader.cs 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. //
  2. // System.Xml.Serialization.XmlSerializationReader.cs
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.Globalization;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using System.Reflection;
  38. namespace System.Xml.Serialization
  39. {
  40. public abstract class XmlSerializationReader
  41. #if NET_2_0
  42. : XmlSerializationGeneratedCode
  43. #endif
  44. {
  45. #region Fields
  46. XmlDocument document;
  47. XmlReader reader;
  48. ArrayList fixups;
  49. Hashtable collFixups;
  50. ArrayList collItemFixups;
  51. Hashtable typesCallbacks;
  52. ArrayList noIDTargets;
  53. Hashtable targets;
  54. Hashtable delayedListFixups;
  55. XmlSerializer eventSource;
  56. int delayedFixupId = 0;
  57. string w3SchemaNS;
  58. string w3SchemaNS2000;
  59. string w3SchemaNS1999;
  60. string w3InstanceNS;
  61. string w3InstanceNS2000;
  62. string w3InstanceNS1999;
  63. string soapNS;
  64. string schema;
  65. string wsdlNS;
  66. string wsdlArrayType;
  67. string nullX;
  68. string nil;
  69. string typeX;
  70. string arrayType;
  71. string anyType;
  72. XmlQualifiedName arrayQName;
  73. #endregion
  74. internal void Initialize (XmlReader reader, XmlSerializer eventSource)
  75. {
  76. w3SchemaNS = reader.NameTable.Add (XmlSchema.Namespace);
  77. w3SchemaNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema");
  78. w3SchemaNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema");
  79. w3InstanceNS = reader.NameTable.Add (XmlSchema.InstanceNamespace);
  80. w3InstanceNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema-instance");
  81. w3InstanceNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema-instance");
  82. soapNS = reader.NameTable.Add (XmlSerializer.EncodingNamespace);
  83. schema = reader.NameTable.Add ("schema");
  84. wsdlNS = reader.NameTable.Add (XmlSerializer.WsdlNamespace);
  85. wsdlArrayType = reader.NameTable.Add ("arrayType");
  86. nullX = reader.NameTable.Add ("null");
  87. nil = reader.NameTable.Add ("nil");
  88. typeX = reader.NameTable.Add ("type");
  89. arrayType = reader.NameTable.Add ("arrayType");
  90. anyType = reader.NameTable.Add ("anyType");
  91. this.reader = reader;
  92. this.eventSource = eventSource;
  93. arrayQName = new XmlQualifiedName ("Array", XmlSerializer.EncodingNamespace);
  94. InitIDs ();
  95. }
  96. private ArrayList EnsureArrayList (ArrayList list)
  97. {
  98. if (list == null)
  99. list = new ArrayList ();
  100. return list;
  101. }
  102. private Hashtable EnsureHashtable (Hashtable hash)
  103. {
  104. if (hash == null)
  105. hash = new Hashtable ();
  106. return hash;
  107. }
  108. protected XmlSerializationReader ()
  109. {
  110. }
  111. protected XmlDocument Document
  112. {
  113. get {
  114. if (document == null)
  115. document = new XmlDocument (reader.NameTable);
  116. return document;
  117. }
  118. }
  119. protected XmlReader Reader {
  120. get { return reader; }
  121. }
  122. [MonoTODO]
  123. protected bool IsReturnValue
  124. {
  125. get {
  126. throw new NotImplementedException ();
  127. }
  128. set {
  129. throw new NotImplementedException ();
  130. }
  131. }
  132. #region Methods
  133. protected void AddFixup (CollectionFixup fixup)
  134. {
  135. collFixups = EnsureHashtable (collFixups);
  136. collFixups [fixup.Id] = fixup;
  137. if (delayedListFixups != null && delayedListFixups.ContainsKey (fixup.Id)) {
  138. fixup.CollectionItems = delayedListFixups [fixup.Id];
  139. delayedListFixups.Remove (fixup.Id);
  140. }
  141. }
  142. protected void AddFixup (Fixup fixup)
  143. {
  144. fixups = EnsureArrayList (fixups);
  145. fixups.Add (fixup);
  146. }
  147. void AddFixup (CollectionItemFixup fixup)
  148. {
  149. collItemFixups = EnsureArrayList (collItemFixups);
  150. collItemFixups.Add(fixup);
  151. }
  152. protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
  153. {
  154. WriteCallbackInfo info = new WriteCallbackInfo ();
  155. info.Type = type;
  156. info.TypeName = name;
  157. info.TypeNs = ns;
  158. info.Callback = read;
  159. typesCallbacks = EnsureHashtable (typesCallbacks);
  160. typesCallbacks.Add (new XmlQualifiedName (name, ns), info);
  161. }
  162. protected void AddTarget (string id, object o)
  163. {
  164. if (id != null) {
  165. targets = EnsureHashtable (targets);
  166. if (targets [id] == null)
  167. targets.Add (id, o);
  168. } else {
  169. if (o != null)
  170. return;
  171. noIDTargets = EnsureArrayList (noIDTargets);
  172. noIDTargets.Add (o);
  173. }
  174. }
  175. private string CurrentTag ()
  176. {
  177. switch (reader.NodeType) {
  178. case XmlNodeType.Element:
  179. return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
  180. reader.NamespaceURI);
  181. case XmlNodeType.Attribute:
  182. return reader.Value;
  183. case XmlNodeType.Text:
  184. return "CDATA";
  185. case XmlNodeType.ProcessingInstruction:
  186. return "<--";
  187. case XmlNodeType.Entity:
  188. return "<?";
  189. case XmlNodeType.EndElement:
  190. return ">";
  191. default:
  192. return "(unknown)";
  193. }
  194. }
  195. protected Exception CreateCtorHasSecurityException (string typeName)
  196. {
  197. string message = string.Format ("The type '{0}' cannot"
  198. + " be serialized because its parameterless"
  199. + " constructor is decorated with declarative"
  200. + " security permission attributes."
  201. + " Consider using imperative asserts or demands"
  202. + " in the constructor.", typeName);
  203. return new InvalidOperationException (message);
  204. }
  205. protected Exception CreateInaccessibleConstructorException (string typeName)
  206. {
  207. string message = string.Format ("{0} cannot be serialized"
  208. + " because it does not have a default public"
  209. + " constructor.", typeName);
  210. return new InvalidOperationException (message);
  211. }
  212. protected Exception CreateAbstractTypeException (string name, string ns)
  213. {
  214. string message = "The specified type is abstrace: name='" + name + "' namespace='" + ns + "', at " + CurrentTag ();
  215. return new InvalidOperationException (message);
  216. }
  217. protected Exception CreateInvalidCastException (Type type, object value)
  218. {
  219. string message = String.Format (CultureInfo.InvariantCulture, "Cannot assign object of type {0} to an object of " +
  220. "type {1}.", value.GetType (), type);
  221. return new InvalidCastException (message);
  222. }
  223. protected Exception CreateReadOnlyCollectionException (string name)
  224. {
  225. string message = String.Format ("Could not serialize {0}. Default constructors are " +
  226. "required for collections and enumerators.", name);
  227. return new InvalidOperationException (message);
  228. }
  229. protected Exception CreateUnknownConstantException (string value, Type enumType)
  230. {
  231. string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
  232. return new InvalidOperationException (message);
  233. }
  234. protected Exception CreateUnknownNodeException ()
  235. {
  236. string message = CurrentTag () + " was not expected";
  237. return new InvalidOperationException (message);
  238. }
  239. protected Exception CreateUnknownTypeException (XmlQualifiedName type)
  240. {
  241. string message = "The specified type was not recognized: name='" + type.Name + "' namespace='" + type.Namespace + "', at " + CurrentTag ();
  242. return new InvalidOperationException (message);
  243. }
  244. protected Array EnsureArrayIndex (Array a, int index, Type elementType)
  245. {
  246. if (a != null && index < a.Length)
  247. return a;
  248. int size;
  249. if (a == null) {
  250. size = 32;
  251. } else {
  252. size = a.Length * 2;
  253. }
  254. Array result = Array.CreateInstance (elementType, size);
  255. if (a != null)
  256. Array.Copy (a, result, index);
  257. return result;
  258. }
  259. [MonoTODO ("Implement")]
  260. protected void FixupArrayRefs (object fixup)
  261. {
  262. throw new NotImplementedException ();
  263. }
  264. [MonoTODO ("Implement")]
  265. protected int GetArrayLength (string name, string ns)
  266. {
  267. throw new NotImplementedException ();
  268. }
  269. protected bool GetNullAttr ()
  270. {
  271. string na = reader.GetAttribute (nullX, w3InstanceNS);
  272. if (na == null) {
  273. na = reader.GetAttribute (nil, w3InstanceNS);
  274. if (na == null) {
  275. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  276. if (na == null)
  277. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  278. }
  279. }
  280. return (na != null);
  281. }
  282. protected object GetTarget (string id)
  283. {
  284. if (targets == null) return null;
  285. return targets [id];
  286. }
  287. bool TargetReady (string id)
  288. {
  289. if (targets == null) return false;
  290. return targets.ContainsKey (id);
  291. }
  292. protected XmlQualifiedName GetXsiType ()
  293. {
  294. string typeName = Reader.GetAttribute ("type", XmlSchema.InstanceNamespace);
  295. if (typeName == string.Empty || typeName == null) {
  296. typeName = Reader.GetAttribute ("type", w3InstanceNS1999);
  297. if (typeName == string.Empty || typeName == null) {
  298. typeName = Reader.GetAttribute ("type", w3InstanceNS2000);
  299. if (typeName == string.Empty || typeName == null)
  300. return null;
  301. }
  302. }
  303. int i = typeName.IndexOf (":");
  304. if (i == -1) return new XmlQualifiedName (typeName, Reader.NamespaceURI);
  305. else
  306. {
  307. string prefix = typeName.Substring(0,i);
  308. string name = typeName.Substring (i+1);
  309. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  310. }
  311. }
  312. protected abstract void InitCallbacks ();
  313. protected abstract void InitIDs ();
  314. protected bool IsXmlnsAttribute (string name)
  315. {
  316. int length = name.Length;
  317. if (length < 5)
  318. return false;
  319. if (length == 5)
  320. return (name == "xmlns");
  321. return name.StartsWith ("xmlns:");
  322. }
  323. protected void ParseWsdlArrayType (XmlAttribute attr)
  324. {
  325. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace && attr.LocalName == "arrayType")
  326. {
  327. string ns = "", type, dimensions;
  328. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  329. if (ns != "") ns = Reader.LookupNamespace (ns) + ":";
  330. attr.Value = ns + type + dimensions;
  331. }
  332. }
  333. protected XmlQualifiedName ReadElementQualifiedName ()
  334. {
  335. if (reader.IsEmptyElement) {
  336. reader.Skip();
  337. return ToXmlQualifiedName (String.Empty);
  338. }
  339. reader.ReadStartElement ();
  340. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  341. reader.ReadEndElement ();
  342. return xqn;
  343. }
  344. protected void ReadEndElement ()
  345. {
  346. while (reader.NodeType == XmlNodeType.Whitespace)
  347. reader.Skip ();
  348. if (reader.NodeType != XmlNodeType.None) {
  349. reader.ReadEndElement ();
  350. } else {
  351. reader.Skip ();
  352. }
  353. }
  354. protected bool ReadNull ()
  355. {
  356. if (!GetNullAttr ())
  357. return false;
  358. if (reader.IsEmptyElement) {
  359. reader.Skip();
  360. return true;
  361. }
  362. reader.ReadStartElement();
  363. while (reader.NodeType != XmlNodeType.EndElement)
  364. UnknownNode (null);
  365. ReadEndElement ();
  366. return true;
  367. }
  368. protected XmlQualifiedName ReadNullableQualifiedName ()
  369. {
  370. if (ReadNull ())
  371. return null;
  372. return ReadElementQualifiedName ();
  373. }
  374. protected string ReadNullableString ()
  375. {
  376. if (ReadNull ())
  377. return null;
  378. return reader.ReadElementString ();
  379. }
  380. protected bool ReadReference (out string fixupReference)
  381. {
  382. string href = reader.GetAttribute ("href");
  383. if (href == null) {
  384. fixupReference = null;
  385. return false;
  386. }
  387. if (href [0] != '#')
  388. throw new InvalidOperationException("href not found: " + href);
  389. fixupReference = href.Substring (1);
  390. if (!reader.IsEmptyElement) {
  391. reader.ReadStartElement ();
  392. ReadEndElement ();
  393. } else {
  394. reader.Skip ();
  395. }
  396. return true;
  397. }
  398. protected object ReadReferencedElement ()
  399. {
  400. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  401. }
  402. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  403. {
  404. if (typesCallbacks == null)
  405. {
  406. typesCallbacks = new Hashtable ();
  407. InitCallbacks ();
  408. }
  409. return (WriteCallbackInfo) typesCallbacks[qname];
  410. }
  411. protected object ReadReferencedElement (string name, string ns)
  412. {
  413. XmlQualifiedName qname = GetXsiType ();
  414. if (qname == null) qname = new XmlQualifiedName (name, ns);
  415. string id = Reader.GetAttribute ("id");
  416. object ob;
  417. if (qname == arrayQName)
  418. {
  419. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  420. if (ReadList (out ob))
  421. {
  422. // List complete (does not contain references)
  423. if (fixup != null)
  424. {
  425. fixup.Callback (fixup.Collection, ob);
  426. collFixups.Remove (id);
  427. ob = fixup.Collection;
  428. }
  429. }
  430. else if (fixup != null)
  431. {
  432. fixup.CollectionItems = (object[])ob;
  433. ob = fixup.Collection;
  434. }
  435. }
  436. else
  437. {
  438. WriteCallbackInfo info = GetCallbackInfo (qname);
  439. if (info == null)
  440. ob = ReadTypedPrimitive (qname);
  441. else
  442. ob = info.Callback();
  443. }
  444. AddTarget (id, ob);
  445. return ob;
  446. }
  447. bool ReadList (out object resultList)
  448. {
  449. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  450. if (arrayType == null) arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.WsdlNamespace);
  451. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  452. int i = qn.Name.LastIndexOf ('[');
  453. string dim = qn.Name.Substring (i);
  454. string itemType = qn.Name.Substring (0,i);
  455. int count = Int32.Parse (dim.Substring (1, dim.Length - 2), CultureInfo.InvariantCulture);
  456. Array list;
  457. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  458. string baseType = itemType.Substring (0,i);
  459. string arrayTypeName;
  460. if (qn.Namespace == XmlSchema.Namespace)
  461. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  462. else
  463. {
  464. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  465. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  466. }
  467. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  468. bool listComplete = true;
  469. if (Reader.IsEmptyElement)
  470. Reader.Skip ();
  471. else {
  472. Reader.ReadStartElement ();
  473. for (int n=0; n<count; n++)
  474. {
  475. Reader.MoveToContent ();
  476. string refid = Reader.GetAttribute ("href");
  477. string id;
  478. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  479. if (id == null)
  480. list.SetValue (item,n);
  481. else
  482. {
  483. AddFixup (new CollectionItemFixup (list, n, id));
  484. listComplete = false;
  485. }
  486. }
  487. Reader.ReadEndElement ();
  488. }
  489. resultList = list;
  490. return listComplete;
  491. }
  492. protected void ReadReferencedElements ()
  493. {
  494. reader.MoveToContent();
  495. XmlNodeType nt = reader.NodeType;
  496. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  497. ReadReferencedElement ();
  498. reader.MoveToContent ();
  499. nt = reader.NodeType;
  500. }
  501. // Registers delayed list
  502. if (delayedListFixups != null)
  503. {
  504. foreach (DictionaryEntry entry in delayedListFixups)
  505. AddTarget ((string)entry.Key, entry.Value);
  506. }
  507. // Fix arrays
  508. if (collItemFixups != null)
  509. {
  510. foreach (CollectionItemFixup itemFixup in collItemFixups)
  511. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  512. }
  513. // Fills collections
  514. if (collFixups != null)
  515. {
  516. ICollection cfixups = collFixups.Values;
  517. foreach (CollectionFixup fixup in cfixups)
  518. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  519. }
  520. // Fills class instances
  521. if (fixups != null)
  522. {
  523. foreach (Fixup fixup in fixups)
  524. fixup.Callback (fixup);
  525. }
  526. }
  527. protected object ReadReferencingElement (out string fixupReference)
  528. {
  529. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  530. }
  531. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  532. {
  533. return ReadReferencingElement (name, ns, false, out fixupReference);
  534. }
  535. protected object ReadReferencingElement (string name,
  536. string ns,
  537. bool elementCanBeType,
  538. out string fixupReference)
  539. {
  540. if (ReadNull ())
  541. {
  542. fixupReference = null;
  543. return null;
  544. }
  545. string refid = Reader.GetAttribute ("href");
  546. if (refid == string.Empty || refid == null)
  547. {
  548. fixupReference = null;
  549. XmlQualifiedName qname = GetXsiType ();
  550. if (qname == null) qname = new XmlQualifiedName (name, ns);
  551. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  552. if (qname == arrayQName || arrayType != null)
  553. {
  554. delayedListFixups = EnsureHashtable (delayedListFixups);
  555. fixupReference = "__<" + (delayedFixupId++) + ">";
  556. object items;
  557. ReadList (out items);
  558. delayedListFixups [fixupReference] = items;
  559. return null;
  560. }
  561. else
  562. {
  563. WriteCallbackInfo info = GetCallbackInfo (qname);
  564. if (info == null)
  565. return ReadTypedPrimitive (qname);
  566. else
  567. return info.Callback();
  568. }
  569. }
  570. else
  571. {
  572. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  573. Reader.ReadStartElement ();
  574. if (TargetReady (refid))
  575. {
  576. fixupReference = null;
  577. return GetTarget (refid);
  578. }
  579. else
  580. {
  581. fixupReference = refid;
  582. return null;
  583. }
  584. }
  585. }
  586. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  587. {
  588. if (ReadNull ()) return null;
  589. int depth = reader.Depth;
  590. serializable.ReadXml (reader);
  591. Reader.MoveToContent ();
  592. while (reader.Depth > depth)
  593. reader.Skip ();
  594. if (reader.Depth == depth && reader.NodeType == XmlNodeType.EndElement)
  595. reader.ReadEndElement ();
  596. return serializable;
  597. }
  598. protected string ReadString (string value)
  599. {
  600. if (value == null || value == String.Empty)
  601. return reader.ReadString ();
  602. return (value + reader.ReadString ());
  603. }
  604. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  605. {
  606. if (qname == null) qname = GetXsiType ();
  607. TypeData typeData = TypeTranslator.FindPrimitiveTypeData (qname.Name);
  608. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive)
  609. {
  610. // Put everything into a node array
  611. XmlNode node = Document.ReadNode (reader);
  612. XmlElement elem = node as XmlElement;
  613. if (elem == null)
  614. return new XmlNode[] {node};
  615. else {
  616. XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];
  617. int n = 0;
  618. foreach (XmlNode no in elem.Attributes)
  619. nodes[n++] = no;
  620. foreach (XmlNode no in elem.ChildNodes)
  621. nodes[n++] = no;
  622. return nodes;
  623. }
  624. }
  625. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  626. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  627. }
  628. protected XmlNode ReadXmlNode (bool wrapped)
  629. {
  630. XmlNode node = Document.ReadNode (reader);
  631. if (wrapped)
  632. return node.FirstChild;
  633. else
  634. return node;
  635. }
  636. protected XmlDocument ReadXmlDocument (bool wrapped)
  637. {
  638. if (wrapped)
  639. reader.ReadStartElement ();
  640. XmlDocument doc = new XmlDocument ();
  641. XmlNode node = doc.ReadNode (reader);
  642. doc.AppendChild (node);
  643. if (wrapped)
  644. reader.ReadEndElement ();
  645. return doc;
  646. }
  647. [MonoTODO ("Implement")]
  648. protected void Referenced (object o)
  649. {
  650. }
  651. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  652. {
  653. if (length == 0 && isNullable) return null;
  654. if (a == null) return Array.CreateInstance (elementType, length);
  655. if (a.Length == length) return a;
  656. Array result = Array.CreateInstance (elementType, length);
  657. Array.Copy (a, result, length);
  658. return result;
  659. }
  660. protected byte[] ToByteArrayBase64 (bool isNull)
  661. {
  662. return Convert.FromBase64String (Reader.ReadString());
  663. }
  664. [MonoTODO ("Implement")]
  665. protected static byte[] ToByteArrayBase64 (string value)
  666. {
  667. throw new NotImplementedException ();
  668. }
  669. [MonoTODO ("Implement")]
  670. protected byte[] ToByteArrayHex (bool isNull)
  671. {
  672. throw new NotImplementedException ();
  673. }
  674. [MonoTODO ("Implement")]
  675. protected static byte[] ToByteArrayHex (string value)
  676. {
  677. throw new NotImplementedException ();
  678. }
  679. protected static char ToChar (string value)
  680. {
  681. return XmlCustomFormatter.ToChar (value);
  682. }
  683. protected static DateTime ToDate (string value)
  684. {
  685. return XmlCustomFormatter.ToDate (value);
  686. }
  687. protected static DateTime ToDateTime (string value)
  688. {
  689. return XmlCustomFormatter.ToDateTime (value);
  690. }
  691. protected static long ToEnum (string value, Hashtable h, string typeName)
  692. {
  693. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  694. }
  695. protected static DateTime ToTime (string value)
  696. {
  697. return XmlCustomFormatter.ToTime (value);
  698. }
  699. protected static string ToXmlName (string value)
  700. {
  701. return XmlCustomFormatter.ToXmlName (value);
  702. }
  703. protected static string ToXmlNCName (string value)
  704. {
  705. return XmlCustomFormatter.ToXmlNCName (value);
  706. }
  707. protected static string ToXmlNmToken (string value)
  708. {
  709. return XmlCustomFormatter.ToXmlNmToken (value);
  710. }
  711. protected static string ToXmlNmTokens (string value)
  712. {
  713. return XmlCustomFormatter.ToXmlNmTokens (value);
  714. }
  715. protected XmlQualifiedName ToXmlQualifiedName (string value)
  716. {
  717. string name;
  718. string ns;
  719. int lastColon = value.LastIndexOf (':');
  720. string decodedValue = XmlConvert.DecodeName (value);
  721. if (lastColon < 0) {
  722. name = reader.NameTable.Add (decodedValue);
  723. ns = reader.LookupNamespace (String.Empty);
  724. } else {
  725. string prefix = value.Substring (0, lastColon);
  726. ns = reader.LookupNamespace (prefix);
  727. if (ns == null)
  728. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  729. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  730. }
  731. return new XmlQualifiedName (name, ns);
  732. }
  733. protected void UnknownAttribute (object o, XmlAttribute attr)
  734. {
  735. int line_number, line_position;
  736. if (Reader is XmlTextReader){
  737. line_number = ((XmlTextReader)Reader).LineNumber;
  738. line_position = ((XmlTextReader)Reader).LinePosition;
  739. } else {
  740. line_number = 0;
  741. line_position = 0;
  742. }
  743. if (eventSource != null)
  744. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  745. }
  746. protected void UnknownElement (object o, XmlElement elem)
  747. {
  748. int line_number, line_position;
  749. if (Reader is XmlTextReader){
  750. line_number = ((XmlTextReader)Reader).LineNumber;
  751. line_position = ((XmlTextReader)Reader).LinePosition;
  752. } else {
  753. line_number = 0;
  754. line_position = 0;
  755. }
  756. if (eventSource != null)
  757. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  758. }
  759. protected void UnknownNode (object o)
  760. {
  761. int line_number, line_position;
  762. if (Reader is XmlTextReader){
  763. line_number = ((XmlTextReader)Reader).LineNumber;
  764. line_position = ((XmlTextReader)Reader).LinePosition;
  765. } else {
  766. line_number = 0;
  767. line_position = 0;
  768. }
  769. if (eventSource != null)
  770. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  771. if (Reader.NodeType == XmlNodeType.Attribute)
  772. {
  773. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  774. UnknownAttribute (o, att);
  775. return;
  776. }
  777. else if (Reader.NodeType == XmlNodeType.Element)
  778. {
  779. XmlElement elem = (XmlElement) ReadXmlNode (false);
  780. UnknownElement (o, elem);
  781. return;
  782. }
  783. else
  784. {
  785. Reader.Skip();
  786. if (Reader.ReadState == ReadState.EndOfFile)
  787. throw new InvalidOperationException ("End of document found");
  788. }
  789. }
  790. protected void UnreferencedObject (string id, object o)
  791. {
  792. if (eventSource != null)
  793. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  794. }
  795. #endregion // Methods
  796. class WriteCallbackInfo
  797. {
  798. public Type Type;
  799. public string TypeName;
  800. public string TypeNs;
  801. public XmlSerializationReadCallback Callback;
  802. }
  803. protected class CollectionFixup {
  804. XmlSerializationCollectionFixupCallback callback;
  805. object collection;
  806. object collectionItems;
  807. string id;
  808. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  809. {
  810. this.callback = callback;
  811. this.collection = collection;
  812. this.id = id;
  813. }
  814. public XmlSerializationCollectionFixupCallback Callback {
  815. get { return callback; }
  816. }
  817. public object Collection {
  818. get { return collection; }
  819. }
  820. public object Id {
  821. get { return id; }
  822. }
  823. internal object CollectionItems
  824. {
  825. get { return collectionItems; }
  826. set { collectionItems = value; }
  827. }
  828. }
  829. protected class Fixup {
  830. object source;
  831. string[] ids;
  832. XmlSerializationFixupCallback callback;
  833. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  834. {
  835. this.source = o;
  836. this.callback = callback;
  837. this.ids = new string[count];
  838. }
  839. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  840. {
  841. this.source = o;
  842. this.ids = ids;
  843. this.callback = callback;
  844. }
  845. public XmlSerializationFixupCallback Callback {
  846. get { return callback; }
  847. }
  848. public string[] Ids {
  849. get { return ids; }
  850. }
  851. public object Source {
  852. get { return source; }
  853. set { source = value; }
  854. }
  855. }
  856. protected class CollectionItemFixup
  857. {
  858. Array list;
  859. int index;
  860. string id;
  861. public CollectionItemFixup (Array list, int index, string id)
  862. {
  863. this.list = list;
  864. this.index = index;
  865. this.id = id;
  866. }
  867. public Array Collection
  868. {
  869. get { return list; }
  870. }
  871. public int Index
  872. {
  873. get { return index; }
  874. }
  875. public string Id
  876. {
  877. get { return id; }
  878. }
  879. }
  880. #if NET_2_0
  881. [MonoTODO]
  882. protected bool DecodeName
  883. {
  884. get { throw new NotImplementedException(); }
  885. set { throw new NotImplementedException(); }
  886. }
  887. [MonoTODO]
  888. protected string CollapseWhitespace (string value)
  889. {
  890. throw new NotImplementedException ();
  891. }
  892. [MonoTODO]
  893. protected Exception CreateBadDeriveationException (
  894. string xsdDerived,
  895. string nsDerived,
  896. string xsdBase,
  897. string nsBase,
  898. string clrDerived,
  899. string clrBase)
  900. {
  901. throw new NotImplementedException ();
  902. }
  903. [MonoTODO]
  904. protected Exception CreateInvalidCastException (Type type, object value, string id)
  905. {
  906. throw new NotImplementedException ();
  907. }
  908. [MonoTODO]
  909. protected Exception CreateMissingIXmlSerializableType (string name, string ns, string clrType)
  910. {
  911. throw new NotImplementedException ();
  912. }
  913. [MonoTODO]
  914. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable, bool wrapped)
  915. {
  916. throw new NotImplementedException ();
  917. }
  918. [MonoTODO]
  919. protected string ReadString (string value, bool trim)
  920. {
  921. throw new NotImplementedException ();
  922. }
  923. [MonoTODO]
  924. protected object ReadTypedNull (XmlQualifiedName type)
  925. {
  926. throw new NotImplementedException ();
  927. }
  928. [MonoTODO]
  929. protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
  930. {
  931. throw new NotImplementedException ();
  932. }
  933. #endif
  934. }
  935. }