XmlSerializationReader.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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) return null;
  242. int i = typeName.IndexOf (":");
  243. if (i == -1) return new XmlQualifiedName (typeName, Reader.NamespaceURI);
  244. else
  245. {
  246. string prefix = typeName.Substring(0,i);
  247. string name = typeName.Substring (i+1);
  248. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  249. }
  250. }
  251. protected abstract void InitCallbacks ();
  252. protected abstract void InitIDs ();
  253. protected bool IsXmlnsAttribute (string name)
  254. {
  255. int length = name.Length;
  256. if (length < 5)
  257. return false;
  258. if (length == 5)
  259. return (name == "xmlns");
  260. return name.StartsWith ("xmlns:");
  261. }
  262. protected void ParseWsdlArrayType (XmlAttribute attr)
  263. {
  264. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace && attr.LocalName == "arrayType")
  265. {
  266. string ns = "", type, dimensions;
  267. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  268. if (ns != "") ns = Reader.LookupNamespace (ns) + ":";
  269. attr.Value = ns + type + dimensions;
  270. }
  271. }
  272. protected XmlQualifiedName ReadElementQualifiedName ()
  273. {
  274. if (reader.IsEmptyElement) {
  275. reader.Skip();
  276. return ToXmlQualifiedName (String.Empty);
  277. }
  278. reader.ReadStartElement ();
  279. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  280. reader.ReadEndElement ();
  281. return xqn;
  282. }
  283. protected void ReadEndElement ()
  284. {
  285. while (reader.NodeType == XmlNodeType.Whitespace)
  286. reader.Skip ();
  287. if (reader.NodeType != XmlNodeType.None) {
  288. reader.ReadEndElement ();
  289. } else {
  290. reader.Skip ();
  291. }
  292. }
  293. protected bool ReadNull ()
  294. {
  295. if (!GetNullAttr ())
  296. return false;
  297. if (reader.IsEmptyElement) {
  298. reader.Skip();
  299. return true;
  300. }
  301. reader.ReadStartElement();
  302. while (reader.NodeType != XmlNodeType.EndElement)
  303. UnknownNode (null);
  304. ReadEndElement ();
  305. return true;
  306. }
  307. protected XmlQualifiedName ReadNullableQualifiedName ()
  308. {
  309. if (ReadNull ())
  310. return null;
  311. return ReadElementQualifiedName ();
  312. }
  313. protected string ReadNullableString ()
  314. {
  315. if (ReadNull ())
  316. return null;
  317. return reader.ReadElementString ();
  318. }
  319. protected bool ReadReference (out string fixupReference)
  320. {
  321. string href = reader.GetAttribute ("href");
  322. if (href == null) {
  323. fixupReference = null;
  324. return false;
  325. }
  326. if (href [0] != '#')
  327. throw new InvalidOperationException("href not found: " + href);
  328. fixupReference = href.Substring (1);
  329. if (!reader.IsEmptyElement) {
  330. reader.ReadStartElement ();
  331. ReadEndElement ();
  332. } else {
  333. reader.Skip ();
  334. }
  335. return true;
  336. }
  337. protected object ReadReferencedElement ()
  338. {
  339. return ReadReferencedElement (Reader.LocalName, Reader.NamespaceURI);
  340. }
  341. WriteCallbackInfo GetCallbackInfo (XmlQualifiedName qname)
  342. {
  343. if (typesCallbacks == null)
  344. {
  345. typesCallbacks = new Hashtable ();
  346. InitCallbacks ();
  347. }
  348. return (WriteCallbackInfo) typesCallbacks[qname];
  349. }
  350. protected object ReadReferencedElement (string name, string ns)
  351. {
  352. XmlQualifiedName qname = GetXsiType ();
  353. if (qname == null) qname = new XmlQualifiedName (name, ns);
  354. string id = Reader.GetAttribute ("id");
  355. object ob;
  356. if (qname == arrayQName)
  357. {
  358. CollectionFixup fixup = (collFixups != null) ? (CollectionFixup) collFixups[id] : null;
  359. if (ReadList (out ob))
  360. {
  361. // List complete (does not contain references)
  362. if (fixup != null)
  363. {
  364. fixup.Callback (fixup.Collection, ob);
  365. collFixups.Remove (id);
  366. ob = fixup.Collection;
  367. }
  368. }
  369. else if (fixup != null)
  370. {
  371. fixup.CollectionItems = (object[])ob;
  372. ob = fixup.Collection;
  373. }
  374. }
  375. else
  376. {
  377. WriteCallbackInfo info = GetCallbackInfo (qname);
  378. if (info == null)
  379. ob = ReadTypedPrimitive (qname);
  380. else
  381. ob = info.Callback();
  382. }
  383. AddTarget (id, ob);
  384. return ob;
  385. }
  386. bool ReadList (out object resultList)
  387. {
  388. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  389. if (arrayType == null) arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.WsdlNamespace);
  390. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  391. int i = qn.Name.LastIndexOf ('[');
  392. string dim = qn.Name.Substring (i);
  393. string itemType = qn.Name.Substring (0,i);
  394. int count = Int32.Parse (dim.Substring (1, dim.Length - 2));
  395. Array list;
  396. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  397. string baseType = itemType.Substring (0,i);
  398. string arrayTypeName;
  399. if (qn.Namespace == XmlSchema.Namespace)
  400. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  401. else
  402. {
  403. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  404. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  405. }
  406. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  407. bool listComplete = true;
  408. Reader.ReadStartElement ();
  409. for (int n=0; n<count; n++)
  410. {
  411. Reader.MoveToContent ();
  412. string refid = Reader.GetAttribute ("href");
  413. string id;
  414. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  415. if (id == null)
  416. list.SetValue (item,n);
  417. else
  418. {
  419. AddFixup (new CollectionItemFixup (list, n, id));
  420. listComplete = false;
  421. }
  422. }
  423. Reader.ReadEndElement ();
  424. resultList = list;
  425. return listComplete;
  426. }
  427. protected void ReadReferencedElements ()
  428. {
  429. reader.MoveToContent();
  430. XmlNodeType nt = reader.NodeType;
  431. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  432. ReadReferencedElement ();
  433. reader.MoveToContent ();
  434. nt = reader.NodeType;
  435. }
  436. // Registers delayed list
  437. if (delayedListFixups != null)
  438. {
  439. foreach (DictionaryEntry entry in delayedListFixups)
  440. AddTarget ((string)entry.Key, entry.Value);
  441. }
  442. // Fix arrays
  443. if (collItemFixups != null)
  444. {
  445. foreach (CollectionItemFixup itemFixup in collItemFixups)
  446. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  447. }
  448. // Fills collections
  449. if (collFixups != null)
  450. {
  451. ICollection cfixups = collFixups.Values;
  452. foreach (CollectionFixup fixup in cfixups)
  453. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  454. }
  455. // Fills class instances
  456. if (fixups != null)
  457. {
  458. foreach (Fixup fixup in fixups)
  459. fixup.Callback (fixup);
  460. }
  461. }
  462. protected object ReadReferencingElement (out string fixupReference)
  463. {
  464. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  465. }
  466. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  467. {
  468. return ReadReferencingElement (name, ns, false, out fixupReference);
  469. }
  470. protected object ReadReferencingElement (string name,
  471. string ns,
  472. bool elementCanBeType,
  473. out string fixupReference)
  474. {
  475. if (ReadNull ())
  476. {
  477. fixupReference = null;
  478. return null;
  479. }
  480. string refid = Reader.GetAttribute ("href");
  481. if (refid == string.Empty || refid == null)
  482. {
  483. fixupReference = null;
  484. XmlQualifiedName qname = GetXsiType ();
  485. if (qname == null) qname = new XmlQualifiedName (name, ns);
  486. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  487. if (qname == arrayQName || arrayType != null)
  488. {
  489. delayedListFixups = EnsureHashtable (delayedListFixups);
  490. fixupReference = "__<" + (delayedFixupId++) + ">";
  491. object items;
  492. ReadList (out items);
  493. delayedListFixups [fixupReference] = items;
  494. return null;
  495. }
  496. else
  497. {
  498. WriteCallbackInfo info = GetCallbackInfo (qname);
  499. if (info == null)
  500. return ReadTypedPrimitive (qname);
  501. else
  502. return info.Callback();
  503. }
  504. }
  505. else
  506. {
  507. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  508. Reader.ReadStartElement ();
  509. if (TargetReady (refid))
  510. {
  511. fixupReference = null;
  512. return GetTarget (refid);
  513. }
  514. else
  515. {
  516. fixupReference = refid;
  517. return null;
  518. }
  519. }
  520. }
  521. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  522. {
  523. if (ReadNull ()) return null;
  524. int depth = reader.Depth;
  525. serializable.ReadXml (reader);
  526. Reader.MoveToContent ();
  527. while (reader.Depth > depth)
  528. reader.Skip ();
  529. if (reader.Depth == depth && reader.NodeType == XmlNodeType.EndElement)
  530. reader.ReadEndElement ();
  531. return serializable;
  532. }
  533. protected string ReadString (string value)
  534. {
  535. if (value == null || value == String.Empty)
  536. return reader.ReadString ();
  537. return (value + reader.ReadString ());
  538. }
  539. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  540. {
  541. if (qname == null) qname = GetXsiType ();
  542. TypeData typeData = TypeTranslator.FindPrimitiveTypeData (qname.Name);
  543. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive)
  544. {
  545. // Put everything into a node array
  546. XmlNode node = Document.ReadNode (reader);
  547. XmlElement elem = node as XmlElement;
  548. if (elem == null)
  549. return new XmlNode[] {node};
  550. else {
  551. XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];
  552. int n = 0;
  553. foreach (XmlNode no in elem.Attributes)
  554. nodes[n++] = no;
  555. foreach (XmlNode no in elem.ChildNodes)
  556. nodes[n++] = no;
  557. return nodes;
  558. }
  559. }
  560. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  561. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  562. }
  563. protected XmlNode ReadXmlNode (bool wrapped)
  564. {
  565. XmlNode node = Document.ReadNode (reader);
  566. if (wrapped)
  567. return node.FirstChild;
  568. else
  569. return node;
  570. }
  571. protected XmlDocument ReadXmlDocument (bool wrapped)
  572. {
  573. if (wrapped)
  574. reader.ReadStartElement ();
  575. XmlDocument doc = new XmlDocument ();
  576. XmlNode node = doc.ReadNode (reader);
  577. doc.AppendChild (node);
  578. if (wrapped)
  579. reader.ReadEndElement ();
  580. return doc;
  581. }
  582. [MonoTODO ("Implement")]
  583. protected void Referenced (object o)
  584. {
  585. }
  586. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  587. {
  588. if (length == 0 && isNullable) return null;
  589. if (a == null) return Array.CreateInstance (elementType, length);
  590. if (a.Length == length) return a;
  591. Array result = Array.CreateInstance (elementType, length);
  592. Array.Copy (a, result, length);
  593. return result;
  594. }
  595. protected byte[] ToByteArrayBase64 (bool isNull)
  596. {
  597. return Convert.FromBase64String (Reader.ReadString());
  598. }
  599. [MonoTODO ("Implement")]
  600. protected static byte[] ToByteArrayBase64 (string value)
  601. {
  602. throw new NotImplementedException ();
  603. }
  604. [MonoTODO ("Implement")]
  605. protected byte[] ToByteArrayHex (bool isNull)
  606. {
  607. throw new NotImplementedException ();
  608. }
  609. [MonoTODO ("Implement")]
  610. protected static byte[] ToByteArrayHex (string value)
  611. {
  612. throw new NotImplementedException ();
  613. }
  614. protected static char ToChar (string value)
  615. {
  616. return XmlCustomFormatter.ToChar (value);
  617. }
  618. protected static DateTime ToDate (string value)
  619. {
  620. return XmlCustomFormatter.ToDate (value);
  621. }
  622. protected static DateTime ToDateTime (string value)
  623. {
  624. return XmlCustomFormatter.ToDateTime (value);
  625. }
  626. protected static long ToEnum (string value, Hashtable h, string typeName)
  627. {
  628. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  629. }
  630. protected static DateTime ToTime (string value)
  631. {
  632. return XmlCustomFormatter.ToTime (value);
  633. }
  634. protected static string ToXmlName (string value)
  635. {
  636. return XmlCustomFormatter.ToXmlName (value);
  637. }
  638. protected static string ToXmlNCName (string value)
  639. {
  640. return XmlCustomFormatter.ToXmlNCName (value);
  641. }
  642. protected static string ToXmlNmToken (string value)
  643. {
  644. return XmlCustomFormatter.ToXmlNmToken (value);
  645. }
  646. protected static string ToXmlNmTokens (string value)
  647. {
  648. return XmlCustomFormatter.ToXmlNmTokens (value);
  649. }
  650. protected XmlQualifiedName ToXmlQualifiedName (string value)
  651. {
  652. string name;
  653. string ns;
  654. int lastColon = value.LastIndexOf (':');
  655. string decodedValue = XmlConvert.DecodeName (value);
  656. if (lastColon < 0) {
  657. name = reader.NameTable.Add (decodedValue);
  658. ns = reader.LookupNamespace (String.Empty);
  659. } else {
  660. string prefix = value.Substring (0, lastColon);
  661. ns = reader.LookupNamespace (prefix);
  662. if (ns == null)
  663. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  664. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  665. }
  666. return new XmlQualifiedName (name, ns);
  667. }
  668. protected void UnknownAttribute (object o, XmlAttribute attr)
  669. {
  670. int line_number, line_position;
  671. if (Reader is XmlTextReader){
  672. line_number = ((XmlTextReader)Reader).LineNumber;
  673. line_position = ((XmlTextReader)Reader).LinePosition;
  674. } else {
  675. line_number = 0;
  676. line_position = 0;
  677. }
  678. if (eventSource != null)
  679. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  680. }
  681. protected void UnknownElement (object o, XmlElement elem)
  682. {
  683. int line_number, line_position;
  684. if (Reader is XmlTextReader){
  685. line_number = ((XmlTextReader)Reader).LineNumber;
  686. line_position = ((XmlTextReader)Reader).LinePosition;
  687. } else {
  688. line_number = 0;
  689. line_position = 0;
  690. }
  691. if (eventSource != null)
  692. eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  693. }
  694. protected void UnknownNode (object o)
  695. {
  696. int line_number, line_position;
  697. if (Reader is XmlTextReader){
  698. line_number = ((XmlTextReader)Reader).LineNumber;
  699. line_position = ((XmlTextReader)Reader).LinePosition;
  700. } else {
  701. line_number = 0;
  702. line_position = 0;
  703. }
  704. if (eventSource != null)
  705. eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  706. if (Reader.NodeType == XmlNodeType.Attribute)
  707. {
  708. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  709. UnknownAttribute (o, att);
  710. return;
  711. }
  712. else if (Reader.NodeType == XmlNodeType.Element)
  713. {
  714. XmlElement elem = (XmlElement) ReadXmlNode (false);
  715. UnknownElement (o, elem);
  716. return;
  717. }
  718. else
  719. {
  720. Reader.Skip();
  721. if (Reader.ReadState == ReadState.EndOfFile)
  722. throw new InvalidOperationException ("End of document found");
  723. }
  724. }
  725. protected void UnreferencedObject (string id, object o)
  726. {
  727. if (eventSource != null)
  728. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  729. }
  730. #endregion // Methods
  731. class WriteCallbackInfo
  732. {
  733. public Type Type;
  734. public string TypeName;
  735. public string TypeNs;
  736. public XmlSerializationReadCallback Callback;
  737. }
  738. protected class CollectionFixup {
  739. XmlSerializationCollectionFixupCallback callback;
  740. object collection;
  741. object collectionItems;
  742. string id;
  743. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  744. {
  745. this.callback = callback;
  746. this.collection = collection;
  747. this.id = id;
  748. }
  749. public XmlSerializationCollectionFixupCallback Callback {
  750. get { return callback; }
  751. }
  752. public object Collection {
  753. get { return collection; }
  754. }
  755. public object Id {
  756. get { return id; }
  757. }
  758. internal object CollectionItems
  759. {
  760. get { return collectionItems; }
  761. set { collectionItems = value; }
  762. }
  763. }
  764. protected class Fixup {
  765. object source;
  766. string[] ids;
  767. XmlSerializationFixupCallback callback;
  768. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  769. {
  770. this.source = o;
  771. this.callback = callback;
  772. this.ids = new string[count];
  773. }
  774. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  775. {
  776. this.source = o;
  777. this.ids = ids;
  778. this.callback = callback;
  779. }
  780. public XmlSerializationFixupCallback Callback {
  781. get { return callback; }
  782. }
  783. public string[] Ids {
  784. get { return ids; }
  785. }
  786. public object Source {
  787. get { return source; }
  788. set { source = value; }
  789. }
  790. }
  791. protected class CollectionItemFixup
  792. {
  793. Array list;
  794. int index;
  795. string id;
  796. public CollectionItemFixup (Array list, int index, string id)
  797. {
  798. this.list = list;
  799. this.index = index;
  800. this.id = id;
  801. }
  802. public Array Collection
  803. {
  804. get { return list; }
  805. }
  806. public int Index
  807. {
  808. get { return index; }
  809. }
  810. public string Id
  811. {
  812. get { return id; }
  813. }
  814. }
  815. }
  816. }