XmlSerializationReader.cs 24 KB

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