2
0

XmlSerializationReader.cs 22 KB

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