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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
|
// created on 07/04/2003 at 17:56
//
// System.Runtime.Serialization.Formatters.Soap.SoapWriter
//
// Authors:
// Jean-Marc Andre (jean-marc.andre@polymtl.ca)
//
//
// 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.Threading;
using System.Reflection;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Metadata;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Globalization;
using System.Text;
namespace System.Runtime.Serialization.Formatters.Soap {
internal class SoapWriter: IComparer {
private struct EnqueuedObject {
public long _id;
public object _object;
public EnqueuedObject(object currentObject, long id) {
_id = id;
_object = currentObject;
}
public long Id
{
get
{
return _id;
}
}
public object Object
{
get
{
return _object;
}
}
}
#region Fields
private XmlTextWriter _xmlWriter;
private Queue _objectQueue = new Queue();
private Hashtable _objectToIdTable = new Hashtable();
private ISurrogateSelector _surrogateSelector;
private SoapTypeMapper _mapper;
private StreamingContext _context;
private ObjectIDGenerator idGen = new ObjectIDGenerator();
private FormatterAssemblyStyle _assemblyFormat = FormatterAssemblyStyle.Full;
private FormatterTypeStyle _typeFormat = FormatterTypeStyle.TypesWhenNeeded;
private static string defaultMessageNamespace;
SerializationObjectManager _manager;
#endregion
~SoapWriter()
{
}
#region Constructors
internal SoapWriter(
Stream outStream,
ISurrogateSelector selector,
StreamingContext context,
ISoapMessage soapMessage)
{
_xmlWriter = new XmlTextWriter(outStream, null);
_xmlWriter.Formatting = Formatting.Indented;
_surrogateSelector = selector;
_context = context;
_manager = new SerializationObjectManager (_context);
}
static SoapWriter()
{
defaultMessageNamespace = typeof(SoapWriter).Assembly.GetName().FullName;
}
#endregion
public SoapTypeMapper Mapper {
get { return _mapper; }
}
public XmlTextWriter XmlWriter {
get { return _xmlWriter; }
}
#region Internal Properties
internal FormatterAssemblyStyle AssemblyFormat
{
get
{
return _assemblyFormat;
}
set
{
_assemblyFormat = value;
}
}
internal FormatterTypeStyle TypeFormat
{
get
{
return _typeFormat;
}
set
{
_typeFormat = value;
}
}
#endregion
private void Id(long id)
{
_xmlWriter.WriteAttributeString(null, "id", null, "ref-" + id.ToString());
}
private void Href(long href)
{
_xmlWriter.WriteAttributeString(null, "href", null, "#ref-" + href.ToString());
}
private void Null()
{
_xmlWriter.WriteAttributeString("xsi", "null", XmlSchema.InstanceNamespace, "1");
}
private bool IsEncodingNeeded(
object componentObject,
Type componentType)
{
if(componentObject == null)
return false;
if(_typeFormat == FormatterTypeStyle.TypesAlways)
return true;
if(componentType == null)
{
componentType = componentObject.GetType();
if(componentType.IsPrimitive || componentType == typeof(string))
return false;
else
return true;
}
else
{
if(componentType == typeof(object) || componentType != componentObject.GetType())
return true;
else
return false;
}
}
internal void Serialize (object objGraph, Header[] headers, FormatterTypeStyle typeFormat, FormatterAssemblyStyle assemblyFormat)
{
CultureInfo savedCi = CultureInfo.CurrentCulture;
try {
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Serialize_inner (objGraph, headers, typeFormat, assemblyFormat);
} finally {
Thread.CurrentThread.CurrentCulture = savedCi;
}
_manager.RaiseOnSerializedEvent ();
}
void Serialize_inner (object objGraph, Header[] headers, FormatterTypeStyle typeFormat, FormatterAssemblyStyle assemblyFormat)
{
_typeFormat = typeFormat;
_assemblyFormat = assemblyFormat;
// Create the XmlDocument with the
// Envelope and Body elements
_mapper = new SoapTypeMapper(_xmlWriter, assemblyFormat, typeFormat);
// The root element
_xmlWriter.WriteStartElement(
SoapTypeMapper.SoapEnvelopePrefix,
"Envelope",
SoapTypeMapper.SoapEnvelopeNamespace);
// adding namespaces
_xmlWriter.WriteAttributeString(
"xmlns",
"xsi",
"http://www.w3.org/2000/xmlns/",
"http://www.w3.org/2001/XMLSchema-instance");
_xmlWriter.WriteAttributeString(
"xmlns",
"xsd",
"http://www.w3.org/2000/xmlns/",
XmlSchema.Namespace);
_xmlWriter.WriteAttributeString(
"xmlns",
SoapTypeMapper.SoapEncodingPrefix,
"http://www.w3.org/2000/xmlns/",
SoapTypeMapper.SoapEncodingNamespace);
_xmlWriter.WriteAttributeString(
"xmlns",
SoapTypeMapper.SoapEnvelopePrefix,
"http://www.w3.org/2000/xmlns/",
SoapTypeMapper.SoapEnvelopeNamespace);
_xmlWriter.WriteAttributeString(
"xmlns",
"clr",
"http://www.w3.org/2000/xmlns/",
SoapServices.XmlNsForClrType);
_xmlWriter.WriteAttributeString(
SoapTypeMapper.SoapEnvelopePrefix,
"encodingStyle",
SoapTypeMapper.SoapEnvelopeNamespace,
"http://schemas.xmlsoap.org/soap/encoding/");
ISoapMessage msg = objGraph as ISoapMessage;
if (msg != null)
headers = msg.Headers;
if (headers != null && headers.Length > 0)
{
_xmlWriter.WriteStartElement (SoapTypeMapper.SoapEnvelopePrefix, "Header", SoapTypeMapper.SoapEnvelopeNamespace);
foreach (Header h in headers)
SerializeHeader (h);
WriteObjectQueue ();
_xmlWriter.WriteEndElement ();
}
// The body element
_xmlWriter.WriteStartElement(
SoapTypeMapper.SoapEnvelopePrefix,
"Body",
SoapTypeMapper.SoapEnvelopeNamespace);
bool firstTime = false;
if (msg != null)
SerializeMessage(msg);
else
_objectQueue.Enqueue(new EnqueuedObject( objGraph, idGen.GetId(objGraph, out firstTime)));
WriteObjectQueue ();
_xmlWriter.WriteFullEndElement(); // the body element
_xmlWriter.WriteFullEndElement(); // the envelope element
_xmlWriter.Flush();
}
private void WriteObjectQueue ()
{
while(_objectQueue.Count > 0)
{
EnqueuedObject currentEnqueuedObject;
currentEnqueuedObject = (EnqueuedObject) _objectQueue.Dequeue();
object currentObject = currentEnqueuedObject.Object;
Type currentType = currentObject.GetType();
if(!currentType.IsValueType) _objectToIdTable[currentObject] = currentEnqueuedObject.Id;
if(currentType.IsArray)
SerializeArray((Array) currentObject, currentEnqueuedObject.Id);
else
SerializeObject(currentObject, currentEnqueuedObject.Id);
}
}
private void SerializeMessage(ISoapMessage message)
{
bool firstTime;
string ns = message.XmlNameSpace != null ? message.XmlNameSpace : defaultMessageNamespace;
_xmlWriter.WriteStartElement("i2", message.MethodName, ns);
Id(idGen.GetId(message, out firstTime));
string[] paramNames = message.ParamNames;
object[] paramValues = message.ParamValues;
int length = (paramNames != null)?paramNames.Length:0;
for(int i = 0; i < length; i++)
{
_xmlWriter.WriteStartElement(paramNames[i]);
SerializeComponent(paramValues[i], true);
_xmlWriter.WriteEndElement();
}
_xmlWriter.WriteFullEndElement();
}
private void SerializeHeader (Header header)
{
string ns = header.HeaderNamespace != null ? header.HeaderNamespace : "http://schemas.microsoft.com/clr/soap";
_xmlWriter.WriteStartElement ("h4", header.Name, ns);
if (header.MustUnderstand)
_xmlWriter.WriteAttributeString ("mustUnderstand", SoapTypeMapper.SoapEnvelopeNamespace, "1");
_xmlWriter.WriteAttributeString ("root", SoapTypeMapper.SoapEncodingNamespace, "1");
if (header.Name == "__MethodSignature") {
// This is a lame way of identifying the signature header, but looks like it is
// what MS.NET does.
Type[] val = header.Value as Type[];
if (val == null)
throw new SerializationException ("Invalid method signature.");
SerializeComponent (new MethodSignature (val), true);
} else
SerializeComponent (header.Value, true);
_xmlWriter.WriteEndElement();
}
private void SerializeObject(object currentObject, long currentObjectId)
{
bool needsSerializationInfo = false;
ISurrogateSelector selector;
ISerializationSurrogate surrogate = null;
if(_surrogateSelector != null)
{
surrogate = _surrogateSelector.GetSurrogate(
currentObject.GetType(),
_context,
out selector);
}
if(currentObject is ISerializable || surrogate != null) needsSerializationInfo = true;
_manager.RegisterObject (currentObject);
if(needsSerializationInfo)
{
SerializeISerializableObject(currentObject, currentObjectId, surrogate);
}
else
{
if(!currentObject.GetType().IsSerializable)
throw new SerializationException(String.Format("Type {0} in assembly {1} is not marked as serializable.", currentObject.GetType(), currentObject.GetType().Assembly.FullName));
SerializeSimpleObject(currentObject, currentObjectId);
}
}
// implement IComparer
public int Compare(object x, object y)
{
MemberInfo a = x as MemberInfo;
MemberInfo b = y as MemberInfo;
return String.Compare(a.Name, b.Name);
}
private void SerializeSimpleObject(
object currentObject,
long currentObjectId)
{
Type currentType = currentObject.GetType();
// Value type have to be serialized "on the fly" so
// SerializeComponent calls SerializeObject when
// a field of another object is a struct. A node with the field
// name has already be written so WriteStartElement must not be called
// again. Fields that are structs are passed to SerializeObject
// with a id = 0
if(currentObjectId > 0)
{
Element element = _mapper.GetXmlElement (currentType);
_xmlWriter.WriteStartElement(element.Prefix, element.LocalName, element.NamespaceURI);
Id(currentObjectId);
}
if (currentType == typeof(TimeSpan))
{
_xmlWriter.WriteString(SoapTypeMapper.GetXsdValue(currentObject));
}
else if(currentType == typeof(string))
{
_xmlWriter.WriteString(currentObject.ToString());
}
else
{
MemberInfo[] memberInfos = FormatterServices.GetSerializableMembers(currentType, _context);
object[] objectData = FormatterServices.GetObjectData(currentObject, memberInfos);
for(int i = 0; i < memberInfos.Length; i++)
{
FieldInfo fieldInfo = (FieldInfo) memberInfos[i];
SoapFieldAttribute at = (SoapFieldAttribute) InternalRemotingServices.GetCachedSoapAttribute (fieldInfo);
_xmlWriter.WriteStartElement (XmlConvert.EncodeLocalName (at.XmlElementName));
SerializeComponent(
objectData[i],
IsEncodingNeeded(objectData[i], fieldInfo.FieldType));
_xmlWriter.WriteEndElement();
}
}
if(currentObjectId > 0)
_xmlWriter.WriteFullEndElement();
}
private void SerializeISerializableObject(
object currentObject,
long currentObjectId,
ISerializationSurrogate surrogate)
{
Type currentType = currentObject.GetType();
SerializationInfo info = new SerializationInfo(currentType, new FormatterConverter());
ISerializable objISerializable = currentObject as ISerializable;
if(surrogate != null) surrogate.GetObjectData(currentObject, info, _context);
else
{
objISerializable.GetObjectData(info, _context);
}
// Same as above
if(currentObjectId > 0L)
{
Element element = _mapper. GetXmlElement (info.FullTypeName, info.AssemblyName);
_xmlWriter.WriteStartElement(element.Prefix, element.LocalName, element.NamespaceURI);
Id(currentObjectId);
}
foreach(SerializationEntry entry in info)
{
_xmlWriter.WriteStartElement(XmlConvert.EncodeLocalName (entry.Name));
SerializeComponent(entry.Value, IsEncodingNeeded(entry.Value, null));
_xmlWriter.WriteEndElement();
}
if(currentObjectId > 0)
_xmlWriter.WriteFullEndElement();
}
private void SerializeArray(Array currentArray, long currentArrayId)
{
Element element = _mapper.GetXmlElement (typeof(System.Array));
// Set the arrayType attribute
Type arrayType = currentArray.GetType().GetElementType();
Element xmlArrayType = _mapper.GetXmlElement (arrayType);
_xmlWriter.WriteStartElement(element.Prefix, element.LocalName, element.NamespaceURI);
if(currentArrayId > 0) Id(currentArrayId);
if (arrayType == typeof(byte)) {
EncodeType (currentArray.GetType());
_xmlWriter.WriteString (Convert.ToBase64String ((byte[])currentArray));
_xmlWriter.WriteFullEndElement();
return;
}
string prefix = GetNamespacePrefix (xmlArrayType);
StringBuilder str = new StringBuilder();
str.AppendFormat("{0}:{1}[", prefix, xmlArrayType.LocalName);
for(int i = 0; i < currentArray.Rank; i++)
{
str.AppendFormat("{0},", currentArray.GetUpperBound(i) + 1);
}
str.Replace(',', ']', str.Length - 1, 1);
_xmlWriter.WriteAttributeString(
SoapTypeMapper.SoapEncodingPrefix,
"arrayType",
SoapTypeMapper.SoapEncodingNamespace,
str.ToString());
// Get the array items
int lastNonNullItem = 0;
int currentIndex = 0;
// bool specifyEncoding = false;
foreach(object item in currentArray)
{
if(item != null)
{
for(int j = lastNonNullItem; j < currentIndex; j++)
{
_xmlWriter.WriteStartElement("item");
Null();
_xmlWriter.WriteEndElement();
};
lastNonNullItem = currentIndex + 1;
// specifyEncoding |= (arrayType != item.GetType());
_xmlWriter.WriteStartElement("item");
SerializeComponent(item, IsEncodingNeeded(item, arrayType));
_xmlWriter.WriteEndElement();
}
currentIndex++;
}
_xmlWriter.WriteFullEndElement();
}
private void SerializeComponent(
object obj,
bool specifyEncoding)
{
if(_typeFormat == FormatterTypeStyle.TypesAlways)
specifyEncoding = true;
// A null component
if(obj == null)
{
Null();
return;
}
Type objType = obj.GetType();
bool canBeValue = _mapper.IsInternalSoapType (objType);
bool firstTime;
long id = 0;
// An object already serialized
if((id = idGen.HasId(obj, out firstTime)) != 0L)
{
Href((long)idGen.GetId(obj, out firstTime));
return;
}
// A string
if(objType == typeof(string))
{
if(_typeFormat != FormatterTypeStyle.XsdString)
{
id = idGen.GetId(obj, out firstTime);
Id(id);
}
// specifyEncoding = false;
}
// This component has to be
// serialized later
if(!canBeValue && !objType.IsValueType)
{
long href = idGen.GetId(obj, out firstTime);
Href(href);
_objectQueue.Enqueue(new EnqueuedObject(obj, href));
return;
}
if(specifyEncoding)
{
EncodeType(objType);
}
// A struct
if(!canBeValue && objType.IsValueType)
{
SerializeObject(obj, 0);
return;
}
_xmlWriter.WriteString (_mapper.GetInternalSoapValue (this, obj));
}
private void EncodeType(Type type)
{
if(type == null)
throw new SerializationException("Oooops");
Element xmlType = _mapper.GetXmlElement (type);
string prefix = GetNamespacePrefix (xmlType);
_xmlWriter.WriteAttributeString (
"xsi",
"type",
"http://www.w3.org/2001/XMLSchema-instance",
prefix + ":" + xmlType.LocalName);
}
public string GetNamespacePrefix (Element xmlType)
{
string prefix = _xmlWriter.LookupPrefix (xmlType.NamespaceURI);
if(prefix == null || prefix == string.Empty)
{
_xmlWriter.WriteAttributeString(
"xmlns",
xmlType.Prefix,
"http://www.w3.org/2000/xmlns/",
xmlType.NamespaceURI);
return xmlType.Prefix;
}
return prefix;
}
}
}
|