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