Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / AddressHeader.cs
1 //
2 // System.ServiceModel.AddressHeader.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Copyright (C) 2005,2010 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.IO;
32 using System.Runtime.Serialization;
33 using System.ServiceModel;
34 using System.Xml;
35 using System.Xml.Schema;
36
37 namespace System.ServiceModel.Channels
38 {
39         public abstract class AddressHeader
40         {
41                 protected AddressHeader () {}
42
43                 public static AddressHeader CreateAddressHeader (object value)
44                 {
45                         return new DefaultAddressHeader (value);
46                 }
47
48                 public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
49                 {
50                         return new DefaultAddressHeader (value, formatter);
51                 }
52
53                 public static AddressHeader CreateAddressHeader (string name, string ns, object value)
54                 {
55                         return new DefaultAddressHeader (name, ns, value);
56                 }
57
58                 public static AddressHeader CreateAddressHeader (string name, string ns, object value, 
59                                                                  XmlObjectSerializer formatter)
60                 {
61                         if (formatter == null)
62                                 throw new ArgumentNullException ("formatter");
63                         return new DefaultAddressHeader (name, ns, value, formatter);
64                 }
65
66                 public override bool Equals (object obj)
67                 {
68                         AddressHeader o = obj as AddressHeader;
69
70                         if (o == null)
71                                 return false;
72
73                         return o.Name == this.Name && o.Namespace == this.Namespace; 
74                 }
75
76                 public virtual XmlDictionaryReader GetAddressHeaderReader ()
77                 {
78                         var sw = new StringWriter ();
79                         var s = new XmlWriterSettings () { OmitXmlDeclaration = true };
80                         var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, s));
81                         WriteAddressHeader (xw);
82                         xw.Close ();
83                         return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ())));
84                 }
85
86                 public override int GetHashCode ()
87                 {
88                         return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
89                 }
90
91                 public T GetValue<T> ()
92                 {
93                         return GetValue<T> (new DataContractSerializer (typeof (T)));
94                 }
95
96                 public T GetValue<T> (XmlObjectSerializer formatter)
97                 {
98                         return (T) formatter.ReadObject (GetAddressHeaderReader ());
99                 }
100
101                 protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
102                 protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
103                 {
104                         if (Name != null && Namespace != null)
105                                 writer.WriteStartElement (Name, Namespace);
106                 }
107
108                 public MessageHeader ToMessageHeader ()
109                 {
110                         throw new NotImplementedException ();                   
111                 }
112
113                 public void WriteAddressHeader (XmlDictionaryWriter writer)
114                 {
115                         if (writer == null)
116                                 throw new ArgumentNullException ("writer is null");
117                         
118                         this.WriteStartAddressHeader (writer);
119                         this.WriteAddressHeaderContents (writer);
120                         if (Name != null && Namespace != null)
121                                 writer.WriteEndElement ();
122                 }
123
124                 public void WriteAddressHeader (XmlWriter writer)
125                 {
126                         this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
127                 }
128
129                 public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
130                 {
131                         this.OnWriteAddressHeaderContents (writer);
132                 }
133
134                 public void WriteStartAddressHeader (XmlDictionaryWriter writer)
135                 {
136                         this.OnWriteStartAddressHeader (writer);
137                 }
138
139                 public abstract string Name { get; }
140                 public abstract string Namespace { get; }
141
142                 internal class DefaultAddressHeader : AddressHeader
143                 {
144                         string name, ns;
145                         XmlObjectSerializer formatter;
146                         object value;
147
148                         internal DefaultAddressHeader (object value)
149                                 : this (null, null, value) {}
150
151                         
152                         internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
153                                 : this (null, null, value, formatter)
154                         {
155                         }
156
157                         internal DefaultAddressHeader (string name, string ns, object value)
158                                 : this (name, ns, value, null) {}
159                         
160                         internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
161                         {
162                                 if (formatter == null)
163                                         formatter = value != null ? new DataContractSerializer (value.GetType ()) : null;
164                                 this.name = name;
165                                 this.ns = ns;
166                                 this.formatter = formatter;
167                                 this.value = value;
168                         }
169
170                         public override string Name {
171                                 get { return name; }
172                         }
173
174                         public override string Namespace {
175                                 get { return ns; }
176                         }
177
178                         protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
179                         {
180                                 if (value == null)
181                                         writer.WriteAttributeString ("i", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
182                                 else
183                                         this.formatter.WriteObject (writer, value);
184                         }
185                 }
186                 
187         }
188 }