| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 |
- //
- // System.Xml.Serialization.XmlSerializationReader.cs
- //
- // Authors:
- // Tim Coleman ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- //
- // Copyright (C) Tim Coleman, 2002
- // (c) 2002 Ximian, Inc. (http://www.ximian.com)
- //
- using System;
- using System.Collections;
- using System.Xml;
- namespace System.Xml.Serialization {
- public abstract class XmlSerializationReader {
- #region Fields
- XmlDocument document;
- XmlReader reader;
- ArrayList fixups;
- ArrayList collFixups;
- Hashtable readCallbacks;
- Hashtable typesCallbacks;
- ArrayList noIDTargets;
- Hashtable targets;
- string w3SchemaNS;
- string w3SchemaNS2000;
- string w3SchemaNS1999;
- string w3InstanceNS;
- string w3InstanceNS2000;
- string w3InstanceNS1999;
- string soapNS;
- string schema;
- string wsdlNS;
- string wsdlArrayType;
- string nullX;
- string nil;
- string typeX;
- string arrayType;
- string anyType;
- #endregion
- private void Initialize ()
- {
- w3SchemaNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema");
- w3SchemaNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema");
- w3SchemaNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema");
- w3InstanceNS = reader.NameTable.Add ("http://www.w3.org/2001/XMLSchema-instance");
- w3InstanceNS2000 = reader.NameTable.Add ("http://www.w3.org/2000/10/XMLSchema-instance");
- w3InstanceNS1999 = reader.NameTable.Add ("http://www.w3.org/1999/XMLSchema-instance");
- soapNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/soap/encoding/");
- schema = reader.NameTable.Add ("schema");
- wsdlNS = reader.NameTable.Add ("http://schemas.xmlsoap.org/wsdl/");
- wsdlArrayType = reader.NameTable.Add ("arrayType");
- nullX = reader.NameTable.Add ("null");
- nil = reader.NameTable.Add ("nil");
- typeX = reader.NameTable.Add ("type");
- arrayType = reader.NameTable.Add ("arrayType");
- anyType = reader.NameTable.Add ("anyType");
- InitIDs ();
- }
-
- private ArrayList EnsureArrayList (ArrayList list)
- {
- if (list == null)
- list = new ArrayList ();
- return list;
- }
-
- private Hashtable EnsureHashtable (Hashtable hash)
- {
- if (hash == null)
- hash = new Hashtable ();
- return hash;
- }
-
- protected XmlSerializationReader ()
- {
- }
- protected XmlDocument Document
- {
- get {
- if (document == null)
- document = new XmlDocument (reader.NameTable);
- return document;
- }
- }
- protected XmlReader Reader {
- get { return reader; }
- }
- #region Methods
- protected void AddFixup (CollectionFixup fixup)
- {
- collFixups = EnsureArrayList (collFixups);
- collFixups.Add(fixup);
- }
- protected void AddFixup (Fixup fixup)
- {
- fixups = EnsureArrayList (fixups);
- fixups.Add(fixup);
- }
- protected void AddReadCallback (string name, string ns, Type type, XmlSerializationReadCallback read)
- {
- XmlNameTable nt = reader.NameTable;
- XmlQualifiedName xqn = new XmlQualifiedName (nt.Add (name), nt.Add (ns));
- readCallbacks = EnsureHashtable (readCallbacks);
- readCallbacks.Add (xqn, read);
- typesCallbacks = EnsureHashtable (typesCallbacks);
- typesCallbacks.Add (xqn, type);
- }
- protected void AddTarget (string id, object o)
- {
- if (id != null) {
- targets = EnsureHashtable (targets);
- if (targets [id] == null)
- targets.Add (id, o);
- } else {
- if (o != null)
- return;
- noIDTargets = EnsureArrayList (noIDTargets);
- noIDTargets.Add (o);
- }
- }
- private string CurrentTag ()
- {
- switch (reader.NodeType) {
- case XmlNodeType.None:
- return String.Format ("<{0} xmlns='{1}'>", reader.LocalName,
- reader.NamespaceURI);
- case XmlNodeType.Attribute:
- return reader.Value;
- case XmlNodeType.Text:
- return "CDATA";
- case XmlNodeType.ProcessingInstruction:
- return "<--";
- case XmlNodeType.Entity:
- return "<?";
- case XmlNodeType.EndElement:
- return ">";
- default:
- return "(unknown)";
- }
- }
- protected Exception CreateAbstractTypeException (string name, string ns)
- {
- string message = "Error at " + name + " " + ns + ":" + CurrentTag ();
- return new InvalidOperationException (message);
- }
- protected Exception CreateInvalidCastException (Type type, object value)
- {
- string message = String.Format ("Cannot assign object of type {0} to an object of " +
- "type {1}.", value.GetType (), type);
- return new InvalidCastException (message);
- }
- protected Exception CreateReadOnlyCollectionException (string name)
- {
- string message = String.Format ("Could not serialize {0}. Default constructors are " +
- "required for collections and enumerators.", name);
- return new InvalidOperationException (message);
- }
- protected Exception CreateUnknownConstantException (string value, Type enumType)
- {
- string message = String.Format ("'{0}' is not a valid value for {1}.", value, enumType);
- return new InvalidOperationException (message);
- }
- protected Exception CreateUnknownNodeException ()
- {
- string message = "Unknown xml node -> " + CurrentTag ();
- return new InvalidOperationException (message);
- }
- protected Exception CreateUnknownTypeException (XmlQualifiedName type)
- {
- string message = "Unknown type " + type.Namespace + ":" + type.Name + " " + CurrentTag ();
- return new InvalidOperationException (message);
- }
- protected Array EnsureArrayIndex (Array a, int index, Type elementType)
- {
- if (a != null && index < a.Length)
- return a;
- int size;
- if (a == null) {
- size = 32;
- } else {
- size = a.Length * 2;
- }
- Array result = Array.CreateInstance (elementType, size);
- if (a != null)
- Array.Copy (a, result, index);
- return result;
- }
- [MonoTODO ("Implement")]
- protected void FixupArrayRefs (object fixup)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected int GetArrayLength (string name, string ns)
- {
- throw new NotImplementedException ();
- }
- protected bool GetNullAttr ()
- {
- string na = reader.GetAttribute (nullX, w3InstanceNS);
- if (na == null) {
- na = reader.GetAttribute (nil, w3InstanceNS);
- if (na == null) {
- na = reader.GetAttribute (nullX, w3InstanceNS2000);
- if (na == null)
- na = reader.GetAttribute (nullX, w3InstanceNS1999);
- }
- }
- return (na != null);
- }
- [MonoTODO ("Implement")]
- protected object GetTarget (string id)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected XmlQualifiedName GetXsiType ()
- {
- throw new NotImplementedException ();
- }
- protected abstract void InitCallbacks ();
- protected abstract void InitIDs ();
- protected bool IsXmlnsAttribute (string name)
- {
- int length = name.Length;
- if (length < 5)
- return false;
- if (length == 5)
- return (name == "xmlns");
- return name.StartsWith ("xmlns:");
- }
- [MonoTODO ("Implement")]
- protected void ParseWsdlArrayType (XmlAttribute attr)
- {
- throw new NotImplementedException ();
- }
- protected XmlQualifiedName ReadElementQualifiedName ()
- {
- if (reader.IsEmptyElement) {
- reader.Skip();
- return ToXmlQualifiedName (String.Empty);
- }
- XmlQualifiedName xqn = ToXmlQualifiedName(reader.ReadString ());
- reader.ReadEndElement ();
- return xqn;
- }
- protected void ReadEndElement ()
- {
- while (reader.NodeType == XmlNodeType.Whitespace)
- reader.Skip ();
- if (reader.NodeType != XmlNodeType.None) {
- reader.ReadEndElement ();
- } else {
- reader.Skip ();
- }
- }
- protected bool ReadNull ()
- {
- if (!GetNullAttr ())
- return false;
- if (reader.IsEmptyElement) {
- reader.Skip();
- return true;
- }
- reader.ReadStartElement();
- while (reader.NodeType != XmlNodeType.EndElement)
- UnknownNode (null);
- ReadEndElement ();
- return true;
- }
- protected XmlQualifiedName ReadNullableQualifiedName ()
- {
- if (ReadNull ())
- return null;
- return ReadElementQualifiedName ();
- }
- protected string ReadNullableString ()
- {
- if (ReadNull ())
- return null;
- return reader.ReadElementString ();
- }
- protected bool ReadReference (out string fixupReference)
- {
- string href = reader.GetAttribute ("href");
- if (href == null) {
- fixupReference = null;
- return false;
- }
- if (href [0] != '#')
- throw new InvalidOperationException("href not found: " + href);
- fixupReference = href.Substring (1);
- if (!reader.IsEmptyElement) {
- reader.ReadStartElement ();
- ReadEndElement ();
- } else {
- reader.Skip ();
- }
- return true;
- }
- protected object ReadReferencedElement ()
- {
- return ReadReferencedElement (null, null);
- }
- protected object ReadReferencedElement (string name, string ns)
- {
- string unused;
- return ReadReferencingElement (name, ns, false, out unused);
- }
- protected void ReadReferencedElements ()
- {
- string unused;
- reader.MoveToContent();
- XmlNodeType nt = reader.NodeType;
- while (nt != XmlNodeType.EndElement && nt != XmlNodeType.None) {
- ReadReferencingElement (null, null, true, out unused);
- reader.MoveToContent ();
- nt = reader.NodeType;
- }
- }
- [MonoTODO ("Implement")]
- protected object ReadReferencingElement (out string fixupReference)
- {
- return ReadReferencingElement (null, null, false, out fixupReference);
- }
- protected object ReadReferencingElement (string name, string ns, out string fixupReference)
- {
- return ReadReferencingElement (name, ns, false, out fixupReference);
- }
- [MonoTODO]
- protected object ReadReferencingElement (string name,
- string ns,
- bool elementCanBeType,
- out string fixupReference)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected IXmlSerializable ReadSerializable (IXmlSerializable serializable)
- {
- throw new NotImplementedException ();
- }
- protected string ReadString (string value)
- {
- if (value == null || value == String.Empty)
- return reader.ReadString ();
- return (value + reader.ReadString ());
- }
- [MonoTODO ("Implement")]
- protected object ReadTypedPrimitive (XmlQualifiedName type)
- {
- throw new NotImplementedException ();
- }
- protected XmlNode ReadXmlNode (bool wrapped)
- {
- return document.ReadNode (reader);
- }
- [MonoTODO ("Implement")]
- protected void Referenced (object o)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected Array ShrinkArray (Array a, int length, Type elementType, bool isNullable)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected byte[] ToByteArrayBase64 (bool isNull)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected static byte[] ToByteArrayBase64 (string value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected byte[] ToByteArrayHex (bool isNull)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected static byte[] ToByteArrayHex (string value)
- {
- throw new NotImplementedException ();
- }
- protected static char ToChar (string value)
- {
- return XmlCustomFormatter.ToChar (value);
- }
- protected static DateTime ToDate (string value)
- {
- return XmlCustomFormatter.ToDate (value);
- }
- protected static DateTime ToDateTime (string value)
- {
- return XmlCustomFormatter.ToDateTime (value);
- }
- [MonoTODO ("Implement")]
- protected static long ToEnum (string value, Hashtable h, string typeName)
- {
- throw new NotImplementedException ();
- }
- protected static DateTime ToTime (string value)
- {
- return XmlCustomFormatter.ToTime (value);
- }
- protected static string ToXmlName (string value)
- {
- return XmlCustomFormatter.ToXmlName (value);
- }
- protected static string ToXmlNCName (string value)
- {
- return XmlCustomFormatter.ToXmlNCName (value);
- }
- protected static string ToXmlNmToken (string value)
- {
- return XmlCustomFormatter.ToXmlNmToken (value);
- }
- protected static string ToXmlNmTokens (string value)
- {
- return XmlCustomFormatter.ToXmlNmTokens (value);
- }
- protected XmlQualifiedName ToXmlQualifiedName (string value)
- {
- string name;
- string ns;
- int lastColon = value.LastIndexOf (':');
- string decodedValue = XmlConvert.DecodeName (value);
- if (lastColon < 0) {
- name = reader.NameTable.Add (decodedValue);
- ns = reader.LookupNamespace (String.Empty);
- } else {
- string prefix = value.Substring (0, lastColon);
- ns = reader.LookupNamespace (prefix);
- if (ns == null)
- throw new InvalidOperationException ("namespace " + prefix + "not defined");
- name = reader.NameTable.Add (value.Substring (lastColon + 1));
- }
- return new XmlQualifiedName (name, ns);
- }
- [MonoTODO ("Implement")]
- protected void UnknownAttribute (object o, XmlAttribute attr)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void UnknownElement (object o, XmlElement elem)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void UnknownNode (object o)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void UnreferencedObject (string id, object o)
- {
- throw new NotImplementedException ();
- }
- #endregion // Methods
- protected class CollectionFixup {
-
- #region Fields
- XmlSerializationCollectionFixupCallback callback;
- object collection;
- object collectionItems;
- #endregion // Fields
- #region Constructors
- [MonoTODO]
- public CollectionFixup (object collection, XmlSerializationCollectionFixupCallback callback, object collectionItems)
- {
- this.callback = callback;
- this.collection = collection;
- this.collectionItems = collectionItems;
- }
- #endregion // Constructors
- #region Properties
- public XmlSerializationCollectionFixupCallback Callback {
- get { return callback; }
- }
- public object Collection {
- get { return collection; }
- }
- public object CollectionItems {
- get { return collectionItems; }
- }
- #endregion // Properties
- }
- protected class Fixup {
- #region Fields
- object source;
- string[] ids;
- XmlSerializationFixupCallback callback;
- #endregion // Fields
- #region Constructors
- [MonoTODO]
- public Fixup (object o, XmlSerializationFixupCallback callback, int count)
- {
- this.callback = callback;
- }
- [MonoTODO]
- public Fixup (object o, XmlSerializationFixupCallback callback, string[] ids)
- {
- this.callback = callback;
- }
- #endregion // Constructors
- #region Properties
- public XmlSerializationFixupCallback Callback {
- get { return callback; }
- }
- public string[] Ids {
- get { return ids; }
- }
- public object Source {
- get { return source; }
- set { source = value; }
- }
- #endregion // Properties
- }
- }
- }
|