XmlSerializationReader.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 id;
  477. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  478. if (id == null)
  479. list.SetValue (item,n);
  480. else
  481. {
  482. AddFixup (new CollectionItemFixup (list, n, id));
  483. listComplete = false;
  484. }
  485. }
  486. Reader.ReadEndElement ();
  487. }
  488. resultList = list;
  489. return listComplete;
  490. }
  491. protected void ReadReferencedElements ()
  492. {
  493. reader.MoveToContent();
  494. XmlNodeType nt = reader.NodeType;
  495. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  496. ReadReferencedElement ();
  497. reader.MoveToContent ();
  498. nt = reader.NodeType;
  499. }
  500. // Registers delayed list
  501. if (delayedListFixups != null)
  502. {
  503. foreach (DictionaryEntry entry in delayedListFixups)
  504. AddTarget ((string)entry.Key, entry.Value);
  505. }
  506. // Fix arrays
  507. if (collItemFixups != null)
  508. {
  509. foreach (CollectionItemFixup itemFixup in collItemFixups)
  510. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  511. }
  512. // Fills collections
  513. if (collFixups != null)
  514. {
  515. ICollection cfixups = collFixups.Values;
  516. foreach (CollectionFixup fixup in cfixups)
  517. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  518. }
  519. // Fills class instances
  520. if (fixups != null)
  521. {
  522. foreach (Fixup fixup in fixups)
  523. fixup.Callback (fixup);
  524. }
  525. }
  526. protected object ReadReferencingElement (out string fixupReference)
  527. {
  528. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  529. }
  530. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  531. {
  532. return ReadReferencingElement (name, ns, false, out fixupReference);
  533. }
  534. protected object ReadReferencingElement (string name,
  535. string ns,
  536. bool elementCanBeType,
  537. out string fixupReference)
  538. {
  539. if (ReadNull ())
  540. {
  541. fixupReference = null;
  542. return null;
  543. }
  544. string refid = Reader.GetAttribute ("href");
  545. if (refid == string.Empty || refid == null)
  546. {
  547. fixupReference = null;
  548. XmlQualifiedName qname = GetXsiType ();
  549. if (qname == null) qname = new XmlQualifiedName (name, ns);
  550. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  551. if (qname == arrayQName || arrayType != null)
  552. {
  553. delayedListFixups = EnsureHashtable (delayedListFixups);
  554. fixupReference = "__<" + (delayedFixupId++) + ">";
  555. object items;
  556. ReadList (out items);
  557. delayedListFixups [fixupReference] = items;
  558. return null;
  559. }
  560. else
  561. {
  562. WriteCallbackInfo info = GetCallbackInfo (qname);
  563. if (info == null)
  564. return ReadTypedPrimitive (qname);
  565. else
  566. return info.Callback();
  567. }
  568. }
  569. else
  570. {
  571. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  572. Reader.ReadStartElement ();
  573. if (TargetReady (refid))
  574. {
  575. fixupReference = null;
  576. return GetTarget (refid);
  577. }
  578. else
  579. {
  580. fixupReference = refid;
  581. return null;
  582. }
  583. }
  584. }
  585. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  586. {
  587. if (ReadNull ()) return null;
  588. int depth = reader.Depth;
  589. serializable.ReadXml (reader);
  590. Reader.MoveToContent ();
  591. while (reader.Depth > depth)
  592. reader.Skip ();
  593. if (reader.Depth == depth && reader.NodeType == XmlNodeType.EndElement)
  594. reader.ReadEndElement ();
  595. return serializable;
  596. }
  597. protected string ReadString (string value)
  598. {
  599. if (value == null || value == String.Empty)
  600. return reader.ReadString ();
  601. return (value + reader.ReadString ());
  602. }
  603. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  604. {
  605. if (qname == null) qname = GetXsiType ();
  606. TypeData typeData = TypeTranslator.FindPrimitiveTypeData (qname.Name);
  607. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive)
  608. {
  609. // Put everything into a node array
  610. XmlNode node = Document.ReadNode (reader);
  611. XmlElement elem = node as XmlElement;
  612. if (elem == null)
  613. return new XmlNode[] {node};
  614. else {
  615. XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];
  616. int n = 0;
  617. foreach (XmlNode no in elem.Attributes)
  618. nodes[n++] = no;
  619. foreach (XmlNode no in elem.ChildNodes)
  620. nodes[n++] = no;
  621. return nodes;
  622. }
  623. }
  624. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  625. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  626. }
  627. protected XmlNode ReadXmlNode (bool wrapped)
  628. {
  629. XmlNode node = Document.ReadNode (reader);
  630. if (wrapped)
  631. return node.FirstChild;
  632. else
  633. return node;
  634. }
  635. protected XmlDocument ReadXmlDocument (bool wrapped)
  636. {
  637. if (wrapped)
  638. reader.ReadStartElement ();
  639. XmlDocument doc = new XmlDocument ();
  640. XmlNode node = doc.ReadNode (reader);
  641. doc.AppendChild (node);
  642. if (wrapped)
  643. reader.ReadEndElement ();
  644. return doc;
  645. }
  646. [MonoTODO ("Implement")]
  647. protected void Referenced (object o)
  648. {
  649. }
  650. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  651. {
  652. if (length == 0 && isNullable) return null;
  653. if (a == null) return Array.CreateInstance (elementType, length);
  654. if (a.Length == length) return a;
  655. Array result = Array.CreateInstance (elementType, length);
  656. Array.Copy (a, result, length);
  657. return result;
  658. }
  659. protected byte[] ToByteArrayBase64 (bool isNull)
  660. {
  661. return Convert.FromBase64String (Reader.ReadString());
  662. }
  663. [MonoTODO ("Implement")]
  664. protected static byte[] ToByteArrayBase64 (string value)
  665. {
  666. throw new NotImplementedException ();
  667. }
  668. [MonoTODO ("Implement")]
  669. protected byte[] ToByteArrayHex (bool isNull)
  670. {
  671. throw new NotImplementedException ();
  672. }
  673. [MonoTODO ("Implement")]
  674. protected static byte[] ToByteArrayHex (string value)
  675. {
  676. throw new NotImplementedException ();
  677. }
  678. protected static char ToChar (string value)
  679. {
  680. return XmlCustomFormatter.ToChar (value);
  681. }
  682. protected static DateTime ToDate (string value)
  683. {
  684. return XmlCustomFormatter.ToDate (value);
  685. }
  686. protected static DateTime ToDateTime (string value)
  687. {
  688. return XmlCustomFormatter.ToDateTime (value);
  689. }
  690. protected static long ToEnum (string value, Hashtable h, string typeName)
  691. {
  692. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  693. }
  694. protected static DateTime ToTime (string value)
  695. {
  696. return XmlCustomFormatter.ToTime (value);
  697. }
  698. protected static string ToXmlName (string value)
  699. {
  700. return XmlCustomFormatter.ToXmlName (value);
  701. }
  702. protected static string ToXmlNCName (string value)
  703. {
  704. return XmlCustomFormatter.ToXmlNCName (value);
  705. }
  706. protected static string ToXmlNmToken (string value)
  707. {
  708. return XmlCustomFormatter.ToXmlNmToken (value);
  709. }
  710. protected static string ToXmlNmTokens (string value)
  711. {
  712. return XmlCustomFormatter.ToXmlNmTokens (value);
  713. }
  714. protected XmlQualifiedName ToXmlQualifiedName (string value)
  715. {
  716. string name;
  717. string ns;
  718. int lastColon = value.LastIndexOf (':');
  719. string decodedValue = XmlConvert.DecodeName (value);
  720. if (lastColon < 0) {
  721. name = reader.NameTable.Add (decodedValue);
  722. ns = reader.LookupNamespace (String.Empty);
  723. } else {
  724. string prefix = value.Substring (0, lastColon);
  725. ns = reader.LookupNamespace (prefix);
  726. if (ns == null)
  727. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  728. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  729. }
  730. return new XmlQualifiedName (name, ns);
  731. }
  732. protected void UnknownAttribute (object o, XmlAttribute attr)
  733. {
  734. int line_number, line_position;
  735. if (Reader is XmlTextReader){
  736. line_number = ((XmlTextReader)Reader).LineNumber;
  737. line_position = ((XmlTextReader)Reader).LinePosition;
  738. } else {
  739. line_number = 0;
  740. line_position = 0;
  741. }
  742. if (eventSource != null)
  743. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  744. }
  745. protected void UnknownElement (object o, XmlElement elem)
  746. {
  747. int line_number, line_position;
  748. if (Reader is XmlTextReader){
  749. line_number = ((XmlTextReader)Reader).LineNumber;
  750. line_position = ((XmlTextReader)Reader).LinePosition;
  751. } else {
  752. line_number = 0;
  753. line_position = 0;
  754. }
  755. if (eventSource != null)
  756. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  757. }
  758. protected void UnknownNode (object o)
  759. {
  760. int line_number, line_position;
  761. if (Reader is XmlTextReader){
  762. line_number = ((XmlTextReader)Reader).LineNumber;
  763. line_position = ((XmlTextReader)Reader).LinePosition;
  764. } else {
  765. line_number = 0;
  766. line_position = 0;
  767. }
  768. if (eventSource != null)
  769. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  770. if (Reader.NodeType == XmlNodeType.Attribute)
  771. {
  772. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  773. UnknownAttribute (o, att);
  774. return;
  775. }
  776. else if (Reader.NodeType == XmlNodeType.Element)
  777. {
  778. XmlElement elem = (XmlElement) ReadXmlNode (false);
  779. UnknownElement (o, elem);
  780. return;
  781. }
  782. else
  783. {
  784. Reader.Skip();
  785. if (Reader.ReadState == ReadState.EndOfFile)
  786. throw new InvalidOperationException ("End of document found");
  787. }
  788. }
  789. protected void UnreferencedObject (string id, object o)
  790. {
  791. if (eventSource != null)
  792. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  793. }
  794. #endregion // Methods
  795. class WriteCallbackInfo
  796. {
  797. public Type Type;
  798. public string TypeName;
  799. public string TypeNs;
  800. public XmlSerializationReadCallback Callback;
  801. }
  802. protected class CollectionFixup {
  803. XmlSerializationCollectionFixupCallback callback;
  804. object collection;
  805. object collectionItems;
  806. string id;
  807. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  808. {
  809. this.callback = callback;
  810. this.collection = collection;
  811. this.id = id;
  812. }
  813. public XmlSerializationCollectionFixupCallback Callback {
  814. get { return callback; }
  815. }
  816. public object Collection {
  817. get { return collection; }
  818. }
  819. public object Id {
  820. get { return id; }
  821. }
  822. internal object CollectionItems
  823. {
  824. get { return collectionItems; }
  825. set { collectionItems = value; }
  826. }
  827. }
  828. protected class Fixup {
  829. object source;
  830. string[] ids;
  831. XmlSerializationFixupCallback callback;
  832. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  833. {
  834. this.source = o;
  835. this.callback = callback;
  836. this.ids = new string[count];
  837. }
  838. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  839. {
  840. this.source = o;
  841. this.ids = ids;
  842. this.callback = callback;
  843. }
  844. public XmlSerializationFixupCallback Callback {
  845. get { return callback; }
  846. }
  847. public string[] Ids {
  848. get { return ids; }
  849. }
  850. public object Source {
  851. get { return source; }
  852. set { source = value; }
  853. }
  854. }
  855. protected class CollectionItemFixup
  856. {
  857. Array list;
  858. int index;
  859. string id;
  860. public CollectionItemFixup (Array list, int index, string id)
  861. {
  862. this.list = list;
  863. this.index = index;
  864. this.id = id;
  865. }
  866. public Array Collection
  867. {
  868. get { return list; }
  869. }
  870. public int Index
  871. {
  872. get { return index; }
  873. }
  874. public string Id
  875. {
  876. get { return id; }
  877. }
  878. }
  879. #if NET_2_0
  880. [MonoTODO]
  881. protected bool DecodeName
  882. {
  883. get { throw new NotImplementedException(); }
  884. set { throw new NotImplementedException(); }
  885. }
  886. [MonoTODO]
  887. protected string CollapseWhitespace (string value)
  888. {
  889. throw new NotImplementedException ();
  890. }
  891. [MonoTODO]
  892. protected Exception CreateBadDeriveationException (
  893. string xsdDerived,
  894. string nsDerived,
  895. string xsdBase,
  896. string nsBase,
  897. string clrDerived,
  898. string clrBase)
  899. {
  900. throw new NotImplementedException ();
  901. }
  902. [MonoTODO]
  903. protected Exception CreateInvalidCastException (Type type, object value, string id)
  904. {
  905. throw new NotImplementedException ();
  906. }
  907. [MonoTODO]
  908. protected Exception CreateMissingIXmlSerializableType (string name, string ns, string clrType)
  909. {
  910. throw new NotImplementedException ();
  911. }
  912. [MonoTODO]
  913. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable, bool wrapped)
  914. {
  915. throw new NotImplementedException ();
  916. }
  917. [MonoTODO]
  918. protected string ReadString (string value, bool trim)
  919. {
  920. throw new NotImplementedException ();
  921. }
  922. [MonoTODO]
  923. protected object ReadTypedNull (XmlQualifiedName type)
  924. {
  925. throw new NotImplementedException ();
  926. }
  927. [MonoTODO]
  928. protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
  929. {
  930. throw new NotImplementedException ();
  931. }
  932. #endif
  933. }
  934. }