XmlSerializationReader.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. namespace System.Xml.Serialization {
  16. public abstract class XmlSerializationReader {
  17. #region Fields
  18. XmlDocument document;
  19. XmlReader reader;
  20. ArrayList fixups;
  21. ArrayList collFixups;
  22. Hashtable readCallbacks;
  23. Hashtable typesCallbacks;
  24. ArrayList noIDTargets;
  25. Hashtable targets;
  26. XmlSerializer eventSource;
  27. string w3SchemaNS;
  28. string w3SchemaNS2000;
  29. string w3SchemaNS1999;
  30. string w3InstanceNS;
  31. string w3InstanceNS2000;
  32. string w3InstanceNS1999;
  33. string soapNS;
  34. string schema;
  35. string wsdlNS;
  36. string wsdlArrayType;
  37. string nullX;
  38. string nil;
  39. string typeX;
  40. string arrayType;
  41. string anyType;
  42. #endregion
  43. internal void Initialize (XmlReader reader, XmlSerializer eventSource)
  44. {
  45. w3SchemaNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema");
  46. w3SchemaNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema");
  47. w3SchemaNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema");
  48. w3InstanceNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema-instance");
  49. w3InstanceNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema-instance");
  50. w3InstanceNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema-instance");
  51. soapNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/soap/encoding/");
  52. schema = reader.NameTable.Add ("schema");
  53. wsdlNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/wsdl/");
  54. wsdlArrayType = reader.NameTable.Add ("arrayType");
  55. nullX = reader.NameTable.Add ("null");
  56. nil = reader.NameTable.Add ("nil");
  57. typeX = reader.NameTable.Add ("type");
  58. arrayType = reader.NameTable.Add ("arrayType");
  59. anyType = reader.NameTable.Add ("anyType");
  60. this.reader = reader;
  61. this.eventSource = eventSource;
  62. InitIDs ();
  63. }
  64. internal abstract object ReadObject ();
  65. private ArrayList EnsureArrayList (ArrayList list)
  66. {
  67. if (list == null)
  68. list = new ArrayList ();
  69. return list;
  70. }
  71. private Hashtable EnsureHashtable (Hashtable hash)
  72. {
  73. if (hash == null)
  74. hash = new Hashtable ();
  75. return hash;
  76. }
  77. protected XmlSerializationReader ()
  78. {
  79. }
  80. protected XmlDocument Document
  81. {
  82. get {
  83. if (document == null)
  84. document = new XmlDocument (reader.NameTable);
  85. return document;
  86. }
  87. }
  88. protected XmlReader Reader {
  89. get { return reader; }
  90. }
  91. #region Methods
  92. protected void AddFixup (CollectionFixup fixup)
  93. {
  94. collFixups = EnsureArrayList (collFixups);
  95. collFixups.Add(fixup);
  96. }
  97. protected void AddFixup (Fixup fixup)
  98. {
  99. fixups = EnsureArrayList (fixups);
  100. fixups.Add(fixup);
  101. }
  102. protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
  103. {
  104. XmlNameTable nt = reader.NameTable;
  105. XmlQualifiedName xqn = new XmlQualifiedName (nt.Add (name), nt.Add (ns));
  106. readCallbacks = EnsureHashtable (readCallbacks);
  107. readCallbacks.Add (xqn, read);
  108. typesCallbacks = EnsureHashtable (typesCallbacks);
  109. typesCallbacks.Add (xqn, type);
  110. }
  111. protected void AddTarget (string id, object o)
  112. {
  113. if (id != null) {
  114. targets = EnsureHashtable (targets);
  115. if (targets [id] == null)
  116. targets.Add (id, o);
  117. } else {
  118. if (o != null)
  119. return;
  120. noIDTargets = EnsureArrayList (noIDTargets);
  121. noIDTargets.Add (o);
  122. }
  123. }
  124. private string CurrentTag ()
  125. {
  126. switch (reader.NodeType) {
  127. case XmlNodeType.None:
  128. return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
  129. reader.NamespaceURI);
  130. case XmlNodeType.Attribute:
  131. return reader.Value;
  132. case XmlNodeType.Text:
  133. return "CDATA";
  134. case XmlNodeType.ProcessingInstruction:
  135. return "<--";
  136. case XmlNodeType.Entity:
  137. return "<?";
  138. case XmlNodeType.EndElement:
  139. return ">";
  140. default:
  141. return "(unknown)";
  142. }
  143. }
  144. protected Exception CreateAbstractTypeException (string name, string ns)
  145. {
  146. string message = "Error at " + name + " " + ns + ":" + CurrentTag ();
  147. return new InvalidOperationException (message);
  148. }
  149. protected Exception CreateInvalidCastException (Type type, object value)
  150. {
  151. string message = String.Format ("Cannot assign object of type {0} to an object of " +
  152. "type {1}.", value.GetType (), type);
  153. return new InvalidCastException (message);
  154. }
  155. protected Exception CreateReadOnlyCollectionException (string name)
  156. {
  157. string message = String.Format ("Could not serialize {0}. Default constructors are " +
  158. "required for collections and enumerators.", name);
  159. return new InvalidOperationException (message);
  160. }
  161. protected Exception CreateUnknownConstantException (string value, Type enumType)
  162. {
  163. string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
  164. return new InvalidOperationException (message);
  165. }
  166. protected Exception CreateUnknownNodeException ()
  167. {
  168. string message = "Unknown xml node -> " + CurrentTag ();
  169. return new InvalidOperationException (message);
  170. }
  171. protected Exception CreateUnknownTypeException (XmlQualifiedName type)
  172. {
  173. string message = "Unknown type " + type.Namespace + ":" + type.Name + " " + CurrentTag ();
  174. return new InvalidOperationException (message);
  175. }
  176. protected Array EnsureArrayIndex (Array a, int index, Type elementType)
  177. {
  178. if (a != null && index < a.Length)
  179. return a;
  180. int size;
  181. if (a == null) {
  182. size = 32;
  183. } else {
  184. size = a.Length * 2;
  185. }
  186. Array result = Array.CreateInstance (elementType, size);
  187. if (a != null)
  188. Array.Copy (a, result, index);
  189. return result;
  190. }
  191. [MonoTODO ("Implement")]
  192. protected void FixupArrayRefs (object fixup)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO ("Implement")]
  197. protected int GetArrayLength (string name, string ns)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. protected bool GetNullAttr ()
  202. {
  203. string na = reader.GetAttribute (nullX, w3InstanceNS);
  204. if (na == string.Empty) {
  205. na = reader.GetAttribute (nil, w3InstanceNS);
  206. if (na == string.Empty) {
  207. na = reader.GetAttribute (nullX, w3InstanceNS2000);
  208. if (na == string.Empty)
  209. na = reader.GetAttribute (nullX, w3InstanceNS1999);
  210. }
  211. }
  212. return (na != string.Empty);
  213. }
  214. [MonoTODO ("Implement")]
  215. protected object GetTarget (string id)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. [MonoTODO ("Implement")]
  220. protected XmlQualifiedName GetXsiType ()
  221. {
  222. string typeName = Reader.GetAttribute ("xsi:type");
  223. if (typeName == string.Empty) return null;
  224. int i = typeName.IndexOf (":");
  225. if (i == -1) return new XmlQualifiedName (typeName, "");
  226. else
  227. {
  228. string prefix = typeName.Substring(0,i);
  229. string name = typeName.Substring (i+1);
  230. return new XmlQualifiedName (name, Reader.LookupNamespace (prefix));
  231. }
  232. }
  233. protected abstract void InitCallbacks ();
  234. protected abstract void InitIDs ();
  235. protected bool IsXmlnsAttribute (string name)
  236. {
  237. int length = name.Length;
  238. if (length < 5)
  239. return false;
  240. if (length == 5)
  241. return (name == "xmlns");
  242. return name.StartsWith ("xmlns:");
  243. }
  244. [MonoTODO ("Implement")]
  245. protected void ParseWsdlArrayType (XmlAttribute attr)
  246. {
  247. throw new NotImplementedException ();
  248. }
  249. protected XmlQualifiedName ReadElementQualifiedName ()
  250. {
  251. if (reader.IsEmptyElement) {
  252. reader.Skip();
  253. return ToXmlQualifiedName (String.Empty);
  254. }
  255. XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
  256. reader.ReadEndElement ();
  257. return xqn;
  258. }
  259. protected void ReadEndElement ()
  260. {
  261. while (reader.NodeType == XmlNodeType.Whitespace)
  262. reader.Skip ();
  263. if (reader.NodeType != XmlNodeType.None) {
  264. reader.ReadEndElement ();
  265. } else {
  266. reader.Skip ();
  267. }
  268. }
  269. protected bool ReadNull ()
  270. {
  271. if (!GetNullAttr ())
  272. return false;
  273. if (reader.IsEmptyElement) {
  274. reader.Skip();
  275. return true;
  276. }
  277. reader.ReadStartElement();
  278. while (reader.NodeType != XmlNodeType.EndElement)
  279. {
  280. UnknownNode (null);
  281. reader.Read ();
  282. }
  283. ReadEndElement ();
  284. return true;
  285. }
  286. protected XmlQualifiedName ReadNullableQualifiedName ()
  287. {
  288. if (ReadNull ())
  289. return null;
  290. return ReadElementQualifiedName ();
  291. }
  292. protected string ReadNullableString ()
  293. {
  294. if (ReadNull ())
  295. return null;
  296. return reader.ReadElementString ();
  297. }
  298. protected bool ReadReference (out string fixupReference)
  299. {
  300. string href = reader.GetAttribute ("href");
  301. if (href == null) {
  302. fixupReference = null;
  303. return false;
  304. }
  305. if (href [0] != '#')
  306. throw new InvalidOperationException("href not found: " + href);
  307. fixupReference = href.Substring (1);
  308. if (!reader.IsEmptyElement) {
  309. reader.ReadStartElement ();
  310. ReadEndElement ();
  311. } else {
  312. reader.Skip ();
  313. }
  314. return true;
  315. }
  316. protected object ReadReferencedElement ()
  317. {
  318. return ReadReferencedElement (null, null);
  319. }
  320. protected object ReadReferencedElement (string name, string ns)
  321. {
  322. string unused;
  323. return ReadReferencingElement (name, ns, false, out unused);
  324. }
  325. protected void ReadReferencedElements ()
  326. {
  327. string unused;
  328. reader.MoveToContent();
  329. XmlNodeType nt = reader.NodeType;
  330. while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
  331. ReadReferencingElement (null, null, true, out unused);
  332. reader.MoveToContent ();
  333. nt = reader.NodeType;
  334. }
  335. }
  336. [MonoTODO ("Implement")]
  337. protected object ReadReferencingElement (out string fixupReference)
  338. {
  339. return ReadReferencingElement (null, null, false, out fixupReference);
  340. }
  341. protected object ReadReferencingElement (string name, string ns, out string fixupReference)
  342. {
  343. return ReadReferencingElement (name, ns, false, out fixupReference);
  344. }
  345. [MonoTODO]
  346. protected object ReadReferencingElement (string name,
  347. string ns,
  348. bool elementCanBeType,
  349. out string fixupReference)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
  354. {
  355. serializable.ReadXml (reader);
  356. return serializable;
  357. }
  358. protected string ReadString (string value)
  359. {
  360. if (value == null || value == String.Empty)
  361. return reader.ReadString ();
  362. return (value + reader.ReadString ());
  363. }
  364. [MonoTODO ("Implement")]
  365. protected object ReadTypedPrimitive (XmlQualifiedName type)
  366. {
  367. throw new NotImplementedException ();
  368. }
  369. protected XmlNode ReadXmlNode (bool wrapped)
  370. {
  371. XmlNode node = Document.ReadNode (reader);
  372. if (wrapped)
  373. return node.FirstChild;
  374. else
  375. return node;
  376. }
  377. [MonoTODO ("Implement")]
  378. protected void Referenced (object o)
  379. {
  380. throw new NotImplementedException ();
  381. }
  382. protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
  383. {
  384. if (length == 0 && isNullable) return null;
  385. if (a == null) return Array.CreateInstance (elementType, length);
  386. if (a.Length == length) return a;
  387. Array result = Array.CreateInstance (elementType, length);
  388. Array.Copy (a, result, length);
  389. return result;
  390. }
  391. [MonoTODO ("Implement")]
  392. protected byte[] ToByteArrayBase64 (bool isNull)
  393. {
  394. throw new NotImplementedException ();
  395. }
  396. [MonoTODO ("Implement")]
  397. protected static byte[] ToByteArrayBase64 (string value)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. [MonoTODO ("Implement")]
  402. protected byte[] ToByteArrayHex (bool isNull)
  403. {
  404. throw new NotImplementedException ();
  405. }
  406. [MonoTODO ("Implement")]
  407. protected static byte[] ToByteArrayHex (string value)
  408. {
  409. throw new NotImplementedException ();
  410. }
  411. protected static char ToChar (string value)
  412. {
  413. return XmlCustomFormatter.ToChar (value);
  414. }
  415. protected static DateTime ToDate (string value)
  416. {
  417. return XmlCustomFormatter.ToDate (value);
  418. }
  419. protected static DateTime ToDateTime (string value)
  420. {
  421. return XmlCustomFormatter.ToDateTime (value);
  422. }
  423. protected static long ToEnum (string value, Hashtable h, string typeName)
  424. {
  425. return XmlCustomFormatter.ToEnum (value, h, typeName, true);
  426. }
  427. protected static DateTime ToTime (string value)
  428. {
  429. return XmlCustomFormatter.ToTime (value);
  430. }
  431. protected static string ToXmlName (string value)
  432. {
  433. return XmlCustomFormatter.ToXmlName (value);
  434. }
  435. protected static string ToXmlNCName (string value)
  436. {
  437. return XmlCustomFormatter.ToXmlNCName (value);
  438. }
  439. protected static string ToXmlNmToken (string value)
  440. {
  441. return XmlCustomFormatter.ToXmlNmToken (value);
  442. }
  443. protected static string ToXmlNmTokens (string value)
  444. {
  445. return XmlCustomFormatter.ToXmlNmTokens (value);
  446. }
  447. protected XmlQualifiedName ToXmlQualifiedName (string value)
  448. {
  449. string name;
  450. string ns;
  451. int lastColon = value.LastIndexOf (':');
  452. string decodedValue = XmlConvert.DecodeName (value);
  453. if (lastColon < 0) {
  454. name = reader.NameTable.Add (decodedValue);
  455. ns = reader.LookupNamespace (String.Empty);
  456. } else {
  457. string prefix = value.Substring (0, lastColon);
  458. ns = reader.LookupNamespace (prefix);
  459. if (ns == null)
  460. throw new InvalidOperationException ("namespace " + prefix + "not defined");
  461. name = reader.NameTable.Add (value.Substring (lastColon + 1));
  462. }
  463. return new XmlQualifiedName (name, ns);
  464. }
  465. protected void UnknownAttribute (object o, XmlAttribute attr)
  466. {
  467. // TODO: line numbers
  468. eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr,0,0,o));
  469. }
  470. protected void UnknownElement (object o, XmlElement elem)
  471. {
  472. // TODO: line numbers
  473. eventSource.OnUnknownElement (new XmlElementEventArgs(elem,0,0,o));
  474. }
  475. protected void UnknownNode (object o)
  476. {
  477. // TODO: line numbers
  478. if (Reader.NodeType == XmlNodeType.Element) Reader.Skip();
  479. eventSource.OnUnknownNode (new XmlNodeEventArgs(0, 0, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
  480. }
  481. protected void UnreferencedObject (string id, object o)
  482. {
  483. eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
  484. }
  485. #endregion // Methods
  486. protected class CollectionFixup {
  487. #region Fields
  488. XmlSerializationCollectionFixupCallback callback;
  489. object collection;
  490. object collectionItems;
  491. #endregion // Fields
  492. #region Constructors
  493. [MonoTODO]
  494. public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, object collectionItems)
  495. {
  496. this.callback = callback;
  497. this.collection = collection;
  498. this.collectionItems = collectionItems;
  499. }
  500. #endregion // Constructors
  501. #region Properties
  502. public XmlSerializationCollectionFixupCallback Callback {
  503. get { return callback; }
  504. }
  505. public object Collection {
  506. get { return collection; }
  507. }
  508. public object CollectionItems {
  509. get { return collectionItems; }
  510. }
  511. #endregion // Properties
  512. }
  513. protected class Fixup {
  514. #region Fields
  515. object source;
  516. string[] ids;
  517. XmlSerializationFixupCallback callback;
  518. #endregion // Fields
  519. #region Constructors
  520. [MonoTODO]
  521. public Fixup (object o, XmlSerializationFixupCallback callback, int count)
  522. {
  523. this.callback = callback;
  524. }
  525. [MonoTODO]
  526. public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
  527. {
  528. this.callback = callback;
  529. }
  530. #endregion // Constructors
  531. #region Properties
  532. public XmlSerializationFixupCallback Callback {
  533. get { return callback; }
  534. }
  535. public string[] Ids {
  536. get { return ids; }
  537. }
  538. public object Source {
  539. get { return source; }
  540. set { source = value; }
  541. }
  542. #endregion // Properties
  543. }
  544. }
  545. }