| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- //
- // System.Xml.Serialization.XmlSerializationWriter.cs
- //
- // Author:
- // Tim Coleman ([email protected])
- //
- // Copyright (C) Tim Coleman, 2002
- //
- using System;
- using System.Collections;
- using System.Text;
- using System.Xml;
- using System.Xml.Schema;
- namespace System.Xml.Serialization {
- public abstract class XmlSerializationWriter {
- #region Fields
- Hashtable references;
- int referenceCount;
- int qnameCount;
- ArrayList namespaces;
- XmlWriter writer;
- #endregion // Fields
- #region Constructors
- [MonoTODO]
- protected XmlSerializationWriter ()
- {
- qnameCount = 0;
- references = new Hashtable ();
- referenceCount = 0;
- }
- #endregion // Constructors
- #region Properties
- protected ArrayList Namespaces {
- get { return namespaces; }
- set { namespaces = value; }
- }
- protected XmlWriter Writer {
- get { return writer; }
- set { writer = value; }
- }
- #endregion // Properties
- #region Methods
- [MonoTODO ("Implement")]
- protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
- {
- throw new NotImplementedException ();
- }
- protected Exception CreateMismatchChoiceException (string value, string elementName, string enumValue)
- {
- string message = String.Format ("Value of {0} mismatches the type of {1}, you need to set it to {2}.", elementName, value, enumValue);
- return new InvalidOperationException (message);
- }
- protected Exception CreateUnknownAnyElementException (string name, string ns)
- {
- string message = String.Format ("The XML element named '{0}' from namespace '{1}' was not expected. The XML element name and namespace must match those provided via XmlAnyElementAttribute(s).", name, ns);
- return new InvalidOperationException (message);
- }
- protected Exception CreateUnknownTypeException (object o)
- {
- return CreateUnknownTypeException (o.GetType ());
- }
- protected Exception CreateUnknownTypeException (Type type)
- {
- string message = String.Format ("The type {0} may not be used in this context.", type);
- return new InvalidOperationException (message);
- }
- protected static string FromByteArrayBase64 (byte[] value)
- {
- return XmlCustomFormatter.FromByteArrayBase64 (value);
- }
- protected static string FromByteArrayHex (byte[] value)
- {
- return XmlCustomFormatter.FromByteArrayHex (value);
- }
- protected static string FromChar (char value)
- {
- return XmlCustomFormatter.FromChar (value);
- }
- protected static string FromDate (DateTime value)
- {
- return XmlCustomFormatter.FromDate (value);
- }
- protected static string FromDateTime (DateTime value)
- {
- return XmlCustomFormatter.FromDateTime (value);
- }
- protected static string FromEnum (long value, string[] values, long[] ids)
- {
- return XmlCustomFormatter.FromEnum (value, values, ids);
- }
- protected static string FromTime (DateTime value)
- {
- return XmlCustomFormatter.FromTime (value);
- }
- protected static string FromXmlName (string name)
- {
- return XmlCustomFormatter.FromXmlName (name);
- }
- protected static string FromXmlNCName (string ncName)
- {
- return XmlCustomFormatter.FromXmlNCName (ncName);
- }
- protected static string FromXmlNmToken (string nmToken)
- {
- return XmlCustomFormatter.FromXmlNmToken (nmToken);
- }
- protected static string FromXmlNmTokens (string nmTokens)
- {
- return XmlCustomFormatter.FromXmlNmTokens (nmTokens);
- }
- [MonoTODO ("Implement")]
- protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName)
- {
- return GetQualifiedName (xmlQualifiedName.Name, xmlQualifiedName.Namespace);
- }
- private string GetId (object o, bool addToReferencesList)
- {
- referenceCount += 1;
- string id = String.Format ("id{0}", referenceCount);
- if (addToReferencesList)
- references[o] = id;
- return id;
- }
- [MonoTODO ("Complete this list.")]
- private string GetPrimitiveTypeName (Type type)
- {
- if (type == typeof (string))
- return "string";
- if (type == typeof (bool))
- return "Boolean";
- if (type == typeof (float))
- return "float";
- if (type == typeof (double))
- return "double";
- if (type == typeof (XmlQualifiedName))
- return "QName";
- return String.Empty;
- }
-
- [MonoTODO ("Need to check for namespace conflicts before blindly allocating qN")]
- private string GetQualifiedName (string name, string ns)
- {
- qnameCount += 1;
- string prefix = String.Format ("q{0}", qnameCount);
- WriteAttribute ("xmlns", prefix, null, ns);
- return String.Format ("{0}:{1}", prefix, name);
- }
- protected abstract void InitCallbacks ();
- [MonoTODO ("Implement")]
- protected void TopLevelElement ()
- {
- throw new NotImplementedException ();
- }
- protected void WriteAttribute (string localName, byte[] value)
- {
- WriteAttribute (localName, String.Empty, value);
- }
- protected void WriteAttribute (string localName, string value)
- {
- WriteAttribute (String.Empty, localName, String.Empty, value);
- }
- protected void WriteAttribute (string localName, string ns, byte[] value)
- {
- Writer.WriteStartAttribute (localName, ns);
- WriteValue (value);
- Writer.WriteEndAttribute ();
- }
- protected void WriteAttribute (string localName, string ns, string value)
- {
- WriteAttribute (String.Empty, localName, ns, value);
- }
- protected void WriteAttribute (string prefix, string localName, string ns, string value)
- {
- Writer.WriteStartAttribute (prefix, localName, ns);
- WriteValue (value);
- Writer.WriteEndAttribute ();
- }
- [MonoTODO ("Implement")]
- protected void WriteElementEncoded (XmlNode node, string name, string ns, bool isNullable, bool any)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteElementLiteral (XmlNode node, string name, string ns, bool isNullable, bool any)
- {
- WriteStartElement (name, ns);
- node.WriteTo (Writer);
- WriteEndElement ();
- }
- protected void WriteElementQualifiedName (string localName, XmlQualifiedName value)
- {
- WriteElementQualifiedName (localName, String.Empty, value, null);
- }
- protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value)
- {
- WriteElementQualifiedName (localName, ns, value, null);
- }
- protected void WriteElementQualifiedName (string localName, XmlQualifiedName value, XmlQualifiedName xsiType)
- {
- WriteElementQualifiedName (localName, String.Empty, value, xsiType);
- }
- protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
- {
- WriteStartElement (localName, ns);
- Writer.WriteString (FromXmlQualifiedName (value));
- WriteEndElement ();
- }
- protected void WriteElementString (string localName, string value)
- {
- WriteElementString (localName, String.Empty, value, null);
- }
- protected void WriteElementString (string localName, string ns, string value)
- {
- WriteElementString (localName, ns, value, null);
- }
- protected void WriteElementString (string localName, string value, XmlQualifiedName xsiType)
- {
- WriteElementString (localName, String.Empty, value, xsiType);
- }
- [MonoTODO ("Implement")]
- protected void WriteElementString (string localName, string ns, string value, XmlQualifiedName xsiType)
- {
- if (xsiType != null) {
- WriteStartElement (localName, ns);
- WriteXsiType (xsiType.Name, xsiType.Namespace);
- Writer.WriteString (value);
- WriteEndElement ();
- }
- else
- Writer.WriteElementString (localName, ns, value);
- }
- protected void WriteElementStringRaw (string localName, byte[] value)
- {
- WriteElementStringRaw (localName, String.Empty, value, null);
- }
- protected void WriteElementStringRaw (string localName, string value)
- {
- WriteElementStringRaw (localName, String.Empty, value, null);
- }
- protected void WriteElementStringRaw (string localName, byte[] value, XmlQualifiedName xsiType)
- {
- WriteElementStringRaw (localName, String.Empty, value, xsiType);
- }
- protected void WriteElementStringRaw (string localName, string ns, byte[] value)
- {
- WriteElementStringRaw (localName, ns, value, null);
- }
- protected void WriteElementStringRaw (string localName, string ns, string value)
- {
- WriteElementStringRaw (localName, ns, value, null);
- }
- protected void WriteElementStringRaw (string localName, string value, XmlQualifiedName xsiType)
- {
- WriteElementStringRaw (localName, String.Empty, value, null);
- }
- [MonoTODO ("Implement")]
- protected void WriteElementStringRaw (string localName, string ns, byte[] value, XmlQualifiedName xsiType)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteElementStringRaw (string localName, string ns, string value, XmlQualifiedName xsiType)
- {
- WriteStartElement (localName, ns);
- if (xsiType != null)
- WriteXsiType (xsiType.Name, xsiType.Namespace);
- Writer.WriteRaw (value);
- WriteEndElement ();
- }
- protected void WriteEmptyTag (string name)
- {
- WriteEmptyTag (name, String.Empty);
- }
- [MonoTODO ("Verify")]
- protected void WriteEmptyTag (string name, string ns)
- {
- Writer.WriteStartElement (name, ns);
- Writer.WriteEndElement ();
- }
- protected void WriteEndElement ()
- {
- WriteEndElement (null);
- }
- [MonoTODO ("Implement")]
- protected void WriteEndElement (object o)
- {
- Writer.WriteEndElement ();
- }
- protected void WriteId (object o)
- {
- WriteAttribute ("id", GetId (o, true));
- }
- protected void WriteNamespaceDeclarations (XmlSerializerNamespaces ns)
- {
- if (ns == null)
- return;
- Hashtable tbl = ns.Namespaces;
- foreach (string key in tbl.Keys) {
- string val = tbl [key] as string;
- if (val == null)
- val = String.Empty;
- WriteAttribute ("xmlns", key, null, val);
- }
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableQualifiedNameEncoded (string name, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableQualifiedNameLiteral (string name, string ns, XmlQualifiedName value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringEncoded (string name, string ns, string value, XmlQualifiedName xsiType)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringLiteral (string name, string ns, string value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
- {
- throw new NotImplementedException ();
- }
- protected void WriteNullTagEncoded (string name)
- {
- WriteNullTagEncoded (name, String.Empty);
- }
- [MonoTODO ("Implement")]
- protected void WriteNullTagEncoded (string name, string ns)
- {
- throw new NotImplementedException ();
- }
- protected void WriteNullTagLiteral (string name)
- {
- WriteNullTagLiteral (name, String.Empty);
- }
- [MonoTODO ("Implement")]
- protected void WriteNullTagLiteral (string name, string ns)
- {
- throw new NotImplementedException ();
- }
- protected void WritePotentiallyReferencingElement (string n, string ns, object o)
- {
- WritePotentiallyReferencingElement (n, ns, o, null, false, false);
- }
- protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
- {
- WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
- }
- protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
- {
- WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
- }
- [MonoTODO ("Implement")]
- protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteReferencedElements ()
- {
- throw new NotImplementedException ();
- }
- protected void WriteReferencingElement (string n, string ns, object o)
- {
- WriteReferencingElement (n, ns, o, false);
- }
- [MonoTODO ("Implement")]
- protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("Implement")]
- protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
- {
- throw new NotImplementedException ();
- }
- protected void WriteStartDocument ()
- {
- Writer.WriteStartDocument ();
- }
- protected void WriteStartElement (string name)
- {
- WriteStartElement (name, String.Empty, null, false);
- }
- protected void WriteStartElement (string name, string ns)
- {
- WriteStartElement (name, ns, null, false);
- }
- protected void WriteStartElement (string name, string ns, bool writePrefixed)
- {
- WriteStartElement (name, ns, null, writePrefixed);
- }
- protected void WriteStartElement (string name, string ns, object o)
- {
- WriteStartElement (name, ns, o, false);
- }
- [MonoTODO]
- protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
- {
- if (writePrefixed)
- Writer.WriteStartElement (String.Empty, name, ns);
- else
- Writer.WriteStartElement (name, ns);
- }
- protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
- {
- string value;
- WriteStartElement (name, ns);
- if (o is XmlQualifiedName)
- value = FromXmlQualifiedName ((XmlQualifiedName) o);
- else
- value = o.ToString ();
- if (xsiType)
- WriteXsiType (GetPrimitiveTypeName (o.GetType ()), XmlSchema.Namespace);
- WriteValue (value);
- WriteEndElement ();
- }
- protected void WriteValue (byte[] value)
- {
- Writer.WriteBase64 (value, 0, value.Length);
- }
- protected void WriteValue (string value)
- {
- Writer.WriteString (value);
- }
- protected void WriteXmlAttribute (XmlNode node)
- {
- WriteXmlAttribute (node, null);
- }
- [MonoTODO ("Implement")]
- protected void WriteXmlAttribute (XmlNode node, object container)
- {
- if (!(node is XmlAttribute))
- throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
- throw new NotImplementedException ();
- }
- protected void WriteXsiType (string name, string ns)
- {
- WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
- }
-
- #endregion
- }
- }
|