2009-02-18 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / AddressHeader.cs
1 //
2 // System.ServiceModel.AddressHeader.cs
3 //
4 // Author: Duncan Mak (duncan@novell.com)
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28 using System;
29 using System.Runtime.Serialization;
30 using System.ServiceModel;
31 using System.Xml;
32
33 namespace System.ServiceModel.Channels
34 {
35         public abstract class AddressHeader
36         {
37                 protected AddressHeader () {}
38
39                 public static AddressHeader CreateAddressHeader (object value)
40                 {
41                         return new DefaultAddressHeader (value);
42                 }
43
44                 public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
45                 {
46                         return new DefaultAddressHeader (value, formatter);
47                 }
48
49                 public static AddressHeader CreateAddressHeader (string name, string ns, object value)
50                 {
51                         return new DefaultAddressHeader (name, ns, value);
52                 }
53
54                 public static AddressHeader CreateAddressHeader (string name, string ns, object value, 
55                                                                  XmlObjectSerializer formatter)
56                 {
57                         if (formatter == null)
58                                 throw new ArgumentNullException ("formatter");
59                         return new DefaultAddressHeader (name, ns, value, formatter);
60                 }
61
62                 public override bool Equals (object obj)
63                 {
64                         AddressHeader o = obj as AddressHeader;
65
66                         if (o == null)
67                                 return false;
68
69                         return o.Name == this.Name && o.Namespace == this.Namespace; 
70                 }
71
72                 [MonoTODO]
73                 public virtual XmlDictionaryReader GetAddressHeaderReader ()
74                 {
75                         throw new NotImplementedException ();
76                 }
77
78                 public override int GetHashCode ()
79                 {
80                         return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
81                 }
82
83                 public T GetValue<T> ()
84                 {
85                         return GetValue<T> (new DataContractSerializer (typeof (T)));
86                 }
87
88                 public T GetValue<T> (XmlObjectSerializer formatter)
89                 {
90                         throw new NotImplementedException ();
91                 }
92
93                 protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
94                 protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
95                 {
96                         if (Name != null && Namespace != null)
97                                 writer.WriteStartElement (Name, Namespace);
98                 }
99
100                 public MessageHeader ToMessageHeader ()
101                 {
102                         throw new NotImplementedException ();                   
103                 }
104
105                 public void WriteAddressHeader (XmlDictionaryWriter writer)
106                 {
107                         if (writer == null)
108                                 throw new ArgumentNullException ("writer is null");
109                         
110                         this.WriteStartAddressHeader (writer);
111                         this.WriteAddressHeaderContents (writer);
112                         if (Name != null && Namespace != null)
113                                 writer.WriteEndElement ();
114                 }
115
116                 public void WriteAddressHeader (XmlWriter writer)
117                 {
118                         this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
119                 }
120
121                 public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
122                 {
123                         this.OnWriteAddressHeaderContents (writer);
124                 }
125
126                 public void WriteStartAddressHeader (XmlDictionaryWriter writer)
127                 {
128                         this.OnWriteStartAddressHeader (writer);
129                 }
130
131                 public abstract string Name { get; }
132                 public abstract string Namespace { get; }
133
134                 internal class DefaultAddressHeader : AddressHeader
135                 {
136                         string name, ns;
137                         XmlObjectSerializer formatter;
138                         object value;
139
140                         internal DefaultAddressHeader (object value)
141                                 : this (null, null, value) {}
142
143                         
144                         internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
145                                 : this (null, null, value, formatter)
146                         {
147                         }
148
149                         internal DefaultAddressHeader (string name, string ns, object value)
150                                 : this (name, ns, value, null) {}
151                         
152                         internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
153                         {
154                                 if (formatter == null) {
155                                         if (value == null)
156                                                 formatter = new NetDataContractSerializer ();
157                                         else
158                                                 formatter = new DataContractSerializer (value.GetType ());
159                                 }
160                                 this.name = name;
161                                 this.ns = ns;
162                                 this.formatter = formatter;
163                                 this.value = value;
164                         }
165
166                         public override string Name {
167                                 get { return name; }
168                         }
169
170                         public override string Namespace {
171                                 get { return ns; }
172                         }
173
174                         protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
175                         {
176                                 this.formatter.WriteObject (writer, value);
177                         }
178                 }
179                 
180         }
181 }