XmlSerializationReader.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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) throw CreateUnknownTypeException (qname);
  379. ob = info.Callback ();
  380. }
  381. AddTarget (id, ob);
  382. return ob;
  383. }
  384. bool ReadList (out object resultList)
  385. {
  386. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  387. if (arrayType == null) arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.WsdlNamespace);
  388. XmlQualifiedName qn = ToXmlQualifiedName (arrayType);
  389. int i = qn.Name.LastIndexOf ('[');
  390. string dim = qn.Name.Substring (i);
  391. string itemType = qn.Name.Substring (0,i);
  392. int count = Int32.Parse (dim.Substring (1, dim.Length - 2));
  393. Array list;
  394. i = itemType.IndexOf ('['); if (i == -1) i = itemType.Length;
  395. string baseType = itemType.Substring (0,i);
  396. string arrayTypeName;
  397. if (qn.Namespace == XmlSchema.Namespace)
  398. arrayTypeName = TypeTranslator.GetPrimitiveTypeData (baseType).Type.FullName + itemType.Substring (i);
  399. else
  400. {
  401. WriteCallbackInfo info = GetCallbackInfo (new XmlQualifiedName (baseType,qn.Namespace));
  402. arrayTypeName = info.Type.FullName + itemType.Substring (i) + ", " + info.Type.Assembly.FullName;
  403. }
  404. list = Array.CreateInstance (Type.GetType (arrayTypeName), count);
  405. bool listComplete = true;
  406. Reader.ReadStartElement ();
  407. for (int n=0; n<count; n++)
  408. {
  409. Reader.MoveToContent ();
  410. string refid = Reader.GetAttribute ("href");
  411. string id;
  412. object item = ReadReferencingElement (itemType, qn.Namespace, out id);
  413. if (id == null)
  414. list.SetValue (item,n);
  415. else
  416. {
  417. AddFixup (new CollectionItemFixup (list, n, id));
  418. listComplete = false;
  419. }
  420. }
  421. Reader.ReadEndElement ();
  422. resultList = list;
  423. return listComplete;
  424. }
  425. protected void ReadReferencedElements ()
  426. {
  427. reader.MoveToContent();
  428. XmlNodeType nt = reader.NodeType;
  429. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  430. ReadReferencedElement ();
  431. reader.MoveToContent ();
  432. nt = reader.NodeType;
  433. }
  434. // Registers delayed list
  435. if (delayedListFixups != null)
  436. {
  437. foreach (DictionaryEntry entry in delayedListFixups)
  438. AddTarget ((string)entry.Key, entry.Value);
  439. }
  440. // Fix arrays
  441. if (collItemFixups != null)
  442. {
  443. foreach (CollectionItemFixup itemFixup in collItemFixups)
  444. itemFixup.Collection.SetValue (GetTarget (itemFixup.Id), itemFixup.Index);
  445. }
  446. // Fills collections
  447. if (collFixups != null)
  448. {
  449. ICollection cfixups = collFixups.Values;
  450. foreach (CollectionFixup fixup in cfixups)
  451. fixup.Callback (fixup.Collection, fixup.CollectionItems);
  452. }
  453. // Fills class instances
  454. if (fixups != null)
  455. {
  456. foreach (Fixup fixup in fixups)
  457. fixup.Callback (fixup);
  458. }
  459. }
  460. protected object ReadReferencingElement (out string fixupReference)
  461. {
  462. return ReadReferencingElement (Reader.LocalName, Reader.NamespaceURI, false, out fixupReference);
  463. }
  464. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  465. {
  466. return ReadReferencingElement (name, ns, false, out fixupReference);
  467. }
  468. protected object ReadReferencingElement (string name,
  469. string ns,
  470. bool elementCanBeType,
  471. out string fixupReference)
  472. {
  473. if (ReadNull ())
  474. {
  475. fixupReference = null;
  476. return null;
  477. }
  478. string refid = Reader.GetAttribute ("href");
  479. if (refid == string.Empty || refid == null)
  480. {
  481. fixupReference = null;
  482. XmlQualifiedName qname = GetXsiType ();
  483. if (qname == null) qname = new XmlQualifiedName (name, ns);
  484. string arrayType = Reader.GetAttribute ("arrayType", XmlSerializer.EncodingNamespace);
  485. if (qname == arrayQName || arrayType != null)
  486. {
  487. delayedListFixups = EnsureHashtable (delayedListFixups);
  488. fixupReference = "__<" + (delayedFixupId++) + ">";
  489. object items;
  490. ReadList (out items);
  491. delayedListFixups [fixupReference] = items;
  492. return null;
  493. }
  494. else
  495. {
  496. WriteCallbackInfo info = GetCallbackInfo (qname);
  497. if (info == null)
  498. return ReadTypedPrimitive (qname);
  499. else
  500. return info.Callback();
  501. }
  502. }
  503. else
  504. {
  505. if (refid.StartsWith ("#")) refid = refid.Substring (1);
  506. Reader.ReadStartElement ();
  507. if (TargetReady (refid))
  508. {
  509. fixupReference = null;
  510. return GetTarget (refid);
  511. }
  512. else
  513. {
  514. fixupReference = refid;
  515. return null;
  516. }
  517. }
  518. }
  519. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  520. {
  521. if (ReadNull ()) return null;
  522. int depth = reader.Depth;
  523. serializable.ReadXml (reader);
  524. Reader.MoveToContent ();
  525. while (reader.Depth > depth)
  526. reader.Skip ();
  527. if (reader.Depth == depth && reader.NodeType == XmlNodeType.EndElement)
  528. reader.ReadEndElement ();
  529. return serializable;
  530. }
  531. protected string ReadString (string value)
  532. {
  533. if (value == null || value == String.Empty)
  534. return reader.ReadString ();
  535. return (value + reader.ReadString ());
  536. }
  537. protected object ReadTypedPrimitive (XmlQualifiedName qname)
  538. {
  539. if (qname == null) qname = GetXsiType ();
  540. TypeData typeData = TypeTranslator.GetPrimitiveTypeData (qname.Name);
  541. if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive)
  542. {
  543. // Put everything into a node array
  544. XmlNode node = Document.ReadNode (reader);
  545. XmlElement elem = node as XmlElement;
  546. if (elem == null)
  547. return new XmlNode[] {node};
  548. else {
  549. XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];
  550. int n = 0;
  551. foreach (XmlNode no in elem.Attributes)
  552. nodes[n++] = no;
  553. foreach (XmlNode no in elem.ChildNodes)
  554. nodes[n++] = no;
  555. return nodes;
  556. }
  557. }
  558. if (typeData.Type == typeof (XmlQualifiedName)) return ReadNullableQualifiedName ();
  559. return XmlCustomFormatter.FromXmlString (typeData, Reader.ReadElementString ());
  560. }
  561. protected XmlNode ReadXmlNode (bool wrapped)
  562. {
  563. XmlNode node = Document.ReadNode (reader);
  564. if (wrapped)
  565. return node.FirstChild;
  566. else
  567. return node;
  568. }
  569. protected XmlDocument ReadXmlDocument (bool wrapped)
  570. {
  571. if (wrapped)
  572. reader.ReadStartElement ();
  573. XmlDocument doc = new XmlDocument ();
  574. XmlNode node = doc.ReadNode (reader);
  575. doc.AppendChild (node);
  576. if (wrapped)
  577. reader.ReadEndElement ();
  578. return doc;
  579. }
  580. [MonoTODO ("Implement")]
  581. protected void Referenced (object o)
  582. {
  583. }
  584. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  585. {
  586. if (length == 0 && isNullable) return null;
  587. if (a == null) return Array.CreateInstance (elementType, length);
  588. if (a.Length == length) return a;
  589. Array result = Array.CreateInstance (elementType, length);
  590. Array.Copy (a, result, length);
  591. return result;
  592. }
  593. protected byte[] ToByteArrayBase64 (bool isNull)
  594. {
  595. return Convert.FromBase64String (Reader.ReadString());
  596. }
  597. [MonoTODO ("Implement")]
  598. protected static byte[] ToByteArrayBase64 (string value)
  599. {
  600. throw new NotImplementedException ();
  601. }
  602. [MonoTODO ("Implement")]
  603. protected byte[] ToByteArrayHex (bool isNull)
  604. {
  605. throw new NotImplementedException ();
  606. }
  607. [MonoTODO ("Implement")]
  608. protected static byte[] ToByteArrayHex (string value)
  609. {
  610. throw new NotImplementedException ();
  611. }
  612. protected static char ToChar (string value)
  613. {
  614. return XmlCustomFormatter.ToChar (value);
  615. }
  616. protected static DateTime ToDate (string value)
  617. {
  618. return XmlCustomFormatter.ToDate (value);
  619. }
  620. protected static DateTime ToDateTime (string value)
  621. {
  622. return XmlCustomFormatter.ToDateTime (value);
  623. }
  624. protected static long ToEnum (string value, Hashtable h, string typeName)
  625. {
  626. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  627. }
  628. protected static DateTime ToTime (string value)
  629. {
  630. return XmlCustomFormatter.ToTime (value);
  631. }
  632. protected static string ToXmlName (string value)
  633. {
  634. return XmlCustomFormatter.ToXmlName (value);
  635. }
  636. protected static string ToXmlNCName (string value)
  637. {
  638. return XmlCustomFormatter.ToXmlNCName (value);
  639. }
  640. protected static string ToXmlNmToken (string value)
  641. {
  642. return XmlCustomFormatter.ToXmlNmToken (value);
  643. }
  644. protected static string ToXmlNmTokens (string value)
  645. {
  646. return XmlCustomFormatter.ToXmlNmTokens (value);
  647. }
  648. protected XmlQualifiedName ToXmlQualifiedName (string value)
  649. {
  650. string name;
  651. string ns;
  652. int lastColon = value.LastIndexOf (':');
  653. string decodedValue = XmlConvert.DecodeName (value);
  654. if (lastColon < 0) {
  655. name = reader.NameTable.Add (decodedValue);
  656. ns = reader.LookupNamespace (String.Empty);
  657. } else {
  658. string prefix = value.Substring (0, lastColon);
  659. ns = reader.LookupNamespace (prefix);
  660. if (ns == null)
  661. throw new InvalidOperationException ("namespace " + prefix + " not defined");
  662. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  663. }
  664. return new XmlQualifiedName (name, ns);
  665. }
  666. protected void UnknownAttribute (object o, XmlAttribute attr)
  667. {
  668. int line_number, line_position;
  669. if (Reader is XmlTextReader){
  670. line_number = ((XmlTextReader)Reader).LineNumber;
  671. line_position = ((XmlTextReader)Reader).LinePosition;
  672. } else {
  673. line_number = 0;
  674. line_position = 0;
  675. }
  676. if (eventSource != null)
  677. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
  678. }
  679. protected void UnknownElement (object o, XmlElement elem)
  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.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
  691. }
  692. protected void UnknownNode (object o)
  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.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  704. if (Reader.NodeType == XmlNodeType.Attribute)
  705. {
  706. XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
  707. UnknownAttribute (o, att);
  708. return;
  709. }
  710. else if (Reader.NodeType == XmlNodeType.Element)
  711. {
  712. XmlElement elem = (XmlElement) ReadXmlNode (false);
  713. UnknownElement (o, elem);
  714. return;
  715. }
  716. else
  717. {
  718. Reader.Skip();
  719. if (Reader.ReadState == ReadState.EndOfFile)
  720. throw new InvalidOperationException ("End of document found");
  721. }
  722. }
  723. protected void UnreferencedObject (string id, object o)
  724. {
  725. if (eventSource != null)
  726. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  727. }
  728. #endregion // Methods
  729. class WriteCallbackInfo
  730. {
  731. public Type Type;
  732. public string TypeName;
  733. public string TypeNs;
  734. public XmlSerializationReadCallback Callback;
  735. }
  736. protected class CollectionFixup {
  737. XmlSerializationCollectionFixupCallback callback;
  738. object collection;
  739. object collectionItems;
  740. string id;
  741. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, string id)
  742. {
  743. this.callback = callback;
  744. this.collection = collection;
  745. this.id = id;
  746. }
  747. public XmlSerializationCollectionFixupCallback Callback {
  748. get { return callback; }
  749. }
  750. public object Collection {
  751. get { return collection; }
  752. }
  753. public object Id {
  754. get { return id; }
  755. }
  756. internal object CollectionItems
  757. {
  758. get { return collectionItems; }
  759. set { collectionItems = value; }
  760. }
  761. }
  762. protected class Fixup {
  763. object source;
  764. string[] ids;
  765. XmlSerializationFixupCallback callback;
  766. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  767. {
  768. this.source = o;
  769. this.callback = callback;
  770. this.ids = new string[count];
  771. }
  772. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  773. {
  774. this.source = o;
  775. this.ids = ids;
  776. this.callback = callback;
  777. }
  778. public XmlSerializationFixupCallback Callback {
  779. get { return callback; }
  780. }
  781. public string[] Ids {
  782. get { return ids; }
  783. }
  784. public object Source {
  785. get { return source; }
  786. set { source = value; }
  787. }
  788. }
  789. protected class CollectionItemFixup
  790. {
  791. Array list;
  792. int index;
  793. string id;
  794. public CollectionItemFixup (Array list, int index, string id)
  795. {
  796. this.list = list;
  797. this.index = index;
  798. this.id = id;
  799. }
  800. public Array Collection
  801. {
  802. get { return list; }
  803. }
  804. public int Index
  805. {
  806. get { return index; }
  807. }
  808. public string Id
  809. {
  810. get { return id; }
  811. }
  812. }
  813. }
  814. }