2 // System.ServiceModel.AddressHeader.cs
5 // Duncan Mak (duncan@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Copyright (C) 2005,2010 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.Serialization;
33 using System.ServiceModel;
35 using System.Xml.Schema;
37 namespace System.ServiceModel.Channels
39 public abstract class AddressHeader
41 protected AddressHeader () {}
43 public static AddressHeader CreateAddressHeader (object value)
45 return new DefaultAddressHeader (value);
48 public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
50 return new DefaultAddressHeader (value, formatter);
53 public static AddressHeader CreateAddressHeader (string name, string ns, object value)
55 return new DefaultAddressHeader (name, ns, value);
58 public static AddressHeader CreateAddressHeader (string name, string ns, object value,
59 XmlObjectSerializer formatter)
61 if (formatter == null)
62 throw new ArgumentNullException ("formatter");
63 return new DefaultAddressHeader (name, ns, value, formatter);
66 public override bool Equals (object obj)
68 AddressHeader o = obj as AddressHeader;
73 return o.Name == this.Name && o.Namespace == this.Namespace;
76 public virtual XmlDictionaryReader GetAddressHeaderReader ()
78 var sw = new StringWriter ();
79 var s = new XmlWriterSettings () { OmitXmlDeclaration = true };
80 var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, s));
81 WriteAddressHeader (xw);
83 return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ())));
86 public override int GetHashCode ()
88 return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
91 public T GetValue<T> ()
93 return GetValue<T> (new DataContractSerializer (typeof (T)));
96 public T GetValue<T> (XmlObjectSerializer formatter)
98 return (T) formatter.ReadObject (GetAddressHeaderReader ());
101 protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
102 protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
104 if (Name != null && Namespace != null)
105 writer.WriteStartElement (Name, Namespace);
108 public MessageHeader ToMessageHeader ()
110 throw new NotImplementedException ();
113 public void WriteAddressHeader (XmlDictionaryWriter writer)
116 throw new ArgumentNullException ("writer is null");
118 this.WriteStartAddressHeader (writer);
119 this.WriteAddressHeaderContents (writer);
120 if (Name != null && Namespace != null)
121 writer.WriteEndElement ();
124 public void WriteAddressHeader (XmlWriter writer)
126 this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
129 public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
131 this.OnWriteAddressHeaderContents (writer);
134 public void WriteStartAddressHeader (XmlDictionaryWriter writer)
136 this.OnWriteStartAddressHeader (writer);
139 public abstract string Name { get; }
140 public abstract string Namespace { get; }
142 internal class DefaultAddressHeader : AddressHeader
145 XmlObjectSerializer formatter;
148 internal DefaultAddressHeader (object value)
149 : this (null, null, value) {}
152 internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
153 : this (null, null, value, formatter)
157 internal DefaultAddressHeader (string name, string ns, object value)
158 : this (name, ns, value, null) {}
160 internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
162 if (formatter == null)
163 formatter = value != null ? new DataContractSerializer (value.GetType ()) : null;
166 this.formatter = formatter;
170 public override string Name {
174 public override string Namespace {
178 protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
181 writer.WriteAttributeString ("i", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
183 this.formatter.WriteObject (writer, value);